Skip to content
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

tokenNotExpired and AuthConfig #11

Closed
ghost opened this issue Jan 6, 2016 · 15 comments
Closed

tokenNotExpired and AuthConfig #11

ghost opened this issue Jan 6, 2016 · 15 comments
Milestone

Comments

@ghost
Copy link

ghost commented Jan 6, 2016

Hi,

I am using angular2-jwt for my first angular 2 app.
I find it quite easy to use, which is nice :)

I have a simple question, and it's about the tokenNotExpired function, as well as the AuthConfig.

Is it possible for the tokenNotExpired function to get the tokenName from the AuthConfig (as well as the getter function?) ?

Injected authconfig into my component did not make it available in the @CanActivate annotation.

I might as well just rename my token to the default id_token, but I think it would be nice to get the same config everywhere.

I have no idea if this is a simple task or not, as I am just starting with Angular 2 (without any Angular 1 background).

Anyway, thanks for this little plugin,

Cheers!

@chenkie
Copy link
Contributor

chenkie commented Jan 6, 2016

Hey! Are you trying to specify another token name for tokenNotExpired to use? If, so you can pass the name of your token in when calling it.

@CanActivate(() => tokenNotExpired('my_token_name'))

Let me know if that's what you're looking for :)

@ghost
Copy link
Author

ghost commented Jan 6, 2016

Hi,

Thanks for the fast reply!

That's what I am trying to achieve but I was wondering if it was possible to do it without specifying the name in the function. Just to keep it DRY.

I tried to create an AuthVerificationService that would look like the following :

import {Injectable} from 'angular2/core';
import {AuthConfig, tokenNotExpired} from 'angular2-jwt';

@Injectable()
export class AuthVerificationService {
  constructor(private authConfig:AuthConfig) {  }

  tokenVerified():boolean {
    var token = this.authConfig.tokenGetter();

    return tokenNotExpired(null, token);
  }
}

and have it injected in my "private" components, but I don't know how to do the following part :

import {Component} from 'angular2/core';
import {CanActivate} from 'angular2/router';

import {AuthVerificationService} from '../services/auth-verification.service';

@Component( ... )
@CanActivate(() => AuthVerificationService.tokenVerified()) // Error, "static" method not good...
export class DashboardComponent { }

I might as well just keep the default name and getter...

If you have any idea to do it though I'll be happy to read about it =D

Thanks again.

Cheers!

@chenkie
Copy link
Contributor

chenkie commented Jan 11, 2016

This is actually probably something that tokenNotExpired should be getting from AuthConfig when the app is bootstrapped, but it isn't currently set up for that. I'll put in a fix for the next release.

@RicardoRFaria
Copy link

Need PR?

@h0st1le
Copy link

h0st1le commented Mar 12, 2016

Until this is integrated it would be great if it was mentioned in the docs. I spent a couple hours trying to figure out why tokenNotExpired returned false and jwtHelper.isTokenExpired also returned false, when using an AuthConfig defining another name for the tokenName

@chenkie
Copy link
Contributor

chenkie commented Mar 12, 2016

@RicardoRFaria feel free to submit a PR if you like :)

@h0st1le good call--I'll add that, thanks.

@v3rron
Copy link

v3rron commented Jun 16, 2016

any progress on this?

@siarheipashkevich
Copy link

Hello, any updates on this?

@ghost
Copy link
Author

ghost commented Apr 17, 2017

Hello, help anyone!
tokenNotExpired is not working properly!
While login it matches username and password from database and also it generated token but after login page doesn't redirect to profile page. I had uploaded to cloud9.
But in localhost work fine.

tokenNotExpired return false

@kksrini89
Copy link

kksrini89 commented Jun 20, 2017

@denishp83 Here also the same!

@chenkie After successful login, tokenNotExpired always returns false. So it doesn't redirect the secured page.

Any Idea pls?

NOTE: I manually passed stored localstorage token name to tokenNotExpired method. It resolves the problem but I don't think this is the right way!

@chenkie
Copy link
Contributor

chenkie commented Jun 20, 2017

@kksrini89 have you double checked that the correct token is used by tokenNotExpired?

@kksrini89
Copy link

@chenkie Yes I did. But it showed me tokenNotExpired is not defined even though I have given reference using import { tokenNotExpired } from 'angular2-jwt';

@chenkie
Copy link
Contributor

chenkie commented Jun 20, 2017

Ok if it's not defined then that's likely another problem. Probably best to open a separate issue for it.

@kksrini89
Copy link

sure. Let me create a new ticket for this! thanks @chenkie

@kksrini89
Copy link

kksrini89 commented Jun 20, 2017

@chenkie created new ticket #369

@chenkie chenkie closed this as completed May 10, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

7 participants