adds working remember function

This commit is contained in:
Marcus Ferl 2024-03-05 22:09:08 +01:00
parent 524de9bd98
commit db4293a1f0
2 changed files with 8 additions and 14 deletions

View File

@ -51,9 +51,7 @@ export class LoginFormComponent {
}
rememberMeChanged = (e: any) => {
this.remember_me = e.value;
console.log(this.remember_me)
}
onCreateAccountClick = () => {
this.router.navigate(['/create-account']);

View File

@ -43,7 +43,7 @@ export class AuthService {
avatarUrl: "https://js.devexpress.com/Demos/WidgetsGallery/JSDemos/images/employees/07.png"
};
localStorage.setItem('access_token', result.token);
// localStorage.setItem('user_data', JSON.stringify(this.user));
localStorage.setItem('user_data', JSON.stringify(this.user));
}
if (rememberMe) {
localStorage.setItem('remember_me', 'true');
@ -54,12 +54,11 @@ export class AuthService {
checkAuthenticationStatus(): void {
const token = localStorage.getItem('access_token');
const rememberMe = localStorage.getItem('remember_me') === 'true';
//const userData = localStorage.getItem('user_data');
//if (userData) {
// this.user = JSON.parse(userData);
//}
const userData = localStorage.getItem('user_data');
if (userData) {
this.user = JSON.parse(userData);
}
if (token && rememberMe) {
// Erstellen des Headers mit dem Token
const headers = new HttpHeaders({
'Authorization': token,
});
@ -70,18 +69,16 @@ export class AuthService {
},
error: (err) => {
console.error(err);
this.logOut(); // Bereinigung im Fehlerfall
this.logOut();
}
});
} else {
this.logOut(); // Bereinigung, falls Remember Me nicht gesetzt ist
this.logOut();
}
}
async getUser() {
try {
// Send request
return {
isOk: true,
data: this.user
@ -102,7 +99,6 @@ export class AuthService {
async createAccount(newUser: CustomerDto) {
try {
const result = await firstValueFrom(this.http.post<any>(`${API_URL}/customerapi/add`, newUser ));
//this.router.navigate(['/create-account']);
return {
isOk: true
};
@ -149,9 +145,9 @@ export class AuthService {
async logOut() {
this.user = null;
// Bereinigen des lokalen Speichers
localStorage.removeItem('access_token');
localStorage.removeItem('remember_me');
localStorage.removeItem('user_data');
this.router.navigate(['/login-form']);
}
}