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 localStorage.getItem(PARAM_TOKEN);
    }

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

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

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

    public isAdmin(): boolean {
        return false;
    }

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

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