Nebular: Access token is not stored after login

Created on 3 Nov 2017  路  10Comments  路  Source: akveo/nebular

Hi,
I make it docs/articles/auth-guard.md.
At app-routing.module.ts

{
path: 'pages',
canActivate: [AuthGuard], //add line
loadChildren: 'app/pages/pages.module#PagesModule' },
}
...
Result cosole.log(result); //line 53 in login.component.js
NbAuthResult {success: true, response: HttpResponse, redirect: "/", errors: Array(1), messages: Array(1), 鈥

However, page not nagivator to "pages/dashboard". If i hidden line canActivate then I can access dashboard but deafault load login.
Where is error?
Pleases help me!
(My english is not so good).

enhancement auth docs

Most helpful comment

Mine is in src\app\@core\core.module.ts (using ngx-admin).
There you can change the token key as follows:

const NB_CORE_PROVIDERS = [
  ...DataModule.forRoot().providers,
  ...NbAuthModule.forRoot({
    providers: {
      email: {
        service: NbEmailPassAuthProvider,
        config: {
          baseEndpoint: '/api',
          token: {
            key: 'token',
          },
          ...

All 10 comments

Hello @aloxiao, we would need much more details on this, are you using ngx-admin? Which provider are you using? Could you provide a reproducible example?

Dear @nnixaa ,
I am begin Github and Angular 4, i can not upload Github.
I give link my project (Note: i am not user email login, i am use account( number : etc 216456)
https://drive.google.com/file/d/1xJ1X7LTd6wu_g15emuzxqHQjG5OgIkIj/view?usp=sharing

I use ngx-admin, i clone ngx-admin 5 day ago when new update Auth (link https://github.com/akveo/nebular/blob/master/docs/articles/auth-concepts-install.md) .

Thanks you for your reply.

Same issue here. My token is in the header of the response from the server - do I need to change the token config from this:

token: {
key: 'data.token',
getter: (module: string, res: HttpResponse) => getDeepFromObject(res.body,
this.getConfigValue('token.key')),
}

to something else? I've tried debugging it through but can't work out what I need to change to specifically tell it where to find the token.

I'm now returning the token as json in the content of the response, i.e. 'token' : 'hsgfhgdhjfgdshfgdfgds', it
still says login succesful inititally but doesn't authenticate so auth guard won't allow a redirect.

You need to have your response structured as such { "data": { "token": "token_value" } }. You can also try to change the token.key from 'data.token' to just 'token' in your auth config.

-Mustafa

Dear @janabimustafa,

Where is config? Thanks!

Mine is in src\app\@core\core.module.ts (using ngx-admin).
There you can change the token key as follows:

const NB_CORE_PROVIDERS = [
  ...DataModule.forRoot().providers,
  ...NbAuthModule.forRoot({
    providers: {
      email: {
        service: NbEmailPassAuthProvider,
        config: {
          baseEndpoint: '/api',
          token: {
            key: 'token',
          },
          ...

@janabimustafa you beat me to it. I had located this yesterday. Basically if you follow the new auth guide you just need to update the token key in the provider that you created. So this should be a file that you created in your instance and added to the module.

I ended up hacking the authenticate method in email-pass-auth provider to get it out of the body, so it doesn't need to use getConfigValue() for the token. Will have a look at refactoring it properly at some point! Thanks for the help.

I also have problem with this.

Token comes from response.body.token, and cant access it

Was this page helpful?
0 / 5 - 0 ratings