Coder Social home page Coder Social logo

Comments (2)

shkangr avatar shkangr commented on May 23, 2024 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

from express-typescript-boilerplate.

lixaotec avatar lixaotec commented on May 23, 2024

+1

from express-typescript-boilerplate.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.