Skip to content

Commit 00649f4

Browse files
committed
Using the same login model as the backend
1 parent 5f8307e commit 00649f4

6 files changed

Lines changed: 37 additions & 42 deletions

File tree

src/app/authentication/model/user.ts renamed to src/app/authentication/model/login-details.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
2-
* Application user. Part of the authentication model.
2+
* User login details. Shows the status after a login attempt.
33
*/
4-
export class User {
4+
export class LoginDetails {
55
/**
66
* User username.
77
*/
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/**
2+
* User login request. Used to attempt a login, for this reason it contains the user credentials.
3+
*/
4+
export class LoginRequest {
5+
/**
6+
* Username for the login attempt.
7+
*/
8+
username: string = '';
9+
/**
10+
* password for the login attempt.
11+
*/
12+
password: string = '';
13+
}

src/app/authentication/model/login-status.ts

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

src/app/authentication/service/authentication.service.ts

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import { ApiResponse } from '@app/api/model/api-response';
44
import { environment } from '@environments/environment';
55
import { BehaviorSubject, Observable } from 'rxjs';
66
import { map, tap } from 'rxjs/operators';
7-
import { LoginStatus } from '../model/login-status';
8-
import { User } from '../model/user';
7+
import { LoginDetails } from '../model/login-details';
8+
import { LoginRequest } from '../model/login-request';
99

1010
/**
1111
* Authentication service. Handles login and logout operations.
@@ -31,9 +31,9 @@ export class AuthenticationService {
3131

3232
private userKey = 'user';
3333

34-
private userSubject: BehaviorSubject<User>;
34+
private userSubject: BehaviorSubject<LoginDetails>;
3535

36-
private user: Observable<User>;
36+
private user: Observable<LoginDetails>;
3737

3838
private rememberMe = false;
3939

@@ -50,12 +50,11 @@ export class AuthenticationService {
5050
*
5151
* If the 'remember me' option is active, the user will be stored in the local storage.
5252
*
53-
* @param username username for login
54-
* @param password password for login
53+
* @param request login request
5554
* @returns the user resulting from the login
5655
*/
57-
public login(username: string, password: string): Observable<User> {
58-
return this.http.post<ApiResponse<LoginStatus>>(this.loginUrl, { username, password })
56+
public login(request: LoginRequest): Observable<LoginDetails> {
57+
return this.http.post<ApiResponse<LoginDetails>>(this.loginUrl, request)
5958
.pipe(map(response => response.content))
6059
.pipe(map(response => this.toUser(response)))
6160
.pipe(tap(user => this.storeUser(user)));
@@ -66,7 +65,7 @@ export class AuthenticationService {
6665
*/
6766
public logout() {
6867
// Store empty user
69-
this.userSubject.next(new User());
68+
this.userSubject.next(new LoginDetails());
7069

7170
// Clear local storage
7271
localStorage.removeItem(this.userKey);
@@ -76,7 +75,7 @@ export class AuthenticationService {
7675
* Returns the user currently in session.
7776
* @returns the user currently in session
7877
*/
79-
public getUser(): User {
78+
public getUser(): LoginDetails {
8079
return this.userSubject.value;
8180
}
8281

@@ -85,7 +84,7 @@ export class AuthenticationService {
8584
*
8685
* @returns the user currently in session as an observable
8786
*/
88-
public getUserObservable(): Observable<User> {
87+
public getUserObservable(): Observable<LoginDetails> {
8988
return this.user;
9089
}
9190

@@ -104,19 +103,19 @@ export class AuthenticationService {
104103
*
105104
* @returns the user stored in the local storage as part of the 'remember me'
106105
*/
107-
private readUserFromLocal(): BehaviorSubject<User> {
108-
let subject: BehaviorSubject<User>;
106+
private readUserFromLocal(): BehaviorSubject<LoginDetails> {
107+
let subject: BehaviorSubject<LoginDetails>;
109108

110109
// If the user was stored, load it
111110
const localUser = localStorage.getItem(this.userKey);
112111
if (localUser) {
113112
// User found in local storage
114113
const readUser = JSON.parse(localUser);
115-
subject = new BehaviorSubject<User>(readUser);
114+
subject = new BehaviorSubject<LoginDetails>(readUser);
116115
} else {
117116
// User not found
118117
// Use default user
119-
subject = new BehaviorSubject<User>(new User());
118+
subject = new BehaviorSubject<LoginDetails>(new LoginDetails());
120119
}
121120

122121
return subject;
@@ -128,10 +127,10 @@ export class AuthenticationService {
128127
* @param status status to map
129128
* @returns user generated from the login status
130129
*/
131-
private toUser(status: LoginStatus): User {
130+
private toUser(status: LoginDetails): LoginDetails {
132131
let loggedUser;
133132

134-
loggedUser = new User();
133+
loggedUser = new LoginDetails();
135134
if (status) {
136135
// Received data
137136
loggedUser.username = status.username;
@@ -148,7 +147,7 @@ export class AuthenticationService {
148147
*
149148
* @param user user to store
150149
*/
151-
private storeUser(user: User) {
150+
private storeUser(user: LoginDetails) {
152151
this.userSubject.next(user);
153152

154153
if (this.rememberMe) {

src/app/login/containers/logout-button/logout-button.component.spec.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11
import { HttpClientTestingModule } from '@angular/common/http/testing';
22
import { ComponentFixture, TestBed } from '@angular/core/testing';
33
import { RouterTestingModule } from '@angular/router/testing';
4-
import { User } from '@app/authentication/model/user';
4+
import { LoginDetails } from '@app/authentication/model/login-details';
55
import { AuthenticationService } from '@app/authentication/service/authentication.service';
66
import { LogoutButtonComponent } from './logout-button.component';
77

88
describe('LogoutButtonComponent', () => {
99
let component: LogoutButtonComponent;
1010
let fixture: ComponentFixture<LogoutButtonComponent>;
1111
let authenticationServiceStub: Partial<AuthenticationService>;
12-
let user: User;
12+
let user: LoginDetails;
1313

1414
beforeEach(async () => {
15-
user = new User();
15+
user = new LoginDetails();
1616

1717
authenticationServiceStub = {
18-
getUser(): User {
18+
getUser(): LoginDetails {
1919
return user;
2020
}
2121
};

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ export class LoginViewComponent implements OnInit {
5353
this.authenticationService.setRememberMe(login.rememberMe);
5454

5555
// Login request
56-
this.authenticationService.login(login.username, login.password)
56+
this.authenticationService.login(login)
5757
.subscribe({
5858
next: user => {
5959
// Succesful request

0 commit comments

Comments
 (0)