<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>LeanCloud注册登录</title>
<div id="welcome">
Welcome! Please Login.
</div>
<button id="logIn">Login</button>
<button id="signIn">Sign In</button>
<button id='logout' hidden="">Logout</button>
<form name="userLogin" hidden="">
<p>
<label for="userName">User Name</label>
<input type="text" name="userName" placeholder="Please input your name">
</p>
<p>
<label for="userEmail">User Email</label>
<input type="email" name="userEmail" placeholder="Please input your email">
</p>
<p>
<label for="passWord">Password</label>
<input type="password" name="passWord" placeholder="Please input password">
</p>
<p>
<input type="submit" name="" value="Login">
</p>
</form>
<form name="userSignin" hidden="">
<p>
<label for="userName">User Name</label>
<input type="text" name="userName" placeholder="Please input your name">
</p>
<p>
<label for="passWord">Password</label>
<input type="password" name="passWord" placeholder="Please input password">
</p>
<p>
<input type="submit" name="" value="Sign in">
</p>
</form>
</head>
<body>
<script type="text/javascript" src="./node_modules/leancloud-storage/dist/av.js"></script>
<script type="text/javascript">
var APP_ID = 'Your_App_Id';
var APP_KEY = 'Your_App_Key';
AV.init({
appId: APP_ID,
appKey: APP_KEY
});
var TestObject = AV.Object.extend('TestObject');
var testObject = new TestObject();
var loginForm = document.querySelector("form[name=userLogin]")
logIn.addEventListener("click",function (e) {
loginForm.hidden = false
})
loginForm.addEventListener("submit",function (e) {
e.preventDefault()
var user = new AV.User()
user.setUsername(loginForm.userName.value);
user.setPassword(loginForm.passWord.value);
user.setEmail(loginForm.userEmail.value);
user.signUp().then(function (loginedUser) {
console.log(loginedUser)
window.location.reload()
}, function (error) {
console.log("Login Error!")
});
})
var signInForm = document.querySelector("form[name=userSignin")
signIn.addEventListener("click",function (e) {
signInForm.hidden = false
})
signInForm.addEventListener("submit",function (e) {
e.preventDefault()
var userName = signInForm.userName.value
var passWord = signInForm.passWord.value
console.log(userName + " " + passWord);
AV.User.logIn(userName, passWord).then(function (loginedUser) {
console.log(loginedUser)
window.location.reload()
}, function (error) {
console.log("Sign In Error!")
if (error.code === 210) {
alert("用户名和密码不匹配")
} else if (error.code === 211) {
alert("用户名不存在")
}
});
})
var user = AV.User.current()
if (user) {
welcome.textContent = "Welcome! " + user.attributes.username
logout.hidden = false
loginForm.hidden = true
signInForm.hidden = true
}
if (logout) {
logout.addEventListener("click",function (e) {
AV.User.logOut()
window.location.reload()
})
}
</script>
</body>
</html>