FastAPI 学习之路(四十七)WebSockets(三)登录后才可以聊天
之前我们是通过前端自动生成的,这次我们通过注册登录,保存到本地去实现。我们可以应该如何实现呢,首先我们实现一个登录界面。放在templates文件下。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js">
</script>
</head>
<body>
<div>
<p><input id="username" placeholder="用户名"></p>
<p><input id="password" placeholder="密码" type="password"></p>
<button id="login">登录</button>
</div>
<script>
$("#login").click(function () {
$.ajax({
type: "post",
url: "/token",
contentType: "application/json; charset=utf-8",
data: JSON.stringify({
email: $("#username").val(),
password: $("#password").val()
}),
success: function (data) {
if (data["msg"] == "pass") {
window.localStorage.setItem("token", data["token"])
window.location.href="/"
}else {
alert(data["msg"])
}
}
})
})
</script>
</body>
</html>


