Browse Source

一些插件

dev
1549469775 3 years ago
parent
commit
cdc4288095
  1. 5
      package.json
  2. 883
      pnpm-lock.yaml
  3. 5
      readme.md
  4. 54
      source/plugins/index.ts
  5. 3
      source/route/views/login.ts
  6. 1
      template/layout/layout.pug
  7. 7
      template/views/include/flush.pug
  8. 2
      template/views/include/form_security.pug
  9. 3
      template/views/login.pug

5
package.json

@ -15,10 +15,14 @@
"license": "ISC",
"dependencies": {
"@hapi/cookie": "^11.0.2",
"@hapi/crumb": "^8.0.1",
"@hapi/hapi": "^20.1.2",
"@hapi/hoek": "^10.0.0",
"@hapi/inert": "^6.0.3",
"@hapi/yar": "^10.1.1",
"@types/hapi__hapi": "^20.0.10",
"@types/hapi__vision": "^5.5.3",
"@types/hapi__yar": "^10.1.1",
"bcrypt": "^5.0.1",
"hapi-auth-jwt2": "^10.2.0",
"hapi-sequelizejs": "^4.5.0",
@ -36,6 +40,7 @@
"@hapi/vision": "^6.1.0",
"@noderun/hapi-router": "link:packages\\hapi-router",
"@types/hapi__cookie": "^10.1.4",
"@types/hapi__crumb": "^7.3.3",
"@types/node": "^17.0.21",
"dotenv": "^10.0.0",
"ejs": "^3.1.6",

883
pnpm-lock.yaml

File diff suppressed because it is too large

5
readme.md

@ -2,4 +2,7 @@
* https://darkmodejs.learn.uno/
* https://blog.csdn.net/weixin_39923599/article/details/111214071
* https://pugjs.org/language/attributes.html
* https://blog.meathill.com/tech/fe/interpolation-in-pug-with-extends.html
* https://blog.meathill.com/tech/fe/interpolation-in-pug-with-extends.html
可参考项目
- https://github.com/hks-epod/paydash

54
source/plugins/index.ts

@ -2,7 +2,11 @@ import filePlugin from "./file-plugin";
import path from "path";
import { sourceDir } from "@/util";
import { plugin as routePlugin } from "@noderun/hapi-router";
import {ServerRegisterPluginObject, Plugin} from "@hapi/hapi"
import {ServerRegisterPluginObject, Plugin, Server, Request, ResponseObject} from "@hapi/hapi"
import Hoek from "@hapi/hoek"
import HapiYar from "@hapi/yar";
import HapiCrumb from "@hapi/crumb"
export default [
{
plugin: filePlugin as unknown as Plugin<any>,
@ -24,4 +28,52 @@ export default [
type: "session"
},
},
{
plugin: {
name: "flash",
version: "0.0.1",
// https://github.com/hks-epod/paydash/blob/master/lib/flash.js
register: function (server: Server, options) {
server.ext('onPreResponse', function(request: Request, h) {
// @ts-ignore
if (request.yar && request.yar.flash && request.response.variety === 'view') {
var flash = request.yar.flash();
request.yar.set('_flash', {});
// @ts-ignore
request.response.source.context = Hoek.applyToDefaults(
{
flash: flash
},
// @ts-ignore
request.response.source.context
);
// @ts-ignore
}
return h.continue;
});
}
} as Plugin<any>
},
{
plugin: HapiYar,
options: {
storeBlank: false,
cookieOptions: {
password: "dsRhw1Y5UZqB8SjfClbkrX9PF7yuDMV3JItcW0G4vgpaxONo6mzenHLQET2AiKyPUjjdgjo10amjfghy",
isSecure: false
}
}
},
{
plugin: HapiCrumb,
options: {
autoGenerate: true,
// skip: function(request, reply) {
// return true;
// },
cookieOptions: {
isSecure: false
}
}
},
] as unknown as ServerRegisterPluginObject<any>;

3
source/route/views/login.ts

@ -27,7 +27,8 @@ export default class {
const account = <any>await User.findOne({ where: { username: username } });
if (!account || !(await bcrypt.compare(password, account.password))) {
return h.redirect("/login");
request.yar.flash('error', 'Invalid username or password');
return h.redirect("/login");
}
request.cookieAuth.set({ id: account.id });

1
template/layout/layout.pug

@ -9,6 +9,7 @@ html(lang="zh-cn")
head
meta(charset="UTF-8")
meta(http-equiv="X-UA-Compatible", content="IE=edge")
link(rel="shortcut icon" href="/public/favicon.ico" type="image/x-icon")
//- device-width 宽度低于1100像素的设备直接缩小
meta(name="viewport", content="width=1100, initial-scale=1.0")
title #{title || 'WEB'}

7
template/views/include/flush.pug

@ -0,0 +1,7 @@
if flash&&flash.error
<div class="message message--error">
<ul>
each item in flash.error
<li>#{item}</li>
</ul>
</div>

2
template/views/include/form_security.pug

@ -0,0 +1,2 @@
if crumb
<input type="hidden" name="crumb" value=#{crumb} />

3
template/views/login.pug

@ -4,6 +4,7 @@ block head
+css("css/page/login.css")
block content
include include/flush.pug
h2 天蚕 / 地雀
.container#container
.form-container.sign-up-container
@ -20,6 +21,7 @@ block content
input(type='text', placeholder='请输入用户名')
input(type='email', placeholder='请输入邮箱')
input(type='password', placeholder='请输入密码')
include include/form_security.pug
button(type='submit') 注册
.form-container.sign-in-container
form(action='/login' method='post')
@ -34,6 +36,7 @@ block content
span 填写您的账户
input(type='text',name="username" , placeholder='请输入邮箱')
input(type='password',name="password" , placeholder='请输入密码')
include include/form_security.pug
a(href='#') 忘记密码?
button 登录
.overlay-container

Loading…
Cancel
Save