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.
51 lines
1.6 KiB
51 lines
1.6 KiB
import { Req, Res, ReturnValue } from "#/global"
|
|
import { UserSchema } from "@/schema"
|
|
import { gFail, gSuccess } from "@/util"
|
|
import { auth, config, method, route, validate } from "@noderun/hapi-router"
|
|
import * as bcrypt from "bcrypt"
|
|
/**
|
|
* 登录界面
|
|
*/
|
|
export default class {
|
|
@method("GET")
|
|
@auth()
|
|
async index(request: Req, h: Res): ReturnValue {
|
|
const isRenderHtmx = Reflect.has(request.query, "htmx")
|
|
const { id } = request.auth.credentials
|
|
const User = request.getModel("User")
|
|
let result = await User.findOne({ where: { id: id } })
|
|
if (result == null) {
|
|
return gFail(null, "不存在该用户")
|
|
}
|
|
const userinfo = result.toJSON()
|
|
delete userinfo.password
|
|
if (isRenderHtmx) {
|
|
return h.view("htmx/path/user.pug", { userinfo })
|
|
}
|
|
return h.view("views/user.pug", { userinfo })
|
|
}
|
|
|
|
@method("GET")
|
|
@auth()
|
|
async logout(request: Req, h: Res): ReturnValue {
|
|
request.yar.flash("success", "用户已退出")
|
|
request.cookieAuth.clear()
|
|
return h.redirect("/")
|
|
}
|
|
|
|
@method("POST")
|
|
@auth()
|
|
async del(request: Req, h: Res): ReturnValue {
|
|
const { id } = request.auth.credentials
|
|
try {
|
|
const User = request.getModel("User")
|
|
await User.destroy({ where: { id: id } })
|
|
request.yar.flash("success", "用户已删除")
|
|
request.cookieAuth.clear()
|
|
} catch (error) {
|
|
loggerSite.error(`用户删除错误`, error.message)
|
|
request.yar.flash("error", "用户删除错误")
|
|
}
|
|
return h.redirect("/")
|
|
}
|
|
}
|
|
|