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