You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

30 lines
836 B

import { Req, Res, ReturnValue } from "#/global";
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 {
return h.view("views/login.ejs");
}
@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 = <any>await User.findOne({ where: { username: username } });
if (!account || !(await bcrypt.compare(password, account.password))) {
return h.redirect("/login");
}
request.cookieAuth.set({ id: account.id });
return h.redirect("/");
}
}