fix(auth): fix login if using remember me
This commit is contained in:
parent
4bb2c107d3
commit
cff1e79837
|
|
@ -9,8 +9,8 @@ import { HistorygraphComponent } from './pages/dashboard/page/historygraph/histo
|
|||
export const routes: Routes = [
|
||||
{
|
||||
path: '',
|
||||
redirectTo: 'auth',
|
||||
pathMatch: 'full'
|
||||
component: AuthComponent,
|
||||
|
||||
},
|
||||
{
|
||||
path: 'auth',
|
||||
|
|
@ -32,7 +32,7 @@ export const routes: Routes = [
|
|||
{
|
||||
path: 'historygraph',
|
||||
component: HistorygraphComponent,
|
||||
canActivate: [AuthGuard],
|
||||
canActivate: [AuthGuard]
|
||||
}
|
||||
]
|
||||
}
|
||||
|
|
|
|||
|
|
@ -14,12 +14,11 @@ export class AuthGuard implements CanActivate {
|
|||
) {}
|
||||
|
||||
canActivate(): boolean {
|
||||
const token = this.storageService.getToken();
|
||||
if (token) {
|
||||
if(this.storageService.getToken()) {
|
||||
return true;
|
||||
} else {
|
||||
}else{
|
||||
this.router.navigate(['/auth']);
|
||||
this.toast.error('You need to login first');
|
||||
this.router.navigate(['auth']);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,10 +1,11 @@
|
|||
import { Injectable } from '@angular/core';
|
||||
import { HttpClient, HttpHeaders } from '@angular/common/http';
|
||||
import { Observable, tap } from 'rxjs';
|
||||
import { Observable, tap, catchError } from 'rxjs';
|
||||
import { ApiService } from './api.service';
|
||||
import { LoginData } from '../interface/auth';
|
||||
import { jwtDecode } from 'jwt-decode';
|
||||
import { StorageService } from './storage.service';
|
||||
import { ToastrService } from 'ngx-toastr';
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
|
|
@ -17,6 +18,7 @@ export class AuthService extends ApiService {
|
|||
constructor(
|
||||
http: HttpClient,
|
||||
private storageService: StorageService,
|
||||
private toast: ToastrService,
|
||||
) {
|
||||
super(http);
|
||||
}
|
||||
|
|
@ -32,12 +34,16 @@ export class AuthService extends ApiService {
|
|||
return this.http.post<any>(this.authUrl, formData, { headers }).pipe(
|
||||
tap(response => {
|
||||
const accessToken = response.data.token;
|
||||
this.storageService.saveToken(accessToken);
|
||||
this.storageService.saveToken(accessToken, data.rememberMe ?? false);
|
||||
|
||||
const jwtToken = response.data.jwtToken;
|
||||
const decodedToken: any = jwtDecode(jwtToken);
|
||||
|
||||
this.storageService.saveUserData(decodedToken.user.fullname, decodedToken.user.avatar);
|
||||
}),
|
||||
catchError(error => {
|
||||
this.toast.error('Login failed, please try again');
|
||||
throw error;
|
||||
})
|
||||
);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,16 +4,24 @@ import { Injectable } from '@angular/core';
|
|||
providedIn: 'root'
|
||||
})
|
||||
export class StorageService {
|
||||
saveToken(token: string): void {
|
||||
localStorage.setItem('accessToken', token);
|
||||
saveToken(token: string, rememberMe: boolean): void {
|
||||
if (rememberMe) {
|
||||
localStorage.setItem('accessToken', token);
|
||||
localStorage.setItem('rememberMe', 'true');
|
||||
} else {
|
||||
sessionStorage.setItem('accessToken', token);
|
||||
localStorage.removeItem('rememberMe')
|
||||
}
|
||||
}
|
||||
|
||||
getToken(): string | null {
|
||||
return localStorage.getItem('accessToken');
|
||||
return localStorage.getItem('accessToken') || sessionStorage.getItem('accessToken');
|
||||
}
|
||||
|
||||
clearToken(): void {
|
||||
localStorage.removeItem('accessToken');
|
||||
sessionStorage.removeItem('accessToken');
|
||||
localStorage.removeItem('rememberMe');
|
||||
}
|
||||
|
||||
saveUserData(fullName: string | null, avatar: string | null): void {
|
||||
|
|
|
|||
|
|
@ -27,6 +27,12 @@ export class AuthComponent {
|
|||
private router: Router,
|
||||
private toast: ToastrService) {}
|
||||
|
||||
ngOnInit(): void {
|
||||
if (this.storageService.getToken()) {
|
||||
this.router.navigate(['/dashboard']);
|
||||
}
|
||||
}
|
||||
|
||||
onSubmit() {
|
||||
this.loading = true;
|
||||
if (!this.email || !this.password) {
|
||||
|
|
@ -43,7 +49,7 @@ export class AuthComponent {
|
|||
|
||||
this.authService.login(loginData).subscribe(
|
||||
(response) => {
|
||||
this.storageService.saveToken(response.data.token);
|
||||
this.storageService.saveToken(response.data.token, this.rememberMe);
|
||||
this.router.navigate(['/dashboard']);
|
||||
this.toast.success('Login successful');
|
||||
this.loading = false;
|
||||
|
|
@ -56,6 +62,6 @@ export class AuthComponent {
|
|||
}
|
||||
|
||||
togglePasswordVisibility() {
|
||||
this.passwordVisible = !this.passwordVisible; // Toggle password visibility
|
||||
this.passwordVisible = !this.passwordVisible;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user