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.
 
 
 
 
 

37 lines
1.0 KiB

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 = <any>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("/");
}
}