From 34525b03af97bd886dc0d5856ab3a751902baab6 Mon Sep 17 00:00:00 2001 From: npmrun Date: Fri, 25 Nov 2022 17:34:26 +0800 Subject: [PATCH] fixed bug --- public/js/page/login.js | 24 ++++++----- route.txt | 7 ++-- source/route/views/index.ts | 85 +++++++++++++++++++++++++++++++++++++++ source/route/views/index/index.ts | 2 +- source/route/views/login.ts | 76 ---------------------------------- template/helper/ui.pug | 6 ++- template/views/login.pug | 1 + 7 files changed, 110 insertions(+), 91 deletions(-) create mode 100644 source/route/views/index.ts delete mode 100644 source/route/views/login.ts diff --git a/public/js/page/login.js b/public/js/page/login.js index ee5c567..7ef26ef 100644 --- a/public/js/page/login.js +++ b/public/js/page/login.js @@ -1,11 +1,17 @@ -const signUpButton = document.getElementById('signUp'); -const signInButton = document.getElementById('signIn'); -const container = document.getElementById('container'); +// const signUpButton = document.getElementById('signUp'); +// const signInButton = document.getElementById('signIn'); +// const container = document.getElementById('container'); -signUpButton.addEventListener('click', () => { - container.classList.add("right-panel-active"); -}); +// signUpButton.addEventListener('click', () => { +// container.classList.add("right-panel-active"); +// }); -signInButton.addEventListener('click', () => { - container.classList.remove("right-panel-active"); -}); \ No newline at end of file +// signInButton.addEventListener('click', () => { +// container.classList.remove("right-panel-active"); +// }); +const referer = document.getElementById("referrer") +if (referer) { + let url = new URLSearchParams(window.location.search).get("next") + referer.value = url ? url : "" +} +console.log(referer); diff --git a/route.txt b/route.txt index 6f5b23e..d7013d2 100644 --- a/route.txt +++ b/route.txt @@ -12,10 +12,11 @@ D:\1XYX\demo\hapi-demo\source\route\views对应路径: 不需权限 : GET /css 不需权限(提供无需验证): GET / 不需权限(提供无需验证): GET /about - 不需权限 : GET /docs/{path*} + 需要权限 : GET /docs/{path*} 不需权限 : GET /{path*} 不需权限(提供无需验证): GET /login 不需权限 : POST /login - 需要权限 : GET /login/logout - 不需权限 : POST /login/register + 需要权限 : GET /logout + 不需权限(提供无需验证): GET /register + 不需权限 : POST /register 不需权限 : GET /nav \ No newline at end of file diff --git a/source/route/views/index.ts b/source/route/views/index.ts new file mode 100644 index 0000000..7045326 --- /dev/null +++ b/source/route/views/index.ts @@ -0,0 +1,85 @@ +import { Req, Res, ReturnValue } from "#/global"; +import { UserSchema } from "@/schema"; +import { gFail, gSuccess } from "@/util"; +import { auth, config, method, route, validate } from "@noderun/hapi-router"; +import * as bcrypt from "bcrypt"; +/** + * 登录界面 + */ +export default class { + @route("/login") + @auth("try") + @method("GET") + async login_GET(request: Req, h: Res): ReturnValue { + if (request.auth.isAuthenticated) { + request.yar.flash('error', '您已经登陆'); + return h.redirect("/") + } else { + logger.debug("未登录"); + } + return h.view("views/login.pug"); + } + + @method("POST") + @route("/login") + async login_POST(request: Req, h: Res): ReturnValue { + const { username, password, referrer } = request.payload as any; + if(!username || !password ){ + request.yar.flash('error', 'username or password can not be empty.'); + return h.redirect("/login"); + } + const User = request.getModel("User"); + const account = await User.findOne({ where: { username: username } }); + + if (!account || !(await bcrypt.compare(password, account.password))) { + request.yar.flash('error', 'Invalid username or password'); + return h.redirect("/login"); + } + request.cookieAuth.set({ id: account.id }); + + return h.redirect(referrer ? referrer : "/"); + } + + @method("GET") + @auth() + async logout(request: Req, h: Res): ReturnValue { + request.yar.flash('error', '用户已退出'); + request.cookieAuth.clear(); + return h.redirect('/'); + } + + @route("/register") + @auth("try") + @method("GET") + async registerView(request: Req, h: Res): ReturnValue { + if (request.auth.isAuthenticated) { + request.yar.flash('error', '您已经登陆'); + return h.redirect("/") + } else { + logger.debug("未登录"); + } + return h.view("views/login.pug"); + } + + @method("POST") + async register(request: Req, h: Res): ReturnValue { + let { username, password, email } = request.payload as any; + if (!username) username = email; + const User = request.getModel("User") + logger.trace(username, email); + try { + const result = await User.findOne({ where: { username: username } }); + if (result != null) { + request.yar.flash('error', '已存在该用户'); + return h.redirect("/login"); + } + let salt = bcrypt.genSaltSync(10); + let pwdLock = bcrypt.hashSync(password, salt); + await User.create({ username, password: pwdLock, email }); + return h.redirect("/") + } catch (e) { + request.yar.flash('error', '注册用户失败'); + return h.redirect("/login"); + } + } +} diff --git a/source/route/views/index/index.ts b/source/route/views/index/index.ts index d265aec..416b839 100644 --- a/source/route/views/index/index.ts +++ b/source/route/views/index/index.ts @@ -51,8 +51,8 @@ export default class Index { }); } -// @auth() @route("/docs/{path*}") + @auth() async docs(req: Req, h: Res): ReturnValue { // const {id} = req.auth.credentials // try { diff --git a/source/route/views/login.ts b/source/route/views/login.ts deleted file mode 100644 index e096074..0000000 --- a/source/route/views/login.ts +++ /dev/null @@ -1,76 +0,0 @@ -import { Req, Res, ReturnValue } from "#/global"; -import { UserSchema } from "@/schema"; -import { gFail, gSuccess } from "@/util"; -import { auth, config, method, route, validate } from "@noderun/hapi-router"; -import * as bcrypt from "bcrypt"; -/** - * 登录界面 - */ -export default class { - @route("/index") - @auth("try") - @method("GET") - async loginView(request: Req, h: Res): ReturnValue { - if (request.auth.isAuthenticated) { - request.yar.flash('error', '您已经登陆'); - return h.redirect("/") - } else { - logger.debug("未登录"); - } - return h.view("views/login.pug"); - } - - @method("POST") - @route("/index") - async loginRes(request: Req, h: Res): ReturnValue { - const { username, password } = request.payload as any; - if(!username || !password ){ - request.yar.flash('error', 'username or password can not be empty.'); - return h.redirect("/login"); - } - const User = request.getModel("User"); - const account = await User.findOne({ where: { username: username } }); - - if (!account || !(await bcrypt.compare(password, account.password))) { - request.yar.flash('error', 'Invalid username or password'); - return h.redirect("/login"); - } - request.cookieAuth.set({ id: account.id }); - - return h.redirect("/"); - } - - @method("GET") - @auth() - @route("/logout") - async logout(request: Req, h: Res): ReturnValue { - request.yar.flash('error', '用户已退出'); - console.log(111,request.auth.artifacts); - - request.cookieAuth.clear(); - return h.redirect("/"); - } - - @method("POST") - @route("/register") - async register(request: Req, h: Res): ReturnValue { - let { username, password, email } = request.payload as any; - if (!username) username = email; - const User = request.getModel("User") - logger.trace(username, email); - try { - const result = await User.findOne({ where: { username: username } }); - if (result != null) { - request.yar.flash('error', '已存在该用户'); - return h.redirect("/login"); - } - let salt = bcrypt.genSaltSync(10); - let pwdLock = bcrypt.hashSync(password, salt); - await User.create({ username, password: pwdLock, email }); - return h.redirect("/") - } catch (e) { - request.yar.flash('error', '注册用户失败'); - return h.redirect("/login"); - } - } -} diff --git a/template/helper/ui.pug b/template/helper/ui.pug index f3f0c17..67ba7ce 100644 --- a/template/helper/ui.pug +++ b/template/helper/ui.pug @@ -14,10 +14,12 @@ mixin header() .navbar-text if !isLogin a(href="/login") - .btn.btn-sm.btn-outline-secondary 登陆/注册 + .btn.btn-sm.btn-outline-secondary 登陆 + a(href="/login") + .btn.btn-sm.btn-outline-secondary 注册 else div #{user.username} - a(href="/login/logout") + a(href="/logout") .btn.btn-sm.btn-outline-secondary 退出 //- form(action="/login/logout", method="post") //- include @/helper/form_security.pug diff --git a/template/views/login.pug b/template/views/login.pug index f5c6b95..e7f8443 100644 --- a/template/views/login.pug +++ b/template/views/login.pug @@ -11,6 +11,7 @@ block content .login h1 登录 form(action='/login' method='post') + input(id="referrer" type="text" name="referrer" class="form-control" style="display:none;") input(type='text', name='username', placeholder='用户名', required) input(type='password', name='password', placeholder='密码', required) include @/helper/form_security.pug