import { Injectable } from '@angular/core';
import { User } from '../entities';

const PARAM_TOKEN = 'token';
const PARAM_USER = 'user';

@Injectable()
export class AuthService {

	constructor() {}

	public getToken(): string {
		return sessionStorage.getItem(PARAM_TOKEN);
	}

	public setToken(token: string): void {
		sessionStorage.setItem(PARAM_TOKEN, token);
	}

	public isAuthenticated(): boolean {
		return this.getToken() != null;
	}

	public disconnect(): void {
		sessionStorage.clear();
	}

	public isAdmin(): boolean {
		return false;
	}

	public setUser(user: User): void {
		sessionStorage.setItem(PARAM_USER, JSON.stringify(user));
	}

	public getUser(): User {
		return JSON.parse(sessionStorage.getItem(PARAM_USER));
	}
}
