I'm trying to get Angular2 Token Auth module to work but having a continuous '401' error on first load.
I have gotten the auth/registration/login to work, however on a fresh reload -every- time someone is not logged in, they get a 401 Error.
Watching the Network log I see that for some reason it makes 2 validate_token
calls, but no token should exist. The first one succeeds with a 200
response - But the 2nd fails with a 401
. I've compared both and the only difference I see is that the 2nd doesn't have content-type
header.
My entire service is pretty generic
import { Injectable } from '@angular/core';
import {Angular2TokenService} from "angular2-token";
import {Subject, Observable} from "rxjs";
import {Response} from "@angular/http";
@Injectable()
export class AuthService {
userSignedIn$:Subject<boolean> = new Subject();
constructor(public authService:Angular2TokenService) {
this.authService.validateToken().subscribe(
res => res.status == 200 ? this.userSignedIn$.next(res.json().success) : this.userSignedIn$.next(false)
)
}
logOutUser():Observable<Response>{
return this.authService.signOut().map(
res => {
this.userSignedIn$.next(false);
return res;
}
);
}
registerUser(signUpData: {email:string, password:string, passwordConfirmation:string}):Observable<Response>{
return this.authService.registerAccount(signUpData).map(
res => {
this.userSignedIn$.next(true);
return res
}
);
}
logInUser(signInData: {email:string, password:string}):Observable<Response>{
return this.authService.signIn(signInData).map(
res => {
this.userSignedIn$.next(true);
return res
}
);
}
}
I believe there is something in Angular2-Token that I do not know about that is causing this but I'm unsure. I've checked the back-end and it has no record of any request coming through.
Failed Request
GET /auth/validate_token HTTP/1.1 Host: baseUrl Connection: keep-alive Pragma: no-cache Cache-Control: no-cache Accept: application/json Origin: http://localhost:4200 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.90 Safari/537.36 Content-Type: application/json Referer: http://localhost:4200/ Accept-Encoding: gzip, deflate, br Accept-Language: en-US,en;q=0.8
Successful Request OPTIONS /auth/validate_token HTTP/1.1 Host: baseUrl Connection: keep-alive Pragma: no-cache Cache-Control: no-cache Access-Control-Request-Method: GET Origin: http://localhost:4200 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.90 Safari/537.36 Access-Control-Request-Headers: content-type Accept: / Referer: http://localhost:4200/ Accept-Encoding: gzip, deflate, br Accept-Language: en-US,en;q=0.8
It seems like the failed one is making a GET request, thus causing the issue but I have no idea what is invoking this call. Is there a way to see?