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.
 
 
 
 
 

100 lines
3.2 KiB

import filePlugin from "./file-plugin";
import path from "path";
import { baseDir, sourceDir } from "@/util";
import { plugin as routePlugin } from "@noderun/hapi-router";
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>,
},
{
plugin: routePlugin as Plugin<any>,
options: {
auth: ['/api'],
sourceDir: [
{
dir: path.resolve(sourceDir, "route/api"),
prefix: "api"
},
{
dir: path.resolve(sourceDir, "route/views"),
prefix: ""
},
],
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', async function(request: Request, h) {
// @ts-ignore
if(request.response.variety === "file") return h.continue; // 返回文件时不处理
// 需要设置auth是try或者true才行
const isLogin = request.auth.isAuthenticated;
console.log("是否登录:",isLogin, request.path);
// @ts-ignore
// console.log(isLogin, request.path, request.response.variety);
let user;
if(isLogin){
const { id } = request.auth.credentials;
const User = request.getModel("User")
user = <any>await User.findOne({ where: { id: id } });
user = user.toJSON();
delete user.password;
}
// @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,
isLogin: isLogin,
user: user
},
// @ts-ignore
request.response.source.context
);
// @ts-ignore
}
return h.continue;
});
}
} as Plugin<any>
},
{
plugin: HapiYar,
options: {
name: "yar",
storeBlank: false,
cookieOptions: {
password: "dsRhw1Y5UZqB8SjfClbkrX9PF7yuDMV3JItcW0G4vgpaxONo6mzenHLQET2AiKyPUjjdgjo10amjfghy",
path: '/',
isSecure: false
}
}
},
{
plugin: HapiCrumb,
options: {
autoGenerate: true,
// skip: function(request, reply) {
// return true;
// },
cookieOptions: {
path: '/',
isSecure: false
}
}
},
] as unknown as ServerRegisterPluginObject<any>;