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/login.ts b/source/route/views/index.ts similarity index 77% rename from source/route/views/login.ts rename to source/route/views/index.ts index e096074..7045326 100644 --- a/source/route/views/login.ts +++ b/source/route/views/index.ts @@ -7,10 +7,10 @@ import * as bcrypt from "bcrypt"; * 登录界面 */ export default class { - @route("/index") + @route("/login") @auth("try") @method("GET") - async loginView(request: Req, h: Res): ReturnValue { + async login_GET(request: Req, h: Res): ReturnValue { if (request.auth.isAuthenticated) { request.yar.flash('error', '您已经登陆'); return h.redirect("/") @@ -21,9 +21,9 @@ export default class { } @method("POST") - @route("/index") - async loginRes(request: Req, h: Res): ReturnValue { - const { username, password } = request.payload as any; + @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"); @@ -37,22 +37,31 @@ export default class { } request.cookieAuth.set({ id: account.id }); - return h.redirect("/"); + return h.redirect(referrer ? referrer : "/"); } @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("/"); + return h.redirect('/'); } - @method("POST") @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; 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/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