nebular icon indicating copy to clipboard operation
nebular copied to clipboard

Token is empty or invalid

Open dmaglio opened this issue 3 years ago • 5 comments
trafficstars

Issue type

I'm submitting a ... (check one with "x")

  • [X] bug report
  • [ ] feature request

Issue description

After logging in regularly, I can no longer log in because the response message is token is empty or invalid. it does not happen on all computers or browsers but randomly on different PCs and different operating systems. Yet the authentication response holds the correct token.

This is the angular code I use for authentication

authenticate(data?: any): Observable<NbAuthResult> {
    const module = 'login';
    const method = this.getOption(`${module}.method`);
    const url = this.getActionEndpoint(module);
    const requireValidToken = this.getOption(`${module}.requireValidToken`);
    return this.http.request(method, url, {body: data, observe: 'response'})
      .pipe(
        map((res) => {
          if (this.getOption(`${module}.alwaysFail`)) {
            throw this.createFailResponse(data);
          }
          return res;
        }),
        map((res) => {
          console.log(res);
          return new NbAuthResult(
            true,
            res,
            this.getOption(`${module}.redirect.success`),
            [],
            this.getOption('messages.getter')(module, res, this.options),
            this.createToken(this.getOption('token.getter')(module, res, this.options), requireValidToken));
        }),
        catchError((res) => {
          return this.handleResponseError(res, module);
        }),
      );
  }

instead the json result of request is

{
    "headers": {
        "normalizedNames": {},
        "lazyUpdate": null
    },
    "status": 200,
    "statusText": "OK",
    "url": "https://api.XXXXXX.XXX/auth/login",
    "ok": true,
    "type": 4,
    "body": {
        "token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJhIGQiLCJyb2xlcyI6WyJhZG1pbiJdLCJleHAiOjE2Mzg0MzkwOTIsImlhdCI6MTYzODQzOTA2MiwidXNlcm5hbWUiOiJhZG1pbiJ9.0AGL6Ztu3hgaAnNdZiMlK9EBGjp4aj8mFtrM7R0TYoM",
        "refresh_token": "test"
    }
}

json of error:

{
    "success": false,
    "response": {},
    "redirect": null,
    "errors": [
        "Token is empty or invalid."
    ],
    "messages": [
        null
    ],
    "token": null
}

dmaglio avatar Dec 02 '21 10:12 dmaglio

Same behavior, same issue. Do you have any tips/solutions for this?

FYI

"@nebular/auth": "7.0.0",
"@nebular/date-fns": "7.0.0",
"@nebular/eva-icons": "7.0.0",
"@nebular/theme": "7.0.0",

kapik avatar Mar 03 '22 09:03 kapik

you need configure the token key

https://akveo.github.io/nebular/docs/auth/getting-user-token#use-token

.... NbAuthModule.forRoot({ strategies: [ NbPasswordAuthStrategy.setup({ name: 'email',

         token: {
           class: NbAuthJWTToken,
          
           key: 'token', // this parameter tells where to look for the token
         }
       }),
     ],
     forms: {},
   }), 
   ...

dev-alexandre avatar Mar 03 '22 10:03 dev-alexandre

Of course @dev-alexandre , I have already done this since this behavior is random like @dmaglio

kapik avatar Mar 03 '22 13:03 kapik

I am having this same issue:

"@nebular/auth": "^9.0.0",
"@nebular/eva-icons": "^9.0.0",
"@nebular/security": "^9.0.0",
"@nebular/theme": "^9.0.0",

ghost avatar Oct 28 '22 09:10 ghost

I had the same issue, I then realized that my laptop did not have the date and time set automatically and that the date was set to the next day. Fixing the laptop date solved that issue for me... maybe check if that is your problem.

lpennec avatar Apr 06 '23 19:04 lpennec