Skip to content

Commit

Permalink
fix: interceptor
Browse files Browse the repository at this point in the history
  • Loading branch information
deepakrkris committed May 1, 2020
1 parent 16bce44 commit bda5b70
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 34 deletions.
21 changes: 13 additions & 8 deletions examples/passport-login/src/application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import {BootMixin} from '@loopback/boot';
import {RepositoryMixin} from '@loopback/repository';
import {RestApplication} from '@loopback/rest';
import {RestApplication, toInterceptor} from '@loopback/rest';
import {ServiceMixin} from '@loopback/service-proxy';
import {MySequence} from './sequence';
import {AuthenticationComponent} from '@loopback/authentication';
Expand All @@ -16,14 +16,12 @@ import {
LocalAuthStrategy,
SessionStrategy,
BasicStrategy,
FacebookOauth2MW,
PassportInitMW,
PassportSessionMW,
FacebookOauth,
} from './authentication-strategies';
import {PassportUserIdentityService, UserServiceBindings} from './services';
import {ApplicationConfig, createBindingFromClass} from '@loopback/core';
import {CrudRestComponent} from '@loopback/rest-crud';
import {createMiddlewareInterceptorBinding} from '@loopback/rest';
import passport from "passport";

export class OAuth2LoginApplication extends BootMixin(
ServiceMixin(RepositoryMixin(RestApplication)),
Expand All @@ -39,9 +37,16 @@ export class OAuth2LoginApplication extends BootMixin(
this.component(AuthenticationComponent);
this.component(CrudRestComponent);

this.add(createMiddlewareInterceptorBinding(PassportInitMW));
this.add(createMiddlewareInterceptorBinding(PassportSessionMW));
this.add(createMiddlewareInterceptorBinding(FacebookOauth2MW));
this.bind('passport-init-mw').to(toInterceptor(passport.initialize()));
this.bind('passport-session-mw').to(toInterceptor(passport.session()));
passport.serializeUser(function(user: any, done) {
done(null, user);
});

passport.deserializeUser(function(user: any, done) {
done(null, user);
});
this.bind('passport-facebook').toProvider(FacebookOauth);

this.projectRoot = __dirname;
// Customize @loopback/boot Booter Conventions here
Expand Down
18 changes: 1 addition & 17 deletions examples/passport-login/src/authentication-strategies/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,23 +98,7 @@ export const mapProfile = function (user: User): UserProfile {
return userProfile;
};

export class PassportInitMW implements Provider<Interceptor> {
constructor() {}

value<Interceptor>() {
return toInterceptor(passport.initialize());
}
}

export class PassportSessionMW implements Provider<Interceptor> {
constructor() {}

value<Interceptor>() {
return toInterceptor(passport.session());
}
}

export class FacebookOauth2MW implements Provider<Interceptor> {
export class FacebookOauth implements Provider<Interceptor> {
constructor(
@inject('facebookOAuth2Options')
public facebookOptions: StrategyOption,
Expand Down
14 changes: 5 additions & 9 deletions examples/passport-login/src/controllers/facebook.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
RequestWithSession,
} from '@loopback/rest';
import {inject, intercept} from '@loopback/core';
import {SecurityBindings, UserProfile} from '@loopback/security';
import {UserProfile} from '@loopback/security';

/**
* Login controller for facebook
Expand All @@ -19,9 +19,7 @@ export class FacebookController {
constructor(
) {}

@intercept('PassportInitMW')
@intercept('PassportSessionMW')
@intercept('FacebookOauth2MW')
@intercept('passport-init-mw', 'passport-session-mw', 'passport-facebook')
@get('/auth/facebook')
/**
* Endpoint: '/auth/facebook'
Expand All @@ -40,20 +38,18 @@ export class FacebookController {
return response;
}

@intercept('PassportInitMW')
@intercept('PassportSessionMW')
@intercept('FacebookOauth2MW')
@intercept('passport-init-mw', 'passport-session-mw', 'passport-facebook')
@get('/facebook/callback')
/**
* Endpoint: '/auth/facebook/callback'
*/
async thirdPartyCallBack(
@inject(SecurityBindings.USER) user: UserProfile,
@inject(RestBindings.Http.REQUEST) request: RequestWithSession,
@inject(RestBindings.Http.RESPONSE) response: Response,
) {
let user: any = request.user;
const profile = {
...user.profile,
...user,
};
request.session.user = profile;
response.redirect('/auth/account');
Expand Down

0 comments on commit bda5b70

Please sign in to comment.