From 358927eb74d5301837ba3ed1807ba6245373515f Mon Sep 17 00:00:00 2001 From: npmrun <1549469775@qq.com> Date: Tue, 7 Feb 2023 18:35:32 +0800 Subject: [PATCH] add --- public/js/page/register.js | 10 ++++++++++ public/style/common/style.css | 4 ++++ source/auth/index.ts | 4 ++++ source/db/data.db | Bin 94208 -> 94208 bytes source/models/User.ts | 5 ++--- source/plugins/index.ts | 3 ++- source/route/views/index.ts | 29 +++++++++++++++-------------- source/schema/index.ts | 16 ++++++++-------- template/ui/header.pug | 8 +++----- template/views/register.pug | 22 ++++++++++++++++++++++ 10 files changed, 70 insertions(+), 31 deletions(-) create mode 100644 public/js/page/register.js create mode 100644 template/views/register.pug 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 80e27197d18901476bb4d569240997d50054547c..6968b044e1cd145f3bd0dde99f63811c67e4ba4c 100644 GIT binary patch delta 1018 zcmZvbyKmD_6vpi~Nz*i~6QmNAf|Q{H5^!x_+ew^(l2_gIk*8C)nVN^wywloo5@$&R zh#7=L1q1&BiUbn_Q|FFI9l9Xag4n^N7NJS6&b`C^hV#?+opXF)Jiai#T((~#2;$JL zex_c$dPt#{ANJp4=-jJC7JOswd@|?MP3Lq zisY|UHiICtUG^t)fxWhWtJPTtNl5b1Xd$VHm8eSk6i7h$h}6pMBv>zGyo?Kw9*^?@ zL3jybQtkaO_YFo1V%J#;1)W1muijb?F1Qf|kaP)?lV{+eM7;&^(I!9w`CzfiGp%5EV)9Df+NHoqX znRc%&8)QTfsMkAf89*2*Hyd)iSPifqNsv^L zilw(zx4L$;>&bu-&6WS8m!JVbO~x}!XH4zSq84QNI*krfukE;F@P9;s6W4d>;x0j^>W!Kj-^;Id*wTpRZ#$Cp(h#_4YGPJyQa?UH4Ys>x zGmBtv7JkgL)^~Hi=5Eaj+6RKzwTo$Mwq(BZbXlEoI2`En#bzd36^E-id=^hOis;+d SXXw(&gEIQ~Q=o)?HvIvTSOa1J delta 375 zcmZp8z}oPDb%M0u0|o{LaUg~QrkaU5#*7a(Cd}h!6y5xvpIw3L5(EDy{-^wR_%Cf1 zFgVE1&A`FJEY2BPoLW@ee8+zJ9eYNN?n*{|-`Rg0)7`Yfs8(B?F zJv|vJGE*xPD>CyOQu9)a^Yioc@={AwjFMCg4OGhgeGRhmgPqON1N98d^So1hJxwG0 z4YN}`Jv@DVlk>9*3|x#W%q+{(LsQH3Dyx#x-ODqvnTINt1hK@(z{p6~5Qxkb3{0#H z46IBI^vq066tsa%0|WfByuc9Q;4om|pT_r**OF%z_ZMy<&Tyar1A`zC^8@`Uz#-M7 tQpqkZF3#AZUXqxUlL`(MFd;TQID>KK^n00%O4GHo811$@XEN?(0|3i3X&wLo 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")