Trying to set up a login feature, I decided to fetch user information using a GET request and store it in local storage. However, I am encountering an issue where the code never reaches the point where I call setItem.
AuthenticationService.ts
export class AuthenticationService {
constructor(private http: HttpClient) {}
login(username, password) {
const httpOptions = {
headers: new HttpHeaders({
'Content-Type': 'application/json',
Authorization: 'Basic ' + btoa('' + username + ':' + password + ''),
}),
};
this.http.get('api/user/loginInfo', httpOptions).subscribe((res) => {
localStorage.setItem('currentUser', JSON.stringify(res));
});
}
}
login.component.ts
export class LoginComponent implements OnInit {
constructor(private authService: AuthenticationService) {}
ngOnInit(): void {}
login() {
const username = (document.getElementById('username') as HTMLInputElement)
.value;
const password = (document.getElementById('password') as HTMLInputElement)
.value;
this.authService.login(username, password);
}
}