Open hamzamumtaz007 opened 3 years ago
+1
You can use JWT for authentication in this boilerplate by change src/auth/authorizationChecker.ts files
In my case, I add "BearerTokenMiddleware.ts" in directory /src/api/middlewares/ using npm "@bukalapak/express-bearer-token "
this is my code
import * as express from 'express'
import bearertoken from '@bukalapak/express-bearer-token'
import { ExpressMiddlewareInterface, Middleware } from 'routing-controllers'
@Middleware({ type: 'before' })
export class BearerTokenMiddleware implements ExpressMiddlewareInterface {
use(
req: express.Request,
res: express.Response,
next: express.NextFunction
): any {
return bearertoken()(req, res, next)
}
}
and then you can get jwt in request.token
now you have to change some codes in authorizationChecker.ts
in my case
export const authorizationChecker = (): ((
action: Action,
roles: any[]
) => Promise<boolean> | boolean) => {
const userService = Container.get<UserService>(UserService)
const authService = Container.get<AuthService>(AuthService)
return async (action: Action, roles: string[]): Promise<boolean> => {
// token text is in action.request.token
const token = action.request.token
// pareseFromRequest -> logic that verify jwt by your own secret key
const { userInfo } = authService.parseFromRequest(token)
// if got some errors in token or in select user, have to response errors
// if you set request.user like this you can get currentUser by using "currentUser" in "routing-controllers"
action.request.user = await userService
.findOne({ where: { ...conditions } })
return true
}
}
Hope that my comment can help you anyway
I want to integrate jwt authentication in this boiler plate, also can someone help me give an overview of this in detail? How everything works?