Skip to content

Commit f77bb81

Browse files
author
Bernardo
committed
User data is stored correctly
1 parent ec2240e commit f77bb81

3 files changed

Lines changed: 27 additions & 18 deletions

File tree

src/app/authentication/model/user.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@ export class User {
22
id: number = 0;
33
username: string = '';
44
password: string = '';
5+
logged: boolean = false;
56
authdata?: string;
67
}

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

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,27 +12,33 @@ import { User } from '../model/user';
1212
export class AuthenticationService {
1313

1414
private userSubject: BehaviorSubject<User>;
15-
public user: Observable<User>;
1615

1716
constructor(
1817
private router: Router,
1918
private http: HttpClient
2019
) {
21-
this.userSubject = new BehaviorSubject<User>(JSON.parse(<string>localStorage.getItem('user')));
22-
this.user = this.userSubject.asObservable();
20+
this.userSubject = new BehaviorSubject<User>(new User());
2321
}
2422

25-
public get userValue(): User {
23+
public get user(): User {
2624
return this.userSubject.value;
2725
}
2826

29-
login(username: string, password: string) {
27+
login(username: string, password: string): Observable<User> {
3028
return this.http.post<User>(`${environment.apiUrl}/login`, { username, password })
3129
.pipe(map(user => {
32-
user.authdata = window.btoa(username + ':' + password);
33-
localStorage.setItem('user', JSON.stringify(user));
34-
this.userSubject.next(user);
35-
return 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;
3642
}));
3743
}
3844

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

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { AuthenticationService } from '@app/authentication/service/authenticatio
55
import { first } from 'rxjs/operators';
66

77
@Component({
8-
selector: 'app-login-form',
8+
selector: 'dahs-login-form',
99
templateUrl: './login-form.component.html',
1010
styleUrls: ['./login-form.component.sass']
1111
})
@@ -24,7 +24,7 @@ export class LoginFormComponent implements OnInit {
2424
private authenticationService: AuthenticationService
2525
) {
2626
// redirect to home if already logged in
27-
if (this.authenticationService.userValue) {
27+
if (this.authenticationService.user.logged) {
2828
this.router.navigate(['/']);
2929
}
3030

@@ -51,17 +51,19 @@ export class LoginFormComponent implements OnInit {
5151
}
5252

5353
this.loading = true;
54-
this.authenticationService.login(this.f.username.value, this.f.password.value)
55-
.pipe(first())
56-
.subscribe(
57-
data => {
54+
this.authenticationService.login(this.f['username'].value, this.f['password'].value)
55+
.subscribe({
56+
next: user => {
5857
this.loading = false;
59-
this.router.navigate([this.returnUrl]);
58+
if (user.logged) {
59+
this.router.navigate([this.returnUrl]);
60+
}
6061
},
61-
error => {
62+
error: error => {
6263
this.error = error;
6364
this.loading = false;
64-
});
65+
}
66+
});
6567
}
6668

6769
}

0 commit comments

Comments
 (0)