Need to set dynamic Auth token in proxy.conf.js

236 views
Skip to first unread message

Babar Shahzad

unread,
Aug 23, 2023, 3:49:01 PM8/23/23
to Angular and AngularJS discussion
I have problem , I implimented the proxy in my Angular 15.0 project, but want to add auth token in it with dynamic way , I have also did a work around, but not able to succeed All files are located src level at ng serve time , project giveing me thr error : An unhandled exception occurred: Cannot find module './config'

this error located in proxy.conf.js line :  const { authConfig } = require('./config');

Here is my
This is the session service:
import { Injectable } from '@angular/core';

@Injectable({
  providedIn: 'root'
})
export class SessionStorageService {
  setItem(key: string, value: any) {
    sessionStorage.setItem(key, JSON.stringify(value?.access_token));
  }

  getItem(key?: string): any {
    const item = sessionStorage.getItem(key);
    return item ? JSON.parse(item) : null;
  }
}

and this the congif file:

import { SessionStorageService } from './session.service';

const authConfigService = new SessionStorageService();
const authToken = authConfigService.getItem();
console.log(authToken);
export const authConfig = {
  authToken: authToken
};
and finally here is proxy.conf.js
const { authConfig } = require('./config');

var defaultTarget = 'https://example.com/';
module.exports = [
  {
    context: ['/v1/**'],
    target: defaultTarget,
    changeOrigin: true,
    headers: { "Authorization": `Bearer ${authConfig.authToken}` }
  }
];

Reply all
Reply to author
Forward
0 new messages