-
Hi community, I'm quite new to LoopBack 4 and passport authentication and have a few questions about their implementation. I followed the todo-jwt example and managed to implement JWT authentication, and passport-login to study passport authentication using third-party OAuth2 (e.g. Google). I have the following questions about the
Sorry if I misunderstood some of the basic concepts. I'm trying to migrate my LB3 server to LB4 and got very confused about authentication, as in LB3 this is mostly done by the built-in authentication and Thanks in advance. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
I managed to implement both passport strategies and JWT authentication and it seems to be working. I modified @oAuth2InterceptExpressMiddleware()
@get('/auth/{provider}/callback')
async thirdPartyCallBack(
@param.path.string('provider') provider: string,
@inject(SecurityBindings.USER) user: UserProfile,
@inject(RestBindings.Http.REQUEST) request: RequestWithSession,
@inject(RestBindings.Http.RESPONSE) response: Response,
) {
const userId = user.profile.id;
const foundUser = await this.userRepository.findById(userId);
// convert a User object into a UserProfile object (reduced set of properties)
const userProfile = this.userService.convertToUserProfile(foundUser);
// create a JSON Web Token based on the user profile
const token = await this.jwtService.generateToken(userProfile);
// redirect to front end with JWT
response.cookie('userId', userId, {signed: request.signedCookies ? true : false, maxAge: 2592000});
response.cookie('access_token', token, {signed: request.signedCookies ? true : false, maxAge: 2592000});
response.redirect(`https://front.end.com`);
return response;
} Not sure if this is best practice though. I noticed that here response is |
Beta Was this translation helpful? Give feedback.
I managed to implement both passport strategies and JWT authentication and it seems to be working. I modified
thirdPartyCallback()
inloopback4-example-passport-login/src/controllers/oauth2.controller.ts
to the following to mimicpassportConfigurator.configureProvider()
in LB3 which sends the access token and userId as Cookies: