Coder Social home page Coder Social logo

Comments (14)

neroniaky avatar neroniaky commented on May 21, 2024

If something like this happens its usually an timing issue. The CanActivate method checks if AuthData is set.

userSignedIn(): boolean {
        return !!this._currentAuthData;
}

In your second example your console.log() methods are outside the subscribe method, so they get run before .signIn() finishes.

console.log(this.tokenService.userSignedIn()); // false
console.log(this.tokenService.validateToken()); // 401

You should check where you redirect and if it triggers before the backend has a chance to answer your signIn call.

from angular-token.

rafaelss95 avatar rafaelss95 commented on May 21, 2024

Well, so how can I solve this timing issue?

In fact, this 2nd example is just to check why the canActivate isn't working... I also tested this inside the subscribe method and it gives the same response.

Thanks for your quick response.

from angular-token.

rafaelss95 avatar rafaelss95 commented on May 21, 2024

@neroniaky, do you have any solution for this?

from angular-token.

chaskas avatar chaskas commented on May 21, 2024

I have the same problem, is there any news?

from angular-token.

neroniaky avatar neroniaky commented on May 21, 2024

@rafaelss95 @chaskas are you using SystemJS?

from angular-token.

rafaelss95 avatar rafaelss95 commented on May 21, 2024

No, I'm using webpack.

from angular-token.

chaskas avatar chaskas commented on May 21, 2024

@neroniaky Yes I am.

from angular-token.

rafaelss95 avatar rafaelss95 commented on May 21, 2024

@neroniaky, can you share the code of the demo site (https://angular2-token.herokuapp.com)? I didn't found here in this repo.

from angular-token.

neroniaky avatar neroniaky commented on May 21, 2024

@rafaelss95 README, paragraph 3. 😉

from angular-token.

tomfloresa avatar tomfloresa commented on May 21, 2024

Hi! Is someone still having this issue? I'm working on a RoR BackEnd and it is generating the token. Angular (or A2T) is also setting it in the local storage as I'm capable of seeing it in the Application tab on DevTools. Thing is that userSignedIn always returns false. Thanks!

from angular-token.

jefree avatar jefree commented on May 21, 2024

Hi everyone, i'm getting this same issue, has anyone found any solution to this already ? Thnks.

from angular-token.

jefree avatar jefree commented on May 21, 2024

For me, the solution was to expose the headers as suggested here, due to i'm running the backend and front as separated apps.

from angular-token.

saransh944 avatar saransh944 commented on May 21, 2024

@rafaelss95 use .map instead of using subscribe and return it, something like this:

return this.tokenService.signIn({
email: '[email protected]',
password: 'secretPassword'
}).map(data => {
console.log(data);
// your if conditions inside this
});

from angular-token.

matheuschvs avatar matheuschvs commented on May 21, 2024

In case anyone is struggling with that like me

I noticed that when calling the sign_in method the headers were not being returned and that's why I couldn't authenticate the user.

what solved for me was to expose the headers at the back-end.
something like that:

Rails.application.config.middleware.insert_before 0, Rack::Cors do
  allow do
    origins '*'

    resource '*',
      headers: :any,
      expose: ['access-token', 'expiry', 'token-type', 'uid', 'client'],
      methods: [:get, :post, :put, :patch, :delete, :options, :head]
  end
end

from angular-token.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.