Commit 59d498c9 authored by huanglu's avatar huanglu

feat: kkk

parent f7f1ec72
registry=http://git.jksxb.cn:8081/repository/geek/
disturl=https://npm.taobao.org/dist
sass_binary_site=https://npm.taobao.org/mirrors/node-sass/
phantomjs_cdnurl=https://npm.taobao.org/mirrors/phantomjs/
fsevents_binary_host_mirror=http://npm.taobao.org/mirrors/fsevents/
import { Controller } from 'egg';
export default class Auth extends Controller {
async login() {
const { ctx } = this
console.log('=== login: ', ctx.query)
ctx.body = ctx.helper.success({
userId: ''
})
}
async getUserInfo() {
this.ctx.body = {
ll: 'sfsfs'
}
}
}
import { Context, EggAppConfig } from 'egg';
export default function jwt(options: EggAppConfig['jwt']) {
return async (ctx: Context, next:() => Promise<any>) => {
//拿到不需要验证的token的路由
const routerAuth = ctx.app.config.routerNotAuth;
//获取当前路由
const url = ctx.url;
console.log('=====: ', routerAuth, url)
//判断当前路由是否需要验证token
const flag = routerAuth.includes(url)
if (flag) {
await next();
} else {
//获取token,如果没有传入token,则为空
const token = ctx.request.headers.authorization as string;
// 解析token
try {
ctx.app.jwt.verify(token, options.secret);
await next();
} catch (err) {
ctx.status = 401;
ctx.body = {
code: 401,
message: 'token失效或解析错误',
data: null
}
}
​
}
};
}
import { Context, EggAppConfig } from 'egg';
export default function jwt(options: EggAppConfig['jwt']) {
return async (ctx: Context, next:() => Promise<any>) => {
const token = ctx.request.headers.authorization;
if (token) {
try {
ctx.app.jwt.verify(token, options.secret);
await next();
} catch (error) {
ctx.status = 401;
ctx.body = {
message: error.message,
};
return;
}
} else {
ctx.status = 401;
ctx.body = {
message: '没有token',
};
return;
}
};
}
import { Application } from 'egg';
import socketRouter from './socket-router';
import authRouter from './router/auth';
export default (app: Application) => {
// const jwt = app.middleware.jwt(app.config.jwt);
socketRouter(app);
authRouter(app)
// app.post('/login', controller.socket.login);
// app.post('/createRoom', jwt, controller.socket.createRoom);
// app.post('/joinRoom', jwt, controller.socket.joinRoom);
......
import { Application } from "egg";
export default (app: Application) => {
const { controller } = app
app.get('/login', controller.auth.login)
app.get('/userInfo', controller.auth.getUserInfo)
};
\ No newline at end of file
import { Context, EggAppConfig, EggAppInfo, PowerPartial, Request } from 'egg';
import * as qs from 'qs';
import { Context, EggAppConfig, EggAppInfo, PowerPartial } from 'egg';
// import * as qs from 'qs';
export default (appInfo: EggAppInfo) => {
const config = {} as PowerPartial<EggAppConfig>;
config.keys = appInfo.name + '_1607088260928_8707';
config.middleware = [];
config.middleware = ['auth'];
config.routerNotAuth = [
'/login', '/register'
]
const bizConfig = {
sourceUrl: 'https://geek.jkwljy.com/',
......@@ -16,24 +19,6 @@ export default (appInfo: EggAppInfo) => {
'.nj': 'nunjucks',
},
};
// const redis = {
// host: '192.168.32.32',
// port: 6379,
// };
const io = {
init: { wsEngine: 'ws' },
namespace: {
'/': {
connectionMiddleware: [ 'auth' ], // 通常在这一步进行授权认证,对认证失败的客户端做出相应的处理
packetMiddleware: [ 'packet' ], // 针对消息的处理
},
},
generateId: (req: Request) => { // 自定义 socket.id 生成函数
const data = qs.parse(req.url.split('?')[1]);
return data.userId || new Date().getTime().toFixed(0);
},
// redis,
};
const cors = {
origin: '*',
......@@ -63,7 +48,7 @@ export default (appInfo: EggAppInfo) => {
...config,
...bizConfig,
view,
io,
// io,
jwt,
cors,
onerror,
......
// This file is created by egg-ts-helper@1.25.8
// This file is created by egg-ts-helper@1.25.9
// Do not modify this file!!!!!!!!!
import 'egg';
import ExportAuth from '../../../app/controller/auth';
import ExportSocket from '../../../app/controller/socket';
declare module 'egg' {
interface IController {
auth: ExportAuth;
socket: ExportSocket;
}
}
// This file is created by egg-ts-helper@1.25.8
// This file is created by egg-ts-helper@1.25.9
// Do not modify this file!!!!!!!!!
import 'egg';
......
// This file is created by egg-ts-helper@1.25.8
// This file is created by egg-ts-helper@1.25.9
// Do not modify this file!!!!!!!!!
import 'egg';
......
// This file is created by egg-ts-helper@1.25.8
// This file is created by egg-ts-helper@1.25.9
// Do not modify this file!!!!!!!!!
import 'egg';
import ExportJwt from '../../../app/middleware/jwt';
import ExportAuth from '../../../app/middleware/auth';
declare module 'egg' {
interface IMiddleware {
jwt: typeof ExportJwt;
auth: typeof ExportAuth;
}
}
// This file is created by egg-ts-helper@1.25.8
// This file is created by egg-ts-helper@1.25.9
// Do not modify this file!!!!!!!!!
import 'egg';
......
// This file is created by egg-ts-helper@1.25.8
// This file is created by egg-ts-helper@1.25.9
// Do not modify this file!!!!!!!!!
import 'egg';
......
// This file is created by egg-ts-helper@1.25.8
// This file is created by egg-ts-helper@1.25.9
// Do not modify this file!!!!!!!!!
import 'egg';
......
// This file is created by egg-ts-helper@1.25.8
// This file is created by egg-ts-helper@1.25.9
// Do not modify this file!!!!!!!!!
import 'egg';
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment