nebular: Adding Multiple OAuth2 Strategies Not working

I implemented more than one strategy like this:

strategies: [
          NbPasswordAuthStrategy.setup({
            name: 'xxx',
            baseEndpoint: '/api/',
            login: {
              endpoint: 'login/xxx',
              method: 'post'
            },
            token: {
              class: NbAuthJWTToken,
              key: 'data.token'
            },
            errors: {
              key: 'error.message'
            },
          }),
          NbPasswordAuthStrategy.setup({
            name: 'yyy',
            baseEndpoint: '/api/',
            login: {
              endpoint: 'login',
              method: 'post'
            },
            register: {
              endpoint: 'register',
              method: 'post'
            },
            token: {
              class: NbAuthJWTToken,
              key: 'data.token'
            },
            errors: {
              key: 'error.message'
            }
          }),

But when trying to use xxx strategy, it always returns “There is no Auth Strategy registered under ‘xxx’ name” error:

this.authService.authenticate('xxx', params)
          .subscribe((result: NbAuthResult) => { 

Was trying to debug by adding log build result of auth.service.js:

function (strategyName) {
        console.log('Strategies: ', this.strategies);
        var found = this.strategies.find(function (strategy) { return strategy.getName() === strategyName; });
        if (!found) {
            throw new TypeError("There is no Auth Strategy registered under '" + strategyName + "' name");
        }
        return found;
    };

But in the result the strategies does not have ‘name’ field:

(2) [NbPasswordAuthStrategy, NbPasswordAuthStrategy]
0
:
NbPasswordAuthStrategy
defaultOptions
:
NbPasswordAuthStrategyOptions {baseEndpoint: "/api/auth/", login: {…}, register: {…}, requestPass: {…}, resetPass: {…}, …}
http
:
HttpClient {handler: HttpInterceptingHandler}
options
:
{baseEndpoint: "/api/", login: {…}, register: {…}, requestPass: {…}, resetPass: {…}, …}
route
:
ActivatedRoute {url: BehaviorSubject, params: BehaviorSubject, queryParams: BehaviorSubject, fragment: BehaviorSubject, data: BehaviorSubject, …}
__proto__
:
NbAuthStrategy
1
:
NbPasswordAuthStrategy```

About this issue

  • Original URL
  • State: open
  • Created 6 years ago
  • Reactions: 2
  • Comments: 18 (3 by maintainers)

Most upvoted comments

any update on the fix or documentation update?

Right, one more step and it should be fine as a workaround:


@Injectable()
export class NbGoogleOAuth2Strategy extends NbOAuth2AuthStrategy {
  static setup(options: NbOAuth2AuthStrategyOptions): [NbAuthStrategyClass, NbOAuth2AuthStrategyOptions] {
    return [NbGoogleOAuth2Strategy, options]; // HERE we make sure our strategy reutrn correct class reference
  }
}

@Injectable()
export class NbFacebookOAuth2Strategy extends NbOAuth2AuthStrategy {
  static setup(options: NbOAuth2AuthStrategyOptions): [NbAuthStrategyClass, NbOAuth2AuthStrategyOptions] {
    return [NbFacebookOAuth2Strategy, options]; // HERE we make sure our strategy reutrn correct class reference
  }
}

Yes, it works correctly @nnixaa Thanks!

@nnixaa I did not get it, guys. Where exactly should I add each portion of code?

Hey @nnixaa could you solve it? I have the same problem when trying to do it with more than one NbOAuth2AuthStrategy.

@Injectable()
export class NbGoogleOAuth2Strategy extends NbOAuth2AuthStrategy {}

@Injectable()
export class NbFacebookOAuth2Strategy extends NbOAuth2AuthStrategy {}

export const NB_CORE_PROVIDERS = [
  ...NbAuthModule.forRoot({
    strategies: [
      NbDummyAuthStrategy.setup({
        name: 'email',
        delay: 3000,
      }),
      NbOAuth2AuthStrategy.setup({
        name: 'google',
        clientId: '*******JoYA',
        clientSecret: '',
        authorize: {
          endpoint: 'https://accounts.google.com/o/oauth2/v2/auth',
          responseType: NbOAuth2ResponseType.TOKEN,
          scope: 'https://www.googleapis.com/auth/userinfo.profile',
          redirectUri: 'http://localhost:4100/example/oauth2/callback',
        }
      }),
      NbFacebookOAuth2Strategy.setup({
        name: 'facebook',
        clientId: '****3259',
        clientSecret: '***aea2f6',
        authorize:{
          endpoint: 'https://www.facebook.com/v3.2/dialog/oauth',
          responseType: NbOAuth2ResponseType.TOKEN,
          redirectUri: 'http://localhost:4200/callback',
        }
      }),
    ],
    forms: {
      login: {
        socialLinks: socialLinks,
      },
      register: {
        socialLinks: socialLinks,
      },
    },
  }).providers
];

Error: image

Following screenshots are from the debug which comes from the file: auth.service.js > this.strategies…

tempsnip

tempsnip2