Skip to content

Commit 9ce5d1a

Browse files
author
Bernardo
committed
Saves token
1 parent f77bb81 commit 9ce5d1a

9 files changed

Lines changed: 82 additions & 107 deletions

src/app/authentication/authentication.module.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
1-
import { NgModule } from '@angular/core';
21
import { CommonModule } from '@angular/common';
32
import { HTTP_INTERCEPTORS } from '@angular/common/http';
3+
import { NgModule } from '@angular/core';
44
import { AuthenticationInterceptor } from './interceptor/authentication.interceptor';
55
import { AuthenticationService } from './service/authentication.service';
6-
import { AuthenticationTokenService } from './service/authentication-token.service';
7-
8-
96

107
@NgModule({
118
declarations: [],
@@ -14,7 +11,6 @@ import { AuthenticationTokenService } from './service/authentication-token.servi
1411
],
1512
providers: [
1613
AuthenticationService,
17-
AuthenticationTokenService,
1814
{ provide: HTTP_INTERCEPTORS, useClass: AuthenticationInterceptor, multi: true }
1915
]
2016
})

src/app/authentication/interceptor/authentication.interceptor.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
import { HttpEvent, HttpHandler, HttpInterceptor, HttpRequest } from '@angular/common/http';
22
import { Injectable } from '@angular/core';
33
import { Observable } from 'rxjs';
4-
import { AuthenticationTokenService } from '../service/authentication-token.service';
4+
import { AuthenticationService } from '../service/authentication.service';
55

66

77
@Injectable()
88
export class AuthenticationInterceptor implements HttpInterceptor {
99

1010
private tokenHeaderKey = 'Authorization';
1111

12-
constructor(private token: AuthenticationTokenService) { }
12+
constructor(private authenticationService: AuthenticationService) { }
1313

1414
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
1515
let authReq = req;
16-
const token = this.token.getToken();
16+
const token = this.authenticationService.getToken();
1717
if (token != null) {
1818
authReq = req.clone({ headers: req.headers.set(this.tokenHeaderKey, 'Bearer ' + token) });
1919
}

src/app/authentication/service/authentication-token.service.spec.ts

Lines changed: 0 additions & 16 deletions
This file was deleted.

src/app/authentication/service/authentication-token.service.ts

Lines changed: 0 additions & 39 deletions
This file was deleted.
Lines changed: 6 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,20 @@
1-
import { HttpClient } from '@angular/common/http';
21
import { Injectable } from '@angular/core';
3-
import { Router } from '@angular/router';
4-
import { environment } from '@environments/environment';
5-
import { BehaviorSubject, Observable } from 'rxjs';
6-
import { map } from 'rxjs/operators';
7-
import { User } from '../model/user';
82

9-
@Injectable({
10-
providedIn: 'root'
11-
})
3+
@Injectable()
124
export class AuthenticationService {
135

14-
private userSubject: BehaviorSubject<User>;
6+
private tokenId: string = 'access_token';
157

168
constructor(
17-
private router: Router,
18-
private http: HttpClient
199
) {
20-
this.userSubject = new BehaviorSubject<User>(new User());
2110
}
2211

23-
public get user(): User {
24-
return this.userSubject.value;
12+
getToken(): string | null {
13+
return localStorage.getItem(this.tokenId);
2514
}
2615

27-
login(username: string, password: string): Observable<User> {
28-
return this.http.post<User>(`${environment.apiUrl}/login`, { username, password })
29-
.pipe(map(user => {
30-
let loggedUser;
31-
32-
if (user) {
33-
loggedUser = user;
34-
loggedUser.authdata = window.btoa(username + ':' + password);
35-
loggedUser.logged = true;
36-
} else {
37-
loggedUser = new User();
38-
}
39-
40-
this.userSubject.next(loggedUser);
41-
return loggedUser;
42-
}));
43-
}
44-
45-
logout() {
46-
// remove user from local storage and set current user to null
47-
localStorage.removeItem('user');
48-
this.userSubject.next(new User());
49-
this.router.navigate(['/login']);
50-
}
51-
52-
register(user: User) {
53-
return this.http.post(`${environment.apiUrl}/users/register`, user);
16+
saveToken(token: string) {
17+
return localStorage.setItem(this.tokenId, token);
5418
}
5519

5620
}

src/app/login/login-form/login-form.component.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { FormBuilder, FormGroup, Validators } from '@angular/forms';
33
import { ActivatedRoute, Router } from '@angular/router';
44
import { AuthenticationService } from '@app/authentication/service/authentication.service';
55
import { first } from 'rxjs/operators';
6+
import { LoginService } from '../service/login.service';
67

78
@Component({
89
selector: 'dahs-login-form',
@@ -21,10 +22,11 @@ export class LoginFormComponent implements OnInit {
2122
private formBuilder: FormBuilder,
2223
private route: ActivatedRoute,
2324
private router: Router,
25+
private loginService: LoginService,
2426
private authenticationService: AuthenticationService
2527
) {
2628
// redirect to home if already logged in
27-
if (this.authenticationService.user.logged) {
29+
if (this.loginService.user.logged) {
2830
this.router.navigate(['/']);
2931
}
3032

@@ -51,12 +53,17 @@ export class LoginFormComponent implements OnInit {
5153
}
5254

5355
this.loading = true;
54-
this.authenticationService.login(this.f['username'].value, this.f['password'].value)
56+
this.loginService.login(this.f['username'].value, this.f['password'].value)
5557
.subscribe({
5658
next: user => {
5759
this.loading = false;
5860
if (user.logged) {
61+
if(user.authdata){
62+
this.authenticationService.saveToken(user.authdata);
63+
}
5964
this.router.navigate([this.returnUrl]);
65+
} else {
66+
this.authenticationService.saveToken('');
6067
}
6168
},
6269
error: error => {

src/app/login/login.module.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { FormsModule, ReactiveFormsModule } from '@angular/forms';
44
import { AuthenticationModule } from '@app/authentication/authentication.module';
55
import { LoginFormComponent } from './login-form/login-form.component';
66
import { LoginRoutingModule } from './login-routing.module';
7+
import { LoginService } from './service/login.service';
78

89

910

@@ -17,6 +18,9 @@ import { LoginRoutingModule } from './login-routing.module';
1718
CommonModule,
1819
FormsModule,
1920
ReactiveFormsModule
21+
],
22+
providers: [
23+
LoginService
2024
]
2125
})
2226
export class LoginModule { }
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { TestBed } from '@angular/core/testing';
2+
3+
import { LoginService } from './login.service';
4+
5+
describe('LoginService', () => {
6+
let service: LoginService;
7+
8+
beforeEach(() => {
9+
TestBed.configureTestingModule({});
10+
service = TestBed.inject(LoginService);
11+
});
12+
13+
it('should be created', () => {
14+
expect(service).toBeTruthy();
15+
});
16+
});
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import { HttpClient } from '@angular/common/http';
2+
import { Injectable } from '@angular/core';
3+
import { Router } from '@angular/router';
4+
import { User } from '@app/authentication/model/user';
5+
import { environment } from '@environments/environment';
6+
import { Observable } from 'rxjs';
7+
import { map } from 'rxjs/operators';
8+
9+
@Injectable()
10+
export class LoginService {
11+
12+
public user: User = new User();
13+
14+
constructor(
15+
private router: Router,
16+
private http: HttpClient
17+
) { }
18+
19+
login(username: string, password: string): Observable<User> {
20+
return this.http.post<User>(`${environment.apiUrl}/login`, { username, password })
21+
.pipe(map(user => {
22+
let loggedUser;
23+
24+
if (user) {
25+
loggedUser = user;
26+
loggedUser.authdata = window.btoa(username + ':' + password);
27+
loggedUser.logged = true;
28+
} else {
29+
loggedUser = new User();
30+
}
31+
32+
this.user = loggedUser;
33+
return this.user;
34+
}));
35+
}
36+
37+
logout() {
38+
// remove user from local storage and set current user to null
39+
localStorage.removeItem('user');
40+
this.user = new User();
41+
this.router.navigate(['/login']);
42+
}
43+
}

0 commit comments

Comments
 (0)