ReferenceError: localStorage is not defined

referenceerror: localstorage is not defined jest
localstorage is not defined vue
referenceerror: sessionstorage is not defined
localstorage is not defined nuxt
localstorage getitem is not a function
react sessionstorage is not defined
localstorage setitem not working
localstorage not working in firefox

I am trying to make User Authentication with Angular and ASP.NET Core.

I am following the tutorial found here:

https://fullstackmark.com/post/10/user-authentication-with-angular-and-asp-net-core

I followed all the steps with no errors.

The app runs, but when I go to "/register" or "/login" routes, I get

NodeInvocationException: Uncaught (in promise): ReferenceError: localStorage is not defined
ReferenceError: localStorage is not defined

the service that uses it is:

import { Injectable, Inject } from '@angular/core';
import { Http, Response, Headers, RequestOptions } from '@angular/http';

import { UserRegistration } from '../shared/models/user.registration.interface';
import { ConfigService } from '../shared/utils/config.service';

import { BaseService } from "./base.service";

import { Observable } from 'rxjs/Rx';
import { BehaviorSubject } from 'rxjs/Rx';

//import * as _ from 'lodash';

// Add the RxJS Observable operators we need in this app.
import '../rxjs-operators';

@Injectable()
export class UserService extends BaseService {

  baseUrl: string = '';

  // Observable navItem source
  private _authNavStatusSource = new BehaviorSubject<boolean>(false);
  // Observable navItem stream
  authNavStatus$ = this._authNavStatusSource.asObservable();

  private loggedIn = false;

  constructor(private http: Http, private configService: ConfigService) {
    super();
    this.loggedIn = !!localStorage.getItem('auth_token');
    // ?? not sure if this the best way to broadcast the status but seems to resolve issue on page refresh where auth status is lost in
    // header component resulting in authed user nav links disappearing despite the fact user is still logged in
    this._authNavStatusSource.next(this.loggedIn);
    this.baseUrl = configService.getApiURI();
  }

    register(email: string, password: string, firstName: string, lastName: string,location: string): Observable<UserRegistration> {
    let body = JSON.stringify({ email, password, firstName, lastName,location });
    let headers = new Headers({ 'Content-Type': 'application/json' });
    let options = new RequestOptions({ headers: headers });

    return this.http.post(this.baseUrl + "/accounts", body, options)
      .map(res => true)
      .catch(this.handleError);
  }  

   login(userName: any, password: any) {
    let headers = new Headers();
    headers.append('Content-Type', 'application/json');

    return this.http
      .post(
      this.baseUrl + '/login',
      JSON.stringify({ userName, password }),{ headers }
      )
      .map(res => res.json())
      .map(res => {
        localStorage.setItem('auth_token', res.auth_token);
        this.loggedIn = true;
        this._authNavStatusSource.next(true);
        return true;
      })
      .catch(this.handleError);
  }

  logout() {
    localStorage.removeItem('auth_token');
    this.loggedIn = false;
    this._authNavStatusSource.next(false);
  }

  isLoggedIn() {
    return this.loggedIn;
  }  
}

the register ts is:

import { Component, OnInit, Inject } from '@angular/core';
import { Router } from '@angular/router';

import { UserRegistration } from '../../shared/models/user.registration.interface';
import { UserService } from "../../services/user.service";

declare var localStorage: any;

@Component({
  selector: 'app-registration-form',
  templateUrl: './registration-form.component.html'
})
export class RegistrationFormComponent implements OnInit {

 errors: string;  
 isRequesting: boolean;
 submitted: boolean = false;

 constructor(private userService: UserService, private router: Router) { 

 }

  ngOnInit() {

  }

  registerUser({ value, valid }: { value: UserRegistration, valid: boolean }) {
     this.submitted = true;
     this.isRequesting = true;
     this.errors='';
     if(valid)
     {
         this.userService.register(value.email,value.password,value.firstName,value.lastName,value.location)
                   .finally(() => this.isRequesting = false)
                   .subscribe(
                     result  => {if(result){
                         this.router.navigate(['/login'],{queryParams: {brandNew: true,email:value.email}});                         
                     }},
                     errors =>  this.errors = errors);
     }      
  }  
}

the login is:

import { Subscription } from 'rxjs';
import { Component, OnInit,OnDestroy } from '@angular/core';
import { Router, ActivatedRoute } from '@angular/router';

import { Credentials } from '../../shared/models/credentials.interface';
import { UserService } from '../../services/user.service';

@Component({
  selector: 'app-login-form',
  templateUrl: './login-form.component.html'
})
export class LoginFormComponent implements OnInit, OnDestroy {

  private subscription: Subscription;

  brandNew: boolean;
  errors: string;
  isRequesting: boolean;
  submitted: boolean = false;
  credentials: Credentials = { email: '', password: '' };

  constructor(private userService: UserService, private router: Router,private activatedRoute: ActivatedRoute) { }

    ngOnInit() {

    // subscribe to router event
    this.subscription = this.activatedRoute.queryParams.subscribe(
      (param: any) => {
         this.brandNew = param['brandNew'];   
         this.credentials.email = param['email'];         
      });      
  }

   ngOnDestroy() {
    // prevent memory leak by unsubscribing
    this.subscription.unsubscribe();
  }

  login({ value, valid }: { value: Credentials, valid: boolean }) {
    this.submitted = true;
    this.isRequesting = true;
    this.errors='';
    if (valid) {
      this.userService.login(value.email, value.password)
        .finally(() => this.isRequesting = false)
        .subscribe(
        result => {         
          if (result) {
             this.router.navigate(['/home']);             
          }
        },
        error => this.errors = error);
    }
  }
}

I tried the solution from:

localStorage is not defined (Angular Universal)

but it does not work...

What am I missing


You can use standard API for localstorage, like

localStorage.setItem(k, v)

and

localStorage.getItem(k)

but If you want a angular solution for this and find a good library for localstorage, one I'm using is this

https://github.com/marcj/angular2-localstorage

also

https://www.npmjs.com/package/angular2-cool-storage

is cool to use.

ReferenceError: localStorage is not defined, You can use standard API for localstorage, like localStorage.setItem(k, v). and localStorage.getItem(k). but If you want a angular solution for this  typeof window !== 'undefined' && # your localStorage interaction is a more simple solution, because the root problem isn't that the window is yet loaded, it's just that gatsby does a lot of optimization when you build, but when it's building and going through your code, it doesn't know what localStorage is. Either way works though.


The standard localStorage API should be available.

No need to declare it.

to set localStorage.setItem('name', 'storingSomething');

to get localStorage.getItem('name');

ReferenceError: localStorage in not defined - Support, This chunk of js is killing my build: data: () => ({ showMessage: !localStorage.​getItem("hideMessage"), }), methods: { dismissMessage:  Attempting to use Webworkers with a LocalStorage Database. Works fine without Webworkers. Build no: v0.2.3-develop-1198 alasql.js:16161 Uncaught ReferenceError: localStorage is not defined LS.get @ alasql.js:16161 LS.createDatabase @ ala


Put localStorage.getItem(...) on `ComponentDidMount()``

Reference Error : localStorage is not defined : reactjs, Reference Error : localStorage is not defined. Why this error ? Can anyone In that case, you should mock local storage on global object. level 2. MYTHiN07. Handle localStorage if not defined Handle localStorage if not defined ^ ReferenceError: localStorage is not defined at Object.<anonymous> (/Users/whatevs


LocalStorage Not defined - Bug Reports, This happens in preview and in the iOS Simulator. [Viewport]: Error: ReferenceError: localStorage is not defined: Name: ReferenceError:  Dismiss Join GitHub today. GitHub is home to over 50 million developers working together to host and review code, manage projects, and build software together.


Error in mounted hook: "ReferenceError: localStorage is not defined , Error in mounted hook: "ReferenceError: localStorage is not defined" solution. Let me talk about the background of the problem. First of all, Vue is definitely not  localStorage.getItem && (window.storage = localStorage);} catch (exception) { } So once this snippet is executed, storage is either undefined — if either 1) localStorage doesn't exist, 2) localStorage.getItem is falsy, 3) localStorage.getItem throws — or it references same object as localStorage (if execution got to the assignment part).


localStorage is not defined (Angular Universal) - angular - html, EXCEPTION: ReferenceError: localStorage is not defined I got the idea from ng​-conf-2016-universal-patterns that using Dependency Injection to solve this. I'm trying to use localstorage in javascript to save data between executions