Ngx-admin: Redirecting after authentication. Not a valid JWT token.

Created on 28 Dec 2017  路  4Comments  路  Source: akveo/ngx-admin

I'm running into this problem of redirecting to dashboard page after authenticating. I have backend service that returns the auth token like this.

{
...
session_id: "6NdC3aMn3Smc5fuRCMvJ" 
...
}

and my token is set like this

token :{
            key : 'session_id',
}

I see auth_app_token getting set in localStorage. But when it tries to redirect I see the error below

"Error: The token 6NdC3aMn3Smc5fuRCMvJ is not valid JWT token and must consist of three parts"

If I change my service response to something like this

data: {
 session_id: 
}

and configure

token {
key : "data.session_id"
}

I don't see the aut_app_token getting set.

What am I doing wrong here.

Thanks!

question

Most helpful comment

I think your issue here is that your backend is not using a JWT Token. You can have a look at JWT (JSON Web Token) tokens and what they are at jwt.io. The Nebular auth service and components are ready to work with regular tokens NbAuthSimpleToken or with JWT tokens NbAuthJWTToken.
If your token is not JWT, you need to make sure that you always treat it like a NbAuthSimpleToken. The getPayload() function is specific for the NbAUthJWTToken class, and that's why your app crashes on that point. You can have a look at the regular token class here: https://github.com/akveo/nebular/blob/893d56adfe56a0d4f6e8fe9b9591a20e3bafade4/src/framework/auth/services/token.service.ts#L16

Hope that solved your issue.

All 4 comments

So, If I comment out the section of this code, my redirection works just fine. Perhaps, I need to change the getter setting

 constructor(private sidebarService: NbSidebarService,
              private menuService: NbMenuService,
              private userService: UserService,
              private analyticsService: AnalyticsService, private authService: NbAuthService) {

                this.authService.onTokenChange()
                .subscribe((token: NbAuthJWTToken) => {

                  /*if (token.getValue()) {
                    this.user = token.getPayload(); 
                  }*/

                });
  }

I think your issue here is that your backend is not using a JWT Token. You can have a look at JWT (JSON Web Token) tokens and what they are at jwt.io. The Nebular auth service and components are ready to work with regular tokens NbAuthSimpleToken or with JWT tokens NbAuthJWTToken.
If your token is not JWT, you need to make sure that you always treat it like a NbAuthSimpleToken. The getPayload() function is specific for the NbAUthJWTToken class, and that's why your app crashes on that point. You can have a look at the regular token class here: https://github.com/akveo/nebular/blob/893d56adfe56a0d4f6e8fe9b9591a20e3bafade4/src/framework/auth/services/token.service.ts#L16

Hope that solved your issue.

Hi @120bits, you just don't need the JWTToken as your token is clearly not JWT.
Skip the step one of the guide (https://akveo.github.io/nebular/#/docs/auth/getting-user-token) and use NbAuthSimpleToken as a type .subscribe((token: NbAuthSimpleToken).

Thanks guys! that helped!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

xandatspain picture xandatspain  路  3Comments

maxlein picture maxlein  路  3Comments

queirozfcom picture queirozfcom  路  4Comments

argnist picture argnist  路  4Comments

mignam picture mignam  路  3Comments