Is it possible to access localstorage from Angular proxy.config.js file?

Asif Karim Bherani

I am trying to use proxy to update the base url and also append the token from the local storage. I am able to update the URL but when I try to access the token it gives me error ReferenceError: window is not defined. Is it possible to access local storage from proxy. I tried localstorage.getItem('token') and also windows.localstorage.getItem('token').

My proxy.conf.js file looks like this

const PROXY_CONF = [
    {
        context: ['/testData/*'],
        target: 'https://test.com/',
        secure: false,
        changeOrigin: true,
        bypass: function (req, res, proxyOptions) {
            req.headers['authorization'] = typeof window !== 'undefined' ? window.localStorage.getItem(`token`) : '';
        },
    }
];
module.exports = PROXY_CONF;

José Epifanio

Proxy.config.js is a configuration used by webpack internally. It doesn't have any knowledge about the browser. This server is ONLY used for development purposes and you won't deploy/use this guy in an actual prod environment.

If you want to add a bearer token (or any other kind of tokens) to your requests you'll have to create an interceptor. Here's a link to a tutorial

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

localStorage access from local file

Is it possible to access LocalStorage from App Script?

config proxy in angular cli

Is it possible to encrypt a config file specified as a configSource from web.config?

Unable to access Associative from config file

Python access config file from every module

Hydra: access name of config file from code

How to access js file in Angular

Angular Js - LocalStorage

Angular proxy config to change url

What is the correct public access config for s3 bucket to access from lambda with http proxy integration?

Access LocalStorage value in ejs file

Access localStorage from service worker

Access JSON file in Angular.js

Access Angular module defined in a different .js file

How to get access from external config file to classpath in executing jar

Is It Possible to scene access from one file to another file in ROKU?

Access runtime env vars from next.config.js

Node.js express: access application config from required modules

What is the use system.config.js file in angular 2?

config.js file in angular2 for the ckeditor?

Global file access from anywhere in angular 4

Access url parameter from angular js

How to access Angular Js data from nodejs

Is it possible to pipe config file to hostapd?

Name in "namespace" in a config file, is it possible?

Is it possible to access a Handlebar variable through an external .js file?

Angular migration (from 4 to 6) e2e --proxy-config not working

Is it possible to add libraries from Node.JS and some libraries from angular.js and also from react.js in one javascript file?