Skip to content

Commit 8605f38

Browse files
committed
Same commit as before
Some new files were untracked.
1 parent 661a430 commit 8605f38

File tree

6 files changed

+120
-0
lines changed

6 files changed

+120
-0
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export const environment = {
2+
production: true,
3+
UserServiceApiUrl: 'http://localhost:3001'
4+
};
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { Injectable } from '@angular/core';
2+
import { HttpRequest, HttpHandler, HttpEvent, HttpInterceptor } from '@angular/common/http';
3+
import { Observable, throwError } from 'rxjs';
4+
import { catchError } from 'rxjs/operators';
5+
6+
import { AuthenticationService } from '../../services/authentication.service';
7+
8+
@Injectable()
9+
export class ErrorInterceptor implements HttpInterceptor {
10+
constructor(private authenticationService: AuthenticationService) { }
11+
12+
intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
13+
return next.handle(request).pipe(catchError(err => {
14+
if ([401, 403].includes(err.status)) {
15+
// auto logout if 401 Unauthorized or 403 Forbidden response returned from api
16+
this.authenticationService.logout();
17+
}
18+
19+
const error = err.error.message || err.statusText;
20+
return throwError(error);
21+
}))
22+
}
23+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { Injectable } from '@angular/core';
2+
import { HttpRequest, HttpHandler, HttpEvent, HttpInterceptor } from '@angular/common/http';
3+
import { Observable } from 'rxjs';
4+
5+
import { environment } from '../../environments/environment';
6+
import { AuthenticationService } from '../../services/authentication.service';
7+
8+
@Injectable()
9+
export class JwtInterceptor implements HttpInterceptor {
10+
constructor(private authenticationService: AuthenticationService) { }
11+
12+
intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
13+
// add auth header with jwt if user is logged in and request is to the api url
14+
const user = this.authenticationService.userValue;
15+
const isLoggedIn = user?.accessToken;
16+
const isApiUrl = request.url.startsWith(environment.UserServiceApiUrl);
17+
if (isLoggedIn && isApiUrl) {
18+
request = request.clone({
19+
setHeaders: {
20+
Authorization: `Bearer ${user.accessToken}`
21+
}
22+
});
23+
}
24+
25+
return next.handle(request);
26+
}
27+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
export class User {
2+
id?: string;
3+
username?: string;
4+
email?: string;
5+
isAdmin?: boolean;
6+
createdAt?: string;
7+
accessToken?: string;
8+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import { Injectable } from '@angular/core';
2+
import { Router } from '@angular/router';
3+
import { HttpClient } from '@angular/common/http';
4+
import { BehaviorSubject, Observable } from 'rxjs';
5+
import { map, tap } from 'rxjs/operators';
6+
import { environment } from '../environments/environment';
7+
import { User } from '../models/user.model';
8+
9+
@Injectable({ providedIn: 'root' })
10+
export class AuthenticationService {
11+
private userSubject: BehaviorSubject<User | null>;
12+
public user: Observable<User | null>;
13+
14+
constructor(
15+
private router: Router,
16+
private http: HttpClient
17+
) {
18+
this.userSubject = new BehaviorSubject(JSON.parse(localStorage.getItem('user')!));
19+
this.user = this.userSubject.asObservable();
20+
}
21+
22+
public get userValue() {
23+
return this.userSubject.value;
24+
}
25+
26+
login(username: string, password: string) {
27+
console.log('in authentication service login');
28+
console.log(username, password);
29+
console.log(`${environment.UserServiceApiUrl}/auth/login`);
30+
return this.http.post<any>(`${environment.UserServiceApiUrl}/auth/login`,
31+
{ "username": username, "password": password })
32+
.pipe(
33+
tap(response => {
34+
console.log('HTTP POST response:', response);
35+
}),
36+
map(response => {
37+
// store user details and jwt token in local storage to keep user logged in between page refreshes
38+
const user = response.data;
39+
localStorage.setItem('user', JSON.stringify(user));
40+
this.userSubject.next(user);
41+
return user;
42+
}));
43+
}
44+
45+
logout() {
46+
// remove user from local storage to log user out
47+
localStorage.removeItem('user');
48+
this.userSubject.next(null);
49+
this.router.navigate(['/login']);
50+
}
51+
}

services/user-service/.gitignore

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
**/node_modules
2+
**/.env
3+
**/.idea
4+
# Vim temp files
5+
*~
6+
*.swp
7+
*.swo

0 commit comments

Comments
 (0)