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.
 
 
 
 
 

77 lines
1.7 KiB

import {
auth,
config,
method,
route,
swagger,
validate,
} from "@noderun/hapi-router";
import { PromiseReturnValue, Req, Res, ReturnValue } from "#/global";
import UploadFunc from "../demo/_upload";
import Joi from "joi";
import User from "@/model/User";
import * as bcrypt from "bcrypt";
export default class Index{
@auth()
@config({
auth: {
mode: "try",
},
})
index(request: Req, h: Res): ReturnValue {
console.log(request.auth.isAuthenticated);
// console.log(request.auth);
// if (request.isAuthenticated) {
// // 登录了
// } else {
// // 未登录
// }
return h.view("views/index.pug");
}
@method("GET")
@route("/login")
loginView(request, h) {
return h.view("views/login.ejs");
}
@method("POST")
@auth(false)
@route("/login")
async login(request: Req, h: Res): PromiseReturnValue {
const { username, password } = request.payload as any;
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("/");
}
@route("/about")
@auth(false)
async about(request, h) {
console.log(request.auth);
console.log(1);
try {
const User = request.getModel("User")
console.log(await User.findOne({ where: { username: "xieyaxin" } }));
} catch (error) {
console.log(error);
}
console.log(2);
return h.view("views/about.ejs");
}
@route("/{path*}")
async 404(req, h) {
// 404页面
return h.view("404.pug");
}
}