-
Notifications
You must be signed in to change notification settings - Fork 1.1k
feat: try passport login example with express middleware #5339
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
dbd8733 to
b8f4da6
Compare
| return userProfile; | ||
| }; | ||
|
|
||
| export class FacebookOauth implements Provider<GenericInterceptor<InvocationContext>> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
GenericInterceptor<InvocationContext> can be shorten as Interceptor.
| this.bind('passport-facebook').toProvider(FacebookOauth); | ||
| this.bind('passport-google').toProvider(GoogleOauth); | ||
| this.bind('passport-oauth2').toProvider(CustomOauth2); | ||
|
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can compose 5 interceptors into one (depending on #5343)
import {composeInterceptors} from '@loopback/context';
const authInterceptor = composeInterceptors(
'passport-init-mw', // or use `toInterceptor(passport.initialize())` directly without binding it
'passport-session-mw', // or use `toInterceptor(passport.session())`
'passport-facebook',
'passport-google',
'passport-oauth2');
// ...
@intercept(authInterceptor)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
perfect, thank you
0f3f486 to
baafe2c
Compare
examples/passport-login/data/db.json
Outdated
| "1": "{\"name\":\"Deepak Rajamohan\",\"username\":\"deepak.r.kris@gmail.com\",\"email\":\"deepak.r.kris@gmail.com\",\"id\":1}" | ||
| }, | ||
| "UserIdentity": { | ||
| "3463934540288186": "{\"id\":\"3463934540288186\",\"provider\":\"facebook\",\"profile\":{\"emails\":[{\"value\":\"deepak.r.kris@gmail.com\"}]},\"authScheme\":\"facebook\",\"created\":\"2020-05-06T00:48:12.355Z\",\"userId\":1}" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I remember that we had a discussion not to include db.json as the data change over time.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes, I did include in the git ignore, not sure how this gets included every time, I will fix that as well.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i just removed the file altogether and its reference
| * 'passport-google', | ||
| * 'passport-oauth2' | ||
| */ | ||
| export function OAuth2InterceptExpressMiddleware() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe the name can be simpler as oauth2. This way, the decorator will be @oauth2
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok, but I have to simplify as well as be clear this is via interceptors middleware chain
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I will try simplifying
14482a7 to
9ca3f83
Compare
jannyHou
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 👍 Awesome improvement! LGTM and please make sure tests pass.
| // if provider name is given then use this middleware otherwise pass along | ||
| if (request.query['oauth2-provider-name'] === 'oauth2') { | ||
| return toInterceptor(passport.authenticate('oauth2'))( | ||
| return toInterceptor(passport.authenticate('facebook'))( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please move this function out as we don't want to call passport.authenticate multiple times.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In the code below, we probably should use return next(); instead of just next();.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
agreed
b5ae95d to
1ba52ea
Compare
|
@deepakrkris Please run |
| @@ -0,0 +1,38 @@ | |||
| // Copyright IBM Corp. 2020. All Rights Reserved. | |||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nitpick: we typically name the file as facebook.interceptor.ts.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
agreed
| ExpressRequestHandler, | ||
| } from '@loopback/rest'; | ||
|
|
||
| export class FacebookOauthInterceptor implements Provider<Interceptor> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's mark it with @bind({scope: BindingScope.SINGLETON}).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
agreed
1ba52ea to
f81a1e3
Compare
raymondfeng
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See my comment.
| this.bind('passport-facebook').toProvider(FacebookOauthInterceptor); | ||
| this.bind('passport-google').toProvider(GoogleOauthInterceptor); | ||
| this.bind('passport-oauth2').toProvider(CustomOauth2Interceptor); | ||
| this.bind('set-session-user').toProvider(SessionAuth); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should be able to make these interceptor providers singleton too.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
agreed
f81a1e3 to
5402e37
Compare
Checklist
👉 Read and sign the CLA (Contributor License Agreement) 👈
npm testpasses on your machinepackages/cliwere updatedexamples/*were updated👉 Check out how to submit a PR 👈