Commit 1c3f9c81 authored by huanglu's avatar huanglu

fix: bugs

parent e28458d4
...@@ -4,11 +4,17 @@ export default class Auth extends Controller { ...@@ -4,11 +4,17 @@ export default class Auth extends Controller {
async login() { async login() {
const { ctx } = this const { ctx } = this
console.log('====: ', ctx.request.body, ctx.data) const data = await ctx.service.auth.user.login()
const data = await ctx.service.auth.index.register()
ctx.body = ctx.helper.success(data) ctx.body = ctx.helper.success(data)
} }
async register() {
console.log('====== register')
const { ctx } = this
const data = await ctx.service.auth.user.register()
ctx.body = ctx.helper.success(data, '注册成功')
}
async getUserInfo() { async getUserInfo() {
this.ctx.body = { this.ctx.body = {
ll: 'sfsfs' ll: 'sfsfs'
......
...@@ -5,10 +5,9 @@ export default function jwt(options: EggAppConfig['jwt']) { ...@@ -5,10 +5,9 @@ export default function jwt(options: EggAppConfig['jwt']) {
//拿到不需要验证的token的路由 //拿到不需要验证的token的路由
const routerAuth = ctx.app.config.routerNotAuth; const routerAuth = ctx.app.config.routerNotAuth;
//获取当前路由 //获取当前路由
const url = ctx.url; const path = ctx.path;
console.log('=====: ', routerAuth, url)
//判断当前路由是否需要验证token //判断当前路由是否需要验证token
const flag = routerAuth.includes(url) const flag = routerAuth.includes(path)
if (flag) { if (flag) {
await next(); await next();
} else { } else {
...@@ -26,7 +25,6 @@ export default function jwt(options: EggAppConfig['jwt']) { ...@@ -26,7 +25,6 @@ export default function jwt(options: EggAppConfig['jwt']) {
data: null data: null
} }
} }
​
} }
}; };
} }
...@@ -5,12 +5,11 @@ module.exports = () => { ...@@ -5,12 +5,11 @@ module.exports = () => {
try { try {
await next(); await next();
} catch (err) { } catch (err) {
ctx.app.emit("error", err, ctx);
const status = err.status || 500; const status = err.status || 500;
// 生产环境时 500 错误的详细错误内容不返回给客户端,因为可能包含敏感信息 // 生产环境时 500 错误的详细错误内容不返回给客户端,因为可能包含敏感信息
const error = status === 500 && err.message || "Internal Server Error"; const error = status === 500 && err.message || "Internal Server Error";
// 从 error 对象上读出各个属性,设置到响应中 // 从 error 对象上读出各个属性,设置到响应中
ctx.status = status
ctx.body = ctx.helper.fail(error) ctx.body = ctx.helper.fail(error)
} }
}; };
......
...@@ -3,5 +3,5 @@ import { Application } from "egg"; ...@@ -3,5 +3,5 @@ import { Application } from "egg";
export default (app: Application) => { export default (app: Application) => {
const { controller } = app const { controller } = app
app.router.post('/login', controller.auth.login) app.router.post('/login', controller.auth.login)
app.router.get('/userInfo', controller.auth.getUserInfo) app.router.post('/register', controller.auth.register)
}; };
\ No newline at end of file
import { Service } from "egg";
export default class AuthService extends Service {
async register() {
const { ctx, app } = this
const { user, password } = ctx.request.body || {}
if (!user || !password) {
throw new Error('user or password not null ')
}
const token = this.app.jwt.sign({
user,
password
}, app.config.jwt.secret)
if (token) {
// const value = await (app as any).mysql.update('geek-user', { username: user, password, token }, { id: 5 })
const sql = `select ? from ?`
console.log('get user : ', sql)
const value = await (app as any).mysql.query(sql, ['username', 'geek-user'])
console.log('get user : ', value)
return {
token,
user
}
} else {
throw new Error('register failed')
}
}
}
\ No newline at end of file
import { Service } from "egg";
export default class AuthService extends Service {
async register() {
const { ctx, app } = this
const { username, password } = ctx.request.body || {}
if (!username || !password) {
throw new Error('user or password not null ')
}
try {
await (app as any).mysql.insert('user', { username, password })
return {
username
}
} catch (error) {
throw error
}
}
async login() {
const { ctx, app } = this
const { username, password } = ctx.request.body || {}
if (!username || !password) {
throw new Error('user or password not null ')
}
try {
const result = await (app as any).mysql.get('user', { username })
// const token = this.app.jwt.sign({
// username,
// password
// }, app.config.jwt.secret)
// await (app as any).mysql.update('user', { password, token }, { username })
// return {
// token,
// username
// }
console.log('query user: ', result)
return result
} catch (error) {
throw error
}
}
}
\ No newline at end of file
...@@ -6,12 +6,12 @@ type AnyClass = new (...args: any[]) => any; ...@@ -6,12 +6,12 @@ type AnyClass = new (...args: any[]) => any;
type AnyFunc<T = any> = (...args: any[]) => T; type AnyFunc<T = any> = (...args: any[]) => T;
type CanExportFunc = AnyFunc<Promise<any>> | AnyFunc<IterableIterator<any>>; type CanExportFunc = AnyFunc<Promise<any>> | AnyFunc<IterableIterator<any>>;
type AutoInstanceType<T, U = T extends CanExportFunc ? T : T extends AnyFunc ? ReturnType<T> : T> = U extends AnyClass ? InstanceType<U> : U; type AutoInstanceType<T, U = T extends CanExportFunc ? T : T extends AnyFunc ? ReturnType<T> : T> = U extends AnyClass ? InstanceType<U> : U;
import ExportAuthIndex from '../../../app/service/auth/index'; import ExportAuthUser from '.../../../app/service/auth/user
declare module 'egg' { declare module 'egg' {
interface IService { interface IService {
auth: { auth: {
index: AutoInstanceType<typeof ExportAuthIndex>; user: AutoInstanceType<typeof ExportAuthUser>;
} }
} }
} }
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