localStorage not set when I set apiBase upon _tokenService.init()
See original GitHub issueI have a rails BE application on localhost:3000 and FE on localhost:4200. I am trying to get the token validation to be done automatically and I understand this happens through localStorage being set and retrieved during the _tokenService.validateToken() method. The problem seems to be that once I sign in (successfully), I looked at my localStorage in my browser and found nothing saved. Hence, when I refresh the page, there is no localStorage to validate as a part of my validateToken() method and hence, the session is not maintained on the browser. What am I missing here? Is there a mistake in the way I have initialised this command? Do I need to manually set the localStorage everytime? If so, how?
I tried a simple app where my app.component.ts has the following
import { Component, ViewEncapsulation, OnInit } from '@angular/core';
import { Angular2TokenService } from 'angular2-token';
@Component
....
export class AppComponent {
constructor(private _tokenService: Angular2TokenService) {
this._tokenService.init({apiBase:'http://localhost:3000'});
}
}
My Sign-in (sign-in.component.ts)
import { Component, OnInit } from '@angular/core';
import { Angular2TokenService, SignInData } from 'angular2-token';
@Component({
selector: 'sign-in',
templateUrl: 'sign-in.component.html'
})
export class SignInComponent {
private _signInData: SignInData = <SignInData>{};
private _output: any;
constructor(private _tokenService: Angular2TokenService) { }
// Submit Data to Backend
onSubmit() {
this._output = null;
this._tokenService.signIn(this._signInData).subscribe(
res => {
this._signInData = <SignInData>{};
this._output = res;
}, error => {
this._signInData = <SignInData>{};
this._output = error;
}
);
}
}
Issue Analytics
- State:
- Created 6 years ago
- Comments:6 (1 by maintainers)
Top GitHub Comments
i had the same problem with a rails api. I couldn’t run my app with a proxy, so i have digged a bit deeper.
My Solution was, to allow the used headers explicit. Example:
resource '*', headers: :any, methods: %i[get post put patch delete options head], :expose => ['access-token', 'client', 'expiry','token-type', 'uid']
Hope this helpes somebody in the future.
This was an error my end. I had two instances running.