Skip to content

Commit bdb9296

Browse files
committed
Stores user in local storage
1 parent 1dac8e2 commit bdb9296

1 file changed

Lines changed: 12 additions & 2 deletions

File tree

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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,16 @@ export class AuthenticationService {
1616

1717
private user: User = new User();
1818

19+
private userKey = 'user';
20+
1921
constructor(
2022
private http: HttpClient
21-
) { }
23+
) {
24+
const localUser = localStorage.getItem(this.userKey);
25+
if (localUser) {
26+
this.user = JSON.parse(localUser);
27+
}
28+
}
2229

2330
public login(username: string, password: string): Observable<User> {
2431
return this.http.post<ApiResponse<LoginStatus>>(this.loginUrl, { username, password })
@@ -41,9 +48,12 @@ export class AuthenticationService {
4148
if (status) {
4249
loggedUser.username = status.username;
4350
loggedUser.logged = status.logged;
51+
loggedUser.token = status.token;
4452

4553
if (loggedUser.logged) {
46-
loggedUser.token = status.token;
54+
localStorage.setItem(this.userKey, JSON.stringify(loggedUser));
55+
} else {
56+
localStorage.removeItem(this.userKey);
4757
}
4858
}
4959

0 commit comments

Comments
 (0)