diff --git a/public/js/page/register.js b/public/js/page/register.js new file mode 100644 index 0000000..85ce9af --- /dev/null +++ b/public/js/page/register.js @@ -0,0 +1,10 @@ +function validateForm() { + var password = document.forms["form"]["password"].value + var confrim_pwd = document.forms["form"]["confrim_pwd"].value + if (confrim_pwd !== password) { + alert("确认密码与密码不相同") + return false + } + delete document.forms["form"]["confrim_pwd"].value + return true +} diff --git a/public/style/common/style.css b/public/style/common/style.css index 484566d..9b3430c 100644 --- a/public/style/common/style.css +++ b/public/style/common/style.css @@ -19,3 +19,7 @@ html { margin: 25px; box-shadow: rgba(0, 0, 0, 0.24) 0px 3px 8px; } + +.navbar-dropdown{ + min-width: auto; +} diff --git a/source/auth/index.ts b/source/auth/index.ts index 454953b..0351838 100644 --- a/source/auth/index.ts +++ b/source/auth/index.ts @@ -15,13 +15,17 @@ export async function validateJwt(decoded, request: Req, h) { export async function validateSession(request: Req, session) { const User = request.getModel("User") + loggerSite.debug(`请求路径:${request.path}, 请求方法:${request.method}`) if (session.id) { const result = await User.findOne({ where: { id: session.id } }) if (result == null) { + loggerSite.debug(`${"cooike中存储的用户不存在"}`) return { valid: false } } + loggerSite.debug(`${"当前登录ID:" + session.id}`) return { valid: true, credentials: result } } else { + loggerSite.debug(`${"用户未登录兵器cooike中不存在信息"}`) return { valid: false } } } diff --git a/source/db/data.db b/source/db/data.db index 80e2719..6968b04 100644 Binary files a/source/db/data.db and b/source/db/data.db differ diff --git a/source/models/User.ts b/source/models/User.ts index 0c0b144..413947c 100644 --- a/source/models/User.ts +++ b/source/models/User.ts @@ -12,7 +12,7 @@ interface UserAttributes { deletedAt?: Date } -export interface UserInput extends Optional {} +export interface UserInput extends Optional {} export interface UserOuput extends Required {} export type TUserModel = ReturnType @@ -33,7 +33,7 @@ export default function UserModel(sequelize: Sequelize, DataTypes: DT) { User.init( { id: { - type: DataTypes.INTEGER.UNSIGNED, + type: DataTypes.INTEGER, autoIncrement: true, primaryKey: true, }, @@ -47,7 +47,6 @@ export default function UserModel(sequelize: Sequelize, DataTypes: DT) { }, nickname: { type: DataTypes.STRING, - allowNull: false, }, email: { type: DataTypes.STRING, diff --git a/source/plugins/index.ts b/source/plugins/index.ts index af12679..ebd48b4 100644 --- a/source/plugins/index.ts +++ b/source/plugins/index.ts @@ -93,7 +93,8 @@ export default [ if (request.path.startsWith("/api") || request.path.startsWith("/htmx")) return h.continue // 需要设置auth是try或者true才行 const isLogin = request.auth.isAuthenticated - loggerSite.debug(`是否登录:${isLogin}, 请求路径:${request.path}, 请求方法:${request.method}`) + // const { id } = request.auth.credentials; + // loggerSite.debug(`${isLogin?'当前登录ID:'+id:'未登录用户'}, 请求路径:${request.path}, 请求方法:${request.method}`) // @ts-ignore // console.log(isLogin, request.path, request.response.variety); diff --git a/source/route/views/index.ts b/source/route/views/index.ts index eb19370..9639a39 100644 --- a/source/route/views/index.ts +++ b/source/route/views/index.ts @@ -31,6 +31,7 @@ export default class { @route("/login") async login_POST(request: Req, h: Res): ReturnValue { const { username, password, referrer } = request.payload as any + const User = request.getModel("User") const account = await User.findOne({ where: { username: username } }) @@ -53,7 +54,7 @@ export default class { @route("/register") @auth("try") - @method("GET") + @method(["POST", "GET"]) async registerView(request: Req, h: Res): ReturnValue { if (request.auth.isAuthenticated) { request.yar.flash("warning", "您已经登陆") @@ -61,36 +62,36 @@ export default class { } else { logger.debug("未登录") } - return h.view("views/login.pug") + return h.view("views/register.pug") } @validate({ payload: RegisterUserSchema, + $errto: "/register", + failAction: "function", + failReason: "注册账户不符合规范", }) @method("POST") async register(request: Req, h: Res): ReturnValue { - let { username, password, email, nickname } = request.payload as any - if (!email) { - request.yar.flash("error", "必须填写邮箱") - return h.redirect("/login") - } - if (!username) username = email - if (!nickname) nickname = username + console.log(request); + let { username, password } = request.payload as any const User = request.getModel("User") - logger.trace(username, email) + logger.trace("当前注册用户:" + username) try { const result = await User.findOne({ where: { username: username } }) if (result != null) { request.yar.flash("error", "已存在该用户") - return h.redirect("/login") + return h.redirect("/register") } let salt = bcrypt.genSaltSync(10) let pwdLock = bcrypt.hashSync(password, salt) - await User.create({ username, nickname, password: pwdLock, email }) - return h.redirect("/") + await User.create({ username, password: pwdLock }) + request.yar.flash("success", "用户注册成功") + return h.redirect("/login") } catch (e) { + loggerSite.error(`注册用户失败`, e.message) request.yar.flash("error", "注册用户失败") - return h.redirect("/login") + return h.redirect("/register") } } } diff --git a/source/schema/index.ts b/source/schema/index.ts index 8742c5c..4f0f6e2 100644 --- a/source/schema/index.ts +++ b/source/schema/index.ts @@ -10,14 +10,14 @@ export const UserSchema = Joi.object({ }).or("username", "email") export const RegisterUserSchema = Joi.object({ - username: Joi.string().alphanum().min(6).max(35), + username: Joi.string().alphanum().min(6).max(35).required(), password: Joi.string().pattern(new RegExp("^[a-zA-Z0-9]{3,30}$")).required(), + confrim_pwd: Joi.ref("password"), email: Joi.string() .email({ minDomainSegments: 2, tlds: { allow: ["com", "net"] }, - }) - .required(), + }), nickname: Joi.string().alphanum().min(4).max(35), }) @@ -25,8 +25,8 @@ export const LoginUserSchema = Joi.object({ referrer: Joi.string().allow("").optional(), username: Joi.string().min(6).max(35), //Joi.string().alphanum().min(6).max(35) password: Joi.string().pattern(new RegExp("^[a-zA-Z0-9]{3,30}$")).required(), - email: Joi.string().email({ - minDomainSegments: 2, - tlds: { allow: ["com", "net"] }, - }), -}).or("username", "email") + // email: Joi.string().email({ + // minDomainSegments: 2, + // tlds: { allow: ["com", "net"] }, + // }), +})//.or("username", "email") diff --git a/template/ui/header.pug b/template/ui/header.pug index 13fd7bd..385d01c 100644 --- a/template/ui/header.pug +++ b/template/ui/header.pug @@ -33,12 +33,10 @@ nav.is-fixed-top.navbar(role='navigation', aria-label='main navigation', style=" else .navbar-item.has-dropdown.is-hoverable a.navbar-link - | #{user.nickname} - .navbar-dropdown + div #{user.nickname} + .navbar-dropdown.is-right a.navbar-item | 用户资料 hr.navbar-divider - a.navbar-item + a.navbar-item(href="/logout") | 退出 - //- a.button.is-danger.is-light(href="/logout") - //- | 退出 diff --git a/template/views/register.pug b/template/views/register.pug new file mode 100644 index 0000000..082896b --- /dev/null +++ b/template/views/register.pug @@ -0,0 +1,22 @@ +extends /layout/layout + +block var + -title="注册" // 网页标题 + -hideHeader=true + +block head + +css("style/views/login.css") + +block content + .login + h1.title.is-1 注册 + form(name="form" action='/register' method='post' onsubmit="return validateForm()") + input(type='text', name='username', placeholder='用户名', required) + input(type='password', name='password', placeholder='密码', required) + input(type='password', name='confrim_pwd', placeholder='确认密码', required) + +security + button.btn.btn-primary.btn-block.btn-large(type='submit') 现在注册! + a(href="/login" style="margin-top: 8px;color: white;font-size: 14px;display: inline-block;float: right") 已有账户,前往登陆 + +block script + +script("js/page/register.js")