Skip to content

loadUserProfile() : Error performing password flow TypeError: Cannot read property 'toLowerCase' of null #260

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

Closed
FarhatW opened this issue Mar 8, 2018 · 14 comments

Comments

@FarhatW
Copy link

FarhatW commented Mar 8, 2018

Hi,

I want to get user profile, so i used it like this :

const user = this.oauthService.loadUserProfile()
console.log(user, user );

My conf :
export const authConfig: AuthConfig = {

clientId : 'vegaJs',
redirectUri : 'http://localhost:5002/#/dashboard?',
responseType : 'id_token token',
scope : 'openid profile vega',
postLogoutRedirectUri : 'http://localhost:5002/',
issuer: 'http://localhost:5000',

}

LoginAuthComponent :

claims: any;
constructor(private oauthService: OAuthService) {

// URL of the SPA to redirect the user to after login
this.configureWithNewConfigApi();

}

private configureWithNewConfigApi() {
this.oauthService.configure(authConfig);
this.oauthService.oidc = true;
this.oauthService.setStorage(sessionStorage);
this.oauthService.tokenValidationHandler = new JwksValidationHandler();
this.oauthService.loadDiscoveryDocument().then(() => {
this.oauthService.tryLogin({});
});
}

public login() {
this.oauthService.initImplicitFlow();
}

public logoff() {
this.oauthService.logOut();
}

public getInfo() {
const user = this.oauthService.loadUserProfile()
console.log(user, user );

}

when i call get info, I have Error performing password flow TypeError: Cannot read property 'toLowerCase' of null

@dosama
Copy link

dosama commented Mar 14, 2018

I'm facing same Issue here .. Are there is any updates regarding it ???

@FarhatW
Copy link
Author

FarhatW commented Mar 19, 2018

make this on the redirectUri page :

private configureWithNewConfigApi() {

this.oauthService.configure(authConfig);
this.oauthService.loadDiscoveryDocument().then(() => {
  this.oauthService.tryLogin().then(() => {
      console.log('hasValidAccessToken : ', this.oauthService.hasValidAccessToken());
      console.log('hasValidIdToken : ', this.oauthService.hasValidIdToken());
      console.log('getAccessTokenExpiration : ', this.oauthService.getAccessTokenExpiration());
      console.log('getAccessToken : ', this.oauthService.getAccessToken());
      console.log('getIdToken : ', this.oauthService.getIdToken());

    // this.oauthService.loadUserProfile().then(user => {
    //   console.log('user : ', user);
    // });

  });
});

}

@Cuiqs76
Copy link

Cuiqs76 commented Apr 13, 2018

I have the same issue. i publish to server. in my computer login is OK. I use password type
this.oauthService
.fetchTokenUsingPasswordFlowAndLoadUserProfile(this.userName.value, this.password.value)
.then( data => {
console.log('successfully logged in');
this.loading = false;
this.reuseTabService.clear();
this.router.navigate(['/']);
})
.catch((err => {
console.error('error logging in', err);
this.error = 账户或密码错误;
this.loading = false;
}));

but in other computer throw the error:
angular-oauth2-oidc.umd.js:995 Error performing password flow TypeError: Cannot read property 'toLowerCase' of null
at HttpXsrfInterceptor.webpackJsonp../node_modules/@angular/common/esm5/http.js.HttpXsrfInterceptor.intercept (http.js:2482)
at HttpInterceptorHandler.webpackJsonp../node_modules/@angular/common/esm5/http.js.HttpInterceptorHandler.handle (http.js:1796)
at HttpInterceptingHandler.webpackJsonp../node_modules/@angular/common/esm5/http.js.HttpInterceptingHandler.handle (http.js:2547)
at MergeMapSubscriber.project (http.js:1466)
at MergeMapSubscriber.webpackJsonp../node_modules/rxjs/_esm5/operators/mergeMap.js.MergeMapSubscriber._tryNext (mergeMap.js:128)
at MergeMapSubscriber.webpackJsonp../node_modules/rxjs/_esm5/operators/mergeMap.js.MergeMapSubscriber._next (mergeMap.js:118)
at MergeMapSubscriber.webpackJsonp../node_modules/rxjs/_esm5/Subscriber.js.Subscriber.next (Subscriber.js:95)
at ScalarObservable.webpackJsonp../node_modules/rxjs/_esm5/observable/ScalarObservable.js.ScalarObservable._subscribe (ScalarObservable.js:51)
at ScalarObservable.webpackJsonp../node_modules/rxjs/_esm5/Observable.js.Observable._trySubscribe (Observable.js:172)
at ScalarObservable.webpackJsonp../node_modules/rxjs/_esm5/Observable.js.Observable.subscribe (Observable.js:160)
(anonymous) @ angular-oauth2-oidc.umd.js:995
webpackJsonp../node_modules/rxjs/_esm5/Subscriber.js.SafeSubscriber.__tryOrSetError @ Subscriber.js:252
webpackJsonp../node_modules/rxjs/_esm5/Subscriber.js.SafeSubscriber.error @ Subscriber.js:206
webpackJsonp../node_modules/rxjs/_esm5/Subscriber.js.Subscriber._error @ Subscriber.js:134
webpackJsonp../node_modules/rxjs/_esm5/Subscriber.js.Subscriber.error @ Subscriber.js:108
webpackJsonp../node_modules/rxjs/_esm5/Subscriber.js.Subscriber._error @ Subscriber.js:134
webpackJsonp../node_modules/rxjs/_esm5/Subscriber.js.Subscriber.error @ Subscriber.js:108
webpackJsonp../node_modules/rxjs/_esm5/Subscriber.js.Subscriber._error @ Subscriber.js:134
webpackJsonp../node_modules/rxjs/_esm5/Subscriber.js.Subscriber.error @ Subscriber.js:108
webpackJsonp../node_modules/rxjs/_esm5/operators/mergeMap.js.MergeMapSubscriber._tryNext @ mergeMap.js:131
webpackJsonp../node_modules/rxjs/_esm5/operators/mergeMap.js.MergeMapSubscriber._next @ mergeMap.js:118
webpackJsonp../node_modules/rxjs/_esm5/Subscriber.js.Subscriber.next @ Subscriber.js:95
webpackJsonp../node_modules/rxjs/_esm5/observable/ScalarObservable.js.ScalarObservable._subscribe @ ScalarObservable.js:51
webpackJsonp../node_modules/rxjs/_esm5/Observable.js.Observable._trySubscribe @ Observable.js:172
webpackJsonp../node_modules/rxjs/_esm5/Observable.js.Observable.subscribe @ Observable.js:160
webpackJsonp../node_modules/rxjs/_esm5/operators/mergeMap.js.MergeMapOperator.call @ mergeMap.js:92
webpackJsonp../node_modules/rxjs/_esm5/Observable.js.Observable.subscribe @ Observable.js:157
webpackJsonp../node_modules/rxjs/_esm5/operators/filter.js.FilterOperator.call @ filter.js:61
webpackJsonp../node_modules/rxjs/_esm5/Observable.js.Observable.subscribe @ Observable.js:157
webpackJsonp../node_modules/rxjs/_esm5/operators/map.js.MapOperator.call @ map.js:57
webpackJsonp../node_modules/rxjs/_esm5/Observable.js.Observable.subscribe @ Observable.js:157
(anonymous) @ angular-oauth2-oidc.umd.js:989
ZoneAwarePromise @ zone.js:891
webpackJsonp../node_modules/angular-oauth2-oidc/angular-oauth2-oidc.umd.js.OAuthService.fetchTokenUsingPasswordFlow @ angular-oauth2-oidc.umd.js:972
webpackJsonp../src/app/routes/passport/login/login.component.ts.UserLoginComponent.login @ login.component.ts:63
(anonymous) @ UserLoginComponent.html:1
handleEvent @ core.js:13547
callWithDebugContext @ core.js:15056
debugHandleEvent @ core.js:14643
dispatchEvent @ core.js:9962
(anonymous) @ core.js:12301
schedulerFn @ core.js:4343
webpackJsonp../node_modules/rxjs/_esm5/Subscriber.js.SafeSubscriber.__tryOrUnsub @ Subscriber.js:243
webpackJsonp../node_modules/rxjs/_esm5/Subscriber.js.SafeSubscriber.next @ Subscriber.js:190
webpackJsonp../node_modules/rxjs/_esm5/Subscriber.js.Subscriber._next @ Subscriber.js:131
webpackJsonp../node_modules/rxjs/_esm5/Subscriber.js.Subscriber.next @ Subscriber.js:95
webpackJsonp../node_modules/rxjs/_esm5/Subject.js.Subject.next @ Subject.js:56
webpackJsonp../node_modules/@angular/core/esm5/core.js.EventEmitter.emit @ core.js:4311
webpackJsonp../node_modules/@angular/forms/esm5/forms.js.FormGroupDirective.onSubmit @ forms.js:6768
(anonymous) @ UserLoginComponent.html:1
handleEvent @ core.js:13547
callWithDebugContext @ core.js:15056
debugHandleEvent @ core.js:14643
dispatchEvent @ core.js:9962
(anonymous) @ core.js:10587
(anonymous) @ platform-browser.js:2628
webpackJsonp../node_modules/zone.js/dist/zone.js.ZoneDelegate.invokeTask @ zone.js:421
onInvokeTask @ core.js:4740
webpackJsonp../node_modules/zone.js/dist/zone.js.ZoneDelegate.invokeTask @ zone.js:420
webpackJsonp../node_modules/zone.js/dist/zone.js.Zone.runTask @ zone.js:188
webpackJsonp../node_modules/zone.js/dist/zone.js.ZoneTask.invokeTask @ zone.js:496
invokeTask @ zone.js:1540
globalZoneAwareCallback @ zone.js:1566
login.component.ts:71 error logging in TypeError: Cannot read property 'toLowerCase' of null
at HttpXsrfInterceptor.webpackJsonp../node_modules/@angular/common/esm5/http.js.HttpXsrfInterceptor.intercept (http.js:2482)
at HttpInterceptorHandler.webpackJsonp../node_modules/@angular/common/esm5/http.js.HttpInterceptorHandler.handle (http.js:1796)
at HttpInterceptingHandler.webpackJsonp../node_modules/@angular/common/esm5/http.js.HttpInterceptingHandler.handle (http.js:2547)
at MergeMapSubscriber.project (http.js:1466)
at MergeMapSubscriber.webpackJsonp../node_modules/rxjs/_esm5/operators/mergeMap.js.MergeMapSubscriber._tryNext (mergeMap.js:128)
at MergeMapSubscriber.webpackJsonp../node_modules/rxjs/_esm5/operators/mergeMap.js.MergeMapSubscriber._next (mergeMap.js:118)
at MergeMapSubscriber.webpackJsonp../node_modules/rxjs/_esm5/Subscriber.js.Subscriber.next (Subscriber.js:95)
at ScalarObservable.webpackJsonp../node_modules/rxjs/_esm5/observable/ScalarObservable.js.ScalarObservable._subscribe (ScalarObservable.js:51)
at ScalarObservable.webpackJsonp../node_modules/rxjs/_esm5/Observable.js.Observable._trySubscribe (Observable.js:172)
at ScalarObservable.webpackJsonp../node_modules/rxjs/_esm5/Observable.js.Observable.subscribe (Observable.js:160)
(anonymous) @ login.component.ts:71
webpackJsonp../node_modules/zone.js/dist/zone.js.ZoneDelegate.invoke @ zone.js:388
onInvoke @ core.js:4749
webpackJsonp../node_modules/zone.js/dist/zone.js.ZoneDelegate.invoke @ zone.js:387
webpackJsonp../node_modules/zone.js/dist/zone.js.Zone.run @ zone.js:138
(anonymous) @ zone.js:872
webpackJsonp../node_modules/zone.js/dist/zone.js.ZoneDelegate.invokeTask @ zone.js:421
onInvokeTask @ core.js:4740
webpackJsonp../node_modules/zone.js/dist/zone.js.ZoneDelegate.invokeTask @ zone.js:420
webpackJsonp../node_modules/zone.js/dist/zone.js.Zone.runTask @ zone.js:188
drainMicroTaskQueue @ zone.js:595
webpackJsonp../node_modules/zone.js/dist/zone.js.ZoneTask.invokeTask @ zone.js:500
invokeTask @ zone.js:1540
globalZoneAwareCallback @ zone.js:1566

@FarhatW
Copy link
Author

FarhatW commented Apr 13, 2018

try to call it into this

this.oauthService.loadDiscoveryDocument().then(() => {
this.oauthService.tryLogin().then(() => {
test if you have a valid token
else call your code

}

}

@dirkbolte
Copy link
Contributor

I ran into the same issue. In my case, I was using plain oauth instead of OIDC, so I had no discovery document and had to configure various parts myself. In my case, I got the error because a call to http was done with an empty URL as I hadn't configured the tokenEndpoint. The issue went away as soon as I had it configured. Furthermore, I had to adjust the setting as the default one for requireHttps is remoteOnly - which obviously failed in local testing. So changing it to true + setting the config value fixed it for me.

This might not be the same issue that was encountered here but it might help with debugging down the line.

@DoanVanThuong
Copy link

DoanVanThuong commented Feb 28, 2020

I resolve by using:

this.oauthService.fetchTokenUsingPasswordFlow(username,password)
    .then((tokenInfo: any) => {
     return this.oauthService.loadUserProfile();
     .then((userInfo: any) => {
                            if (userInfo) {
                             
                            }
                        })
     })

manfredsteyer added a commit that referenced this issue Mar 2, 2020
…ointUrl

#260: Improve error handling on missing tokenEndpoint
@jeroenheijmans
Copy link
Collaborator

Looking at the history and merges I think this issue can be cleaned up now? Let us know, here or in a fresh issue, if you encounter problems still.

@Gaurav-enest
Copy link

@jeroenheijmans Still getting the same issue with
version: "angular-oauth2-oidc": "^9.0.1",

@jeroenheijmans
Copy link
Collaborator

Would you be able to create a fresh StackBlitz or repository with a minimal repro? The original post is old, and a tad hard to read. Also, you may have the same symptom, but could be that the root cause has changed since 2018...

@Gaurav-enest
Copy link

Gaurav-enest commented Mar 18, 2020

@jeroenheijmans Case is same it's working fine in login process .

this.oauthService.loadDiscoveryDocument().then(doc => {
      this.oauthService
        .tryLogin({
          customHashFragment: hash,
          onTokenReceived: context => {
            this.oauthService.loadUserProfile()
              .then((userProfile)=>{
                   console.log(userProfile);
           });
          },
          onLoginError: err => {
            console.log(err);
          }
        })
        .then(() => {
          if (this.hasInValidToken()) {
            this.oauthService.initImplicitFlow();
          }
        });
    });

but when we reload page after login then it's showing error

Cannot read property 'toLowerCase' of null

with below code

 ngOnInit(): void {
   const user = this.oauthService.loadUserProfile();
  }

Follow steps to replicate:

  1. Implement login flow
  2. Then try to get user profile on ngOnInit method you will see this error message.

@jeroenheijmans
Copy link
Collaborator

Hmm, I can imagine a few things that trigger the error with the given code. The most likely culprit is that you might be trying to load the user profile before the other initialization has properly taken place.

I would probably do something like this in the constructor instead of ngOnInit():

    this.oauthService.events
      .pipe(filter(e => ['token_received'].includes(e.type)))
      .subscribe(e => this.oauthService.loadUserProfile());

Or otherwise make sure loadUserProfile is only called in a then chain after you are sure the disco document was loaded and also the tokens are available.

@LeoPote
Copy link

LeoPote commented Apr 15, 2020

Hello,

I am facing quite the same error calling "this.oAuthService.revokeTokenAndLogout()" for logout.

"angular-oauth2-oidc.js:2374 Error revoking token TypeError: Cannot read property 'toLowerCase' of undefined"

Any solution on this?

Tks.

@steven-rothwell
Copy link

I was getting the same error after calling: this.oAuthService.revokeTokenAndLogout(). This was happening when I would log in, make changes to my angular app, then refresh the page and try to log out.

My auth guard was:

if (this.oauthService.hasValidAccessToken()) {
      return true;
    }
    return this.oauthService.loadDiscoveryDocumentAndLogin();

By adding && this.oauthService.discoveryDocumentLoaded to the if statement, it made sure to load the discovery document so that on log out, the null error would not be thrown.

@seyon1988
Copy link

I have used below code and it worked for me

    this.oauthService.loadDiscoveryDocument()
      .then(doc => {{
        console.log(doc);
        this.oauthService.loadUserProfile()
          .then(userprofile => {
            console.log(userprofile)
          })
          .catch();
      }})
      .catch();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

10 participants