Hey there, looking for a basic auth authentication service in angular2?
Here's the issue I'm facing: When a user logs in for the first time, everything works smoothly. However, if they try to log in with a different account for the second time, I notice that there are two basic auth strings in the request headers. It looks something like this: "Authorization:Basic YWRtaW46YWJjMTIz,Basic RMcasd9WJjMXoPj".
Let me show you the code snippet:
@Injectable()
export class AuthenticationService {
private url: string = 'http://localhost:8080/api/test';
private username: string;
private password: string;
private authenticationStatus: boolean = false;
constructor(private http: Http) { }
authentication(username: string, password: string): Promise<boolean> {
let headers = new Headers(); // <== previous headers object with old Authorization string get back from grave.
console.log(headers);
headers.append("Authorization", "Basic " + btoa(username + ":" + password));
headers.append("Content-Type","application/json;charset=utf-8");
return this.http.get(this.url, { headers: headers })
.toPromise()
.then(response => { ...
This is my initial attempt at an Angular/Typescript app. I'm puzzled by not getting a fresh object when using both let and new here. Could it be due to the fact that headersString within the header class is static? I've delved into the Angular headers class API documentation but haven't managed to resolve it yet. I even tried calling headers.delete("Authorization");
immediately after let headers = new Headers();
, but the old Authorization header persists.