Comments (14)
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.
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.
@neroniaky, do you have any solution for this?
from angular-token.
I have the same problem, is there any news?
from angular-token.
@rafaelss95 @chaskas are you using SystemJS?
from angular-token.
No, I'm using webpack.
from angular-token.
@neroniaky Yes I am.
from angular-token.
@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.
@rafaelss95 README, paragraph 3. 😉
from angular-token.
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.
Hi everyone, i'm getting this same issue, has anyone found any solution to this already ? Thnks.
from angular-token.
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.
@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.
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)
- `currentUserData` is not updated after `registerAccount()` HOT 2
- Omniauth from electron app HOT 1
- Create a observable with the logic of userSignedIn() method HOT 2
- Guarding routes with currentUserData Not working after Refresh or Force link. HOT 2
- `signOutFailedValidate` option not working
- How can we add new header key value in http request
- HttpInterceptor Tokens Leak on CORS requests and break CORS. HOT 1
- Error in angular-token.model.d.ts
- Trying to contribute, how do you build a linkable lib? HOT 1
- Oauth callback window won't close after successful login HOT 1
- Auth data not saved when using oAuth HOT 1
- Compatibility With Angular 9 HOT 16
- Email and Password Can't be blank HOT 1
- User is being signed out after page reload
- additionalData not working HOT 1
- signOut() not working on Chrome and Firefox HOT 1
- Compatibility with Angular 11
- UserSignedIn Returns '401 Unauthorized'
- How to install the library having angular 14 HOT 3
- Add Angular 16 support HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from angular-token.