-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
87 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import * as Router from 'koa-router'; | ||
|
||
const router = new Router(); | ||
|
||
router.prefix('/api/v0/user'); | ||
|
||
router.get('/:id', async (ctx) => { | ||
const { id } = ctx.params; | ||
ctx.body = { | ||
id, | ||
name: 'John', | ||
age: Math.floor(Math.random() * 100) | ||
}; | ||
}); | ||
|
||
export default router; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,30 @@ | ||
import * as Koa from 'koa'; | ||
import { decodeToken } from './utils'; | ||
import { decodeToken, getToken } from './utils'; | ||
|
||
interface User { | ||
id: string | number; | ||
} | ||
|
||
const auth = async (ctx: Koa.Context, next: Function) => { | ||
try { | ||
// get token from ctx.request, decode token and compare with token in cache => get userid | ||
const uid = await decodeToken(ctx.request); | ||
if (uid) { | ||
// get token from ctx.request | ||
const token = getToken(ctx.request); | ||
if (token) { | ||
try { | ||
// decode token and compare with token in cache => get userid | ||
const user = await decodeToken(token); | ||
const { id: uid } = user as User; | ||
(ctx.request as any).uid = uid; | ||
await next(); | ||
return; | ||
// const cacheToken = cache.get(`user-${uid}-token`); | ||
// if (!cacheToken) { | ||
// ctx.throw(401, 'unauthorized'); | ||
// } | ||
} catch (error) { | ||
ctx.throw(401, 'unauthorized'); | ||
} | ||
} catch (error) { | ||
} else { | ||
ctx.throw(401, 'unauthorized'); | ||
} | ||
ctx.throw(401, 'unauthorized'); | ||
await next(); | ||
}; | ||
|
||
export default auth; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
// connect db and get data |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters