How to handle User Session in angular 5

Session management will happen automatically.

If we have client on one server and backend on another server at that time we just need to add 'proxy.conf.json' file. And need to add that file entry in 'package.json' file.

Important:- Add server url upto the port number in 'proxy.conf.json'. (Server url entry till port number)

{
    "/": {
      "target": "https://localhost:30443",
      "secure": false
    }
 }

In my case, I was using one extra parameter /mainlayer in server URL. (Which was wrong.)

{
   "/": {
     "target": "https://localhost:30443/mainlayer",
     "secure": false
   }
}

And in LoginService class just add that extra parameter.

private _url:string = "mainlayer/v1/login";

You need to check "jsessionid" in session storage before sending auth reques.

login(){
 const data = {userName:"root", password:"root"};
 if(this.isLoggedIn()){
  return Observable.of(JSON.parse(sessionStorage.getItem('jsessionid')))
 }

 return this.http.post(this._url,{headers: this.headers},{params : data});
}

private isLoggedIn(): boolean {
 const result = !!(sessionStorage.getItem('jsessionid'));
 return result;
}

Also you need to create auth interceptor https://medium.com/@ryanchenkie_40935/angular-authentication-using-the-http-client-and-http-interceptors-2f9d1540eb8

Tags:

Angular