'localStorage' is undefined error. #1050

Closed
opened 2025-08-09 17:18:41 +00:00 by fergalmoran · 0 comments
Owner

Originally created by @digital-nomad17 on 2/7/2017

I am having an issue where I am unable to compile typescript code that references the browser api. Namely localStorage. The project uses Angular 2 and typescript 2.1. I tried setting a type declaration at the beginning of the file but I am still getting the error. https://i.imgur.com/U3wiDmA.png
Here's the code:

import { Injectable, EventEmitter } from "@angular/core";
import { Http, Headers, Response, RequestOptions } from "@angular/http";
import { Observable } from "rxjs/Observable";
import { AuthHttp } from "./auth.http";


@Injectable()
export class AuthService {
    authKey = "auth";

    constructor(private http: AuthHttp) {
    }

    login(username: string, password: string): any {
        var url = "api/connect/token";  // JwtProvider's LoginPath

        var data = {
            username: username,
            password: password,
            client_id: "OpenGameList",
            // required when signing up with username/password
            grant_type: "password",
            // space-separated list of scopes for which the token is issued
            scope: "offline_access profile email"
        };

        return this.http.post(
            url,
            this.toUrlEncodedString(data),
            new RequestOptions({
                headers: new Headers({
                    "Content-Type": "application/x-www-form-urlencoded"
                })
            }))
            .map(response => {
                var auth = response.json();
                console.log("The following auth JSON object has been received:");
                console.log(auth);
                this.setAuth(auth);
                return auth;
            });
    }

    logout(): boolean {
        this.setAuth(null);
        return true;
    }

    // Converts a Json object to urlencoded format
    toUrlEncodedString(data: any) {
        var body = "";
        for (var key in data) {
            if (body.length) {
                body += "&";
            }
            body += key + "=";
            body += encodeURIComponent(data[key]);
        }
        return body;
    }

    // Persist auth into localStorage or removes it if a NULL argument is given
    setAuth(auth: any): boolean {
        if (auth) {
            localStorage.setItem(this.authKey, JSON.stringify(auth));
        }
        else {
            localStorage.removeItem(this.authKey);
        }
        return true;
    }

    // Retrieves the auth JSON object (or NULL if none)
    getAuth(): any {
        var i = localStorage.getItem(this.authKey);
        if (i) {
            return JSON.parse(i);
        }
        else {
            return null;
        }
    }

    // Returns TRUE if the user is logged in, FALSE otherwise.
    isLoggedIn(): boolean {
        return localStorage.getItem(this.authKey) != null;
    }
}

I am using aspnetcore-spa 0.8.0. Any ideas?

*Originally created by @digital-nomad17 on 2/7/2017* I am having an issue where I am unable to compile typescript code that references the browser api. Namely localStorage. The project uses Angular 2 and typescript 2.1. I tried setting a type declaration at the beginning of the file but I am still getting the error. https://i.imgur.com/U3wiDmA.png Here's the code: ``` import { Injectable, EventEmitter } from "@angular/core"; import { Http, Headers, Response, RequestOptions } from "@angular/http"; import { Observable } from "rxjs/Observable"; import { AuthHttp } from "./auth.http"; @Injectable() export class AuthService { authKey = "auth"; constructor(private http: AuthHttp) { } login(username: string, password: string): any { var url = "api/connect/token"; // JwtProvider's LoginPath var data = { username: username, password: password, client_id: "OpenGameList", // required when signing up with username/password grant_type: "password", // space-separated list of scopes for which the token is issued scope: "offline_access profile email" }; return this.http.post( url, this.toUrlEncodedString(data), new RequestOptions({ headers: new Headers({ "Content-Type": "application/x-www-form-urlencoded" }) })) .map(response => { var auth = response.json(); console.log("The following auth JSON object has been received:"); console.log(auth); this.setAuth(auth); return auth; }); } logout(): boolean { this.setAuth(null); return true; } // Converts a Json object to urlencoded format toUrlEncodedString(data: any) { var body = ""; for (var key in data) { if (body.length) { body += "&"; } body += key + "="; body += encodeURIComponent(data[key]); } return body; } // Persist auth into localStorage or removes it if a NULL argument is given setAuth(auth: any): boolean { if (auth) { localStorage.setItem(this.authKey, JSON.stringify(auth)); } else { localStorage.removeItem(this.authKey); } return true; } // Retrieves the auth JSON object (or NULL if none) getAuth(): any { var i = localStorage.getItem(this.authKey); if (i) { return JSON.parse(i); } else { return null; } } // Returns TRUE if the user is logged in, FALSE otherwise. isLoggedIn(): boolean { return localStorage.getItem(this.authKey) != null; } } ``` I am using aspnetcore-spa 0.8.0. Any ideas?
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github/JavaScriptServices#1050
No description provided.