/
JwtPracticeController.ts
50 lines (36 loc) · 1.28 KB
/
JwtPracticeController.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
/**
* Example Controller for the Overnight web-framework.
*
* created by Sean Maxwell Aug 26, 2018
*/
import { JwtManager, ISecureRequest } from '@overnightjs/jwt';
import { Controller, Middleware, Get, Post, Put } from '@overnightjs/core';
import { Request, Response } from 'express';
const jwtMgr = new JwtManager('secret', '10h');
@Controller('api/jwt')
export class JwtPracticeController {
@Get(':email')
private getJwt(req: Request, res: Response): void {
const jwtStr = JwtManager.jwt({
email: req.params.email,
});
res.status(200).json({jwt: jwtStr});
}
@Post('callProtectedRoute')
@Middleware(JwtManager.middleware)
private callProtectedRoute(req: ISecureRequest, res: Response): void {
res.status(200).json({email: req.payload.email});
}
@Put('getJwtAlt/:fullname')
private getJwtFromHandler(req: Request, res: Response): void {
const jwtStr = jwtMgr.jwt({
fullName: req.params.fullname,
});
res.status(200).json({jwt: jwtStr});
}
@Post('callProtectedRouteAlt')
@Middleware(jwtMgr.middleware)
private callProtectedRouteFromHandler(req: ISecureRequest, res: Response): void {
res.status(200).json({fullname: req.payload.fullName});
}
}