Skip to content
Snippets Groups Projects
Commit c18c5838 authored by David Noah Donges's avatar David Noah Donges
Browse files

Persist user data

Store and load user data so we don't lose the user's data on reload.
parent 99c864eb
Branches
Tags
1 merge request!58Persist user data
......@@ -3,17 +3,27 @@ import { User } from './user';
import { Observable } from 'rxjs/Observable';
import { of } from 'rxjs/observable/of';
import { UserRole } from './user-roles.enum';
import { DataStoreService } from './data-store.service';
// TODO: connect to API
// TODO: persist user data (shouldn't get lost on page refresh)
@Injectable()
export class AuthenticationService {
private mockUser: User;
constructor() { }
private readonly STORAGE_KEY: string = 'USER';
private user: User;
constructor(private dataStoreService: DataStoreService) {
if (dataStoreService.has(this.STORAGE_KEY)) {
// Load user data from local data store if available
this.user = JSON.parse(dataStoreService.get(this.STORAGE_KEY));
}
}
login(email: string, password: string, role: UserRole): Observable<boolean> {
this.mockUser = new User(1, '', email, role);
this.user = new User(1, '', email, role, 'TOKEN');
// Store user data in local storage to retain the data when the user reloads the page
this.dataStoreService.set(this.STORAGE_KEY, JSON.stringify(this.user));
return of(true);
}
......@@ -26,19 +36,21 @@ export class AuthenticationService {
}
logout() {
this.mockUser = null;
// Destroy the persisted user data
this.dataStoreService.remove(this.STORAGE_KEY);
this.user = undefined;
}
getUser(): Observable<User> {
return of(this.mockUser);
return of(this.user);
}
isLoggedIn(): Observable<boolean> {
return of(this.mockUser !== undefined);
return of(this.user !== undefined);
}
getRole(): Observable<UserRole> {
return of(this.mockUser.role);
return of(this.user.role);
}
}
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment