question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

localStorage not set when I set apiBase upon _tokenService.init()

See original GitHub issue

I 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:closed
  • Created 6 years ago
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
tomuenchcommented, Dec 7, 2017

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.

0reactions
raysuelzercommented, Oct 6, 2017

This was an error my end. I had two instances running.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why is Angular2 token localStorage not set when I set ...
I have a rails BE application on localhost:3000 and FE on localhost:4200. I am trying to get the token validation to be done...
Read more >
[Feature] API for changing localStorage · Issue #6258 - GitHub
Just to confirm, after you use evaluate to set values in the local storage, does context.storageState() return you the values?
Read more >
Window.localStorage - Web APIs | MDN
The localStorage read-only property of the window interface allows you to access a Storage object for the Document's origin; the stored data ...
Read more >
TIL: Mocking localStorage and sessionStorage in Angular Unit ...
Let's say we have a service in an Angular app: TokenService which reads/writes a token into localStorage or sessionStorage on the browser.
Read more >
How to Persist React State in Local Storage - Felix Gerschau
Learn how to persist the data of your React application, using local storage.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found