import { Req, Res, ReturnValue } from "#/global"; import { gFail } from "@/util"; import { auth, config, method, route } 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) { return h.redirect("/") } else { logger.debug("未登录"); } return h.view("views/login.pug"); } @method("POST") @route("/index") @auth(false) async loginRes(request: Req, h: Res): ReturnValue { const { username, password } = request.payload as any; 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("/"); } }