|
|
@@ -7,6 +7,7 @@ const bodyparser = require('koa-bodyparser');
|
|
|
const logger = require('koa-logger');
|
|
|
const cors = require('koa2-cors');
|
|
|
const jwt = require('koa-jwt');
|
|
|
+const jsonwebtoken = require('jsonwebtoken')
|
|
|
const helmet = require('koa-helmet');
|
|
|
const mount = require('koa-mount');
|
|
|
const set = require('lodash/set');
|
|
|
@@ -76,12 +77,6 @@ app.use(async (ctx, next) => {
|
|
|
console.log(`${ctx.method} ${ctx.url} - ${ms}ms`);
|
|
|
});
|
|
|
|
|
|
-// app.use(jwt({
|
|
|
-// secret: secret.sign
|
|
|
-// }).unless({
|
|
|
-// path: [/\/api\/user\/login/, /\/api\/user\/register/]
|
|
|
-// }));
|
|
|
-
|
|
|
// Custom 401 handling if you don't want to expose koa-jwt errors to users
|
|
|
app.use(function(ctx, next){
|
|
|
return next().catch((err) => {
|
|
|
@@ -94,8 +89,6 @@ app.use(function(ctx, next){
|
|
|
});
|
|
|
});
|
|
|
//
|
|
|
-// routes
|
|
|
-app.use(router().routes(), router().allowedMethods());
|
|
|
|
|
|
// error-handling
|
|
|
app.on('error', (err, ctx) => {
|
|
|
@@ -164,6 +157,17 @@ async function start () {
|
|
|
await builder.build()
|
|
|
}
|
|
|
|
|
|
+ app.use(jwt({ secret: 'seec-portal' }).unless({
|
|
|
+ // 设置login、register接口,可以不需要认证访问
|
|
|
+ path: [
|
|
|
+ /\/api\/user\/login/, /\/api\/user\/register/, /\/api\/interaction/, /\/api\/receiveIDToken/, /\/api\/authenticate/,
|
|
|
+ /^((?!\/api).)*$/ // 设置除了私有接口外的其它资源,可以不需要认证访问
|
|
|
+ ]
|
|
|
+ }));
|
|
|
+
|
|
|
+ // routes
|
|
|
+ app.use(router().routes(), router().allowedMethods());
|
|
|
+
|
|
|
app.use((ctx) => {
|
|
|
ctx.req.session = ctx.session;
|
|
|
ctx.status = 200
|
|
|
@@ -172,6 +176,76 @@ async function start () {
|
|
|
nuxt.render(ctx.req, ctx.res)
|
|
|
})
|
|
|
|
|
|
+ // app.use((ctx, next) => {
|
|
|
+ // console.log("ctx.url", ctx.url);
|
|
|
+ // console.log("ctx.url.match(/^\\/api/)", ctx.url.match(/\/api/));
|
|
|
+ // if (
|
|
|
+ // ctx.url.match(/\/api\/user\/login/) ||
|
|
|
+ // ctx.url.match(/\/api\/user\/register/)||
|
|
|
+ // ctx.url.match(/\/api\/receiveIDToken/)||
|
|
|
+ // ctx.url.match(/\/api\/interaction/)||
|
|
|
+ // ctx.url.match(/\/api\/authenticate/)
|
|
|
+ // ) {
|
|
|
+ // return next();
|
|
|
+ // }
|
|
|
+ // if (ctx.url.substr(0, 4) === '/api') {
|
|
|
+ // //路由判断是否以/api开头的url,是则进行鉴权,否则直接输入内容
|
|
|
+ // let authorization = ctx.headers.authorization,
|
|
|
+ // token;
|
|
|
+ // console.log('authorization', authorization);
|
|
|
+ // if (ctx.headers) {
|
|
|
+ // if (!authorization) {
|
|
|
+ // ctx.status = 401;
|
|
|
+ // return (ctx.body = "Bad permissions");
|
|
|
+ // }
|
|
|
+ // token = authorization.split(" ")[1];
|
|
|
+ // try {
|
|
|
+ // jwt.verify(token, 'seec-portal', (err, authData) => {
|
|
|
+ // if(err) {
|
|
|
+ // console.log("err:", err);
|
|
|
+ // ctx.status = 401;
|
|
|
+ // return (ctx.body = "Bad permissions");
|
|
|
+ // }
|
|
|
+ // });
|
|
|
+ // // ,refresh;
|
|
|
+ // // //这里写刷新令牌逻辑,refresh是判断是否到达刷新令牌时间结果
|
|
|
+ // // if (refresh) {
|
|
|
+ // // //在刷新时间范围内请求则刷新令牌
|
|
|
+ // // try {
|
|
|
+ // // let newToken = jsonwebtoken.sign(
|
|
|
+ // // user,
|
|
|
+ // // SECRET,
|
|
|
+ // // {
|
|
|
+ // // expiresIn: "1h"
|
|
|
+ // // }
|
|
|
+ // // );
|
|
|
+ // // ctx.cookies.set(
|
|
|
+ // // "authUser",
|
|
|
+ // // JSON.stringify(newToken),
|
|
|
+ // // {
|
|
|
+ // // maxAge: 60000 * 60,
|
|
|
+ // // overwrite: true
|
|
|
+ // // }
|
|
|
+ // // );
|
|
|
+ // // } catch (err) {
|
|
|
+ // // console.log(err);
|
|
|
+ // // }
|
|
|
+ // // }
|
|
|
+ // } catch (err) {
|
|
|
+ // ctx.status = 401;
|
|
|
+ // return (ctx.body = "Bad permissions");
|
|
|
+ // }
|
|
|
+ // }
|
|
|
+ // return next();
|
|
|
+ // } else {
|
|
|
+ // ctx.req.session = ctx.session;
|
|
|
+ // ctx.status = 200
|
|
|
+ // ctx.respond = false // Bypass Koa's built-in response handling
|
|
|
+ // ctx.req.ctx = ctx // This might be useful later on, e.g. in nuxtServerInit or with nuxt-stash
|
|
|
+ // nuxt.render(ctx.req, ctx.res);
|
|
|
+ // }
|
|
|
+ // });
|
|
|
+
|
|
|
app.listen(port, host)
|
|
|
consola.ready({
|
|
|
message: `Server listening on http://${host}:${port}`,
|