-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
21 changed files
with
450 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,21 @@ | ||
import { NgModule } from '@angular/core'; | ||
import { CommonModule } from '@angular/common'; | ||
import { AuthService } from '../modules/auth/services/auth.service'; | ||
import { HTTP_INTERCEPTORS } from '@angular/common/http'; | ||
import { JwtInterceptor } from './services/jwt.interceptor'; | ||
//where to add interfaces | ||
//answer: core module is the best place to add interfaces at the providers or | ||
@NgModule({ | ||
declarations: [], | ||
imports: [CommonModule], | ||
providers: [], | ||
providers: [ | ||
AuthService, | ||
{ | ||
provide: HTTP_INTERCEPTORS, | ||
useClass: JwtInterceptor, | ||
multi: true, | ||
}, | ||
], | ||
exports: [], | ||
}) | ||
export class CoreModule {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import { Injectable } from '@angular/core'; | ||
import { | ||
HttpEvent, | ||
HttpInterceptor, | ||
HttpHandler, | ||
HttpRequest, | ||
HttpErrorResponse, | ||
} from '@angular/common/http'; | ||
import { Observable, throwError } from 'rxjs'; | ||
import { catchError, switchMap } from 'rxjs/operators'; | ||
import { AuthService } from 'src/app/modules/auth/services/auth.service'; | ||
|
||
@Injectable() | ||
export class JwtInterceptor implements HttpInterceptor { | ||
constructor(private authService: AuthService) {} | ||
|
||
intercept( | ||
req: HttpRequest<any>, | ||
next: HttpHandler | ||
): Observable<HttpEvent<any>> { | ||
const token = localStorage.getItem('token'); | ||
if (!token) { | ||
return next.handle(req); | ||
} | ||
|
||
return this.authService.validateToken(token).pipe( | ||
switchMap((isValid) => { | ||
if (isValid) { | ||
const authReq = req.clone({ | ||
setHeaders: { | ||
Authorization: `Bearer ${token}`, | ||
}, | ||
}); | ||
return next.handle(authReq); | ||
} else { | ||
this.authService.logout(); | ||
return throwError('Token is invalid'); | ||
} | ||
}), | ||
catchError((error: HttpErrorResponse) => { | ||
if (error.status === 401) { | ||
this.authService.logout(); | ||
} | ||
return throwError(error); | ||
}) | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,62 @@ | ||
import { Injectable } from '@angular/core'; | ||
import { HttpClient } from '@angular/common/http'; | ||
import { HttpClient, HttpHeaders } from '@angular/common/http'; | ||
import { LoginRequestInterface } from '../interfaces/login-request-interface'; | ||
import { environment } from 'src/environments/environment.development'; | ||
import { Observable } from 'rxjs'; | ||
import { Observable, catchError, map, of, tap } from 'rxjs'; | ||
import { LoginResponseInterface } from '../interfaces/login-response-interface'; | ||
import { Router } from '@angular/router'; | ||
import { LocalStorageService } from 'src/app/core/services/local-storage.service'; | ||
|
||
@Injectable({ | ||
providedIn: 'root', | ||
}) | ||
export class AuthService { | ||
constructor(private http: HttpClient) {} | ||
constructor( | ||
private http: HttpClient, | ||
private router: Router, | ||
private localStorageService: LocalStorageService | ||
) {} | ||
|
||
private API_BASE_URL = environment.API_BASE_URL + '/Auth'; | ||
private API_BASE_URL = environment.API_BASE_URL + '/auth'; | ||
|
||
login(data: LoginRequestInterface): Observable<LoginResponseInterface> { | ||
return this.http.post<LoginResponseInterface>( | ||
`${this.API_BASE_URL}/login`, | ||
data | ||
); | ||
} | ||
|
||
validateToken(token: string): Observable<boolean> { | ||
const headers = new HttpHeaders({ | ||
Authorization: `Bearer ${token}`, | ||
}); | ||
return this.http | ||
.get(`${this.API_BASE_URL}/validateToken`, { | ||
headers, | ||
observe: 'response', | ||
}) | ||
.pipe( | ||
map((response) => response.status === 200), | ||
catchError(() => of(false)) | ||
); | ||
} | ||
|
||
refreshToken(): Observable<any> { | ||
return this.http | ||
.post<any>( | ||
`${this.API_BASE_URL}/refreshToken`, | ||
{}, | ||
{ withCredentials: true } | ||
) | ||
.pipe( | ||
tap((response) => { | ||
this.localStorageService.setItem('accessToken', response.token); | ||
}) | ||
); | ||
} | ||
|
||
logout() { | ||
localStorage.removeItem('token'); | ||
this.router.navigate(['/auth/login']); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
1 change: 1 addition & 0 deletions
1
bisHelpers/src/app/modules/dashboard/components/gpa-analysis/gpa-analysis.component.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<p>gpa-analysis works!</p> |
10 changes: 10 additions & 0 deletions
10
bisHelpers/src/app/modules/dashboard/components/gpa-analysis/gpa-analysis.component.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { Component } from '@angular/core'; | ||
|
||
@Component({ | ||
selector: 'app-gpa-analysis', | ||
templateUrl: './gpa-analysis.component.html', | ||
styleUrls: ['./gpa-analysis.component.css'] | ||
}) | ||
export class GpaAnalysisComponent { | ||
|
||
} |
Empty file.
Oops, something went wrong.