Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import { Injectable } from '@angular/core';
- export interface Credentials {
- token: string;
- }
- const credentialsKey = 'credentials';
- /**
- * Provides storage for authentication credentials.
- * The Credentials interface should be replaced with proper implementation.
- */
- @Injectable({
- providedIn: 'root'
- })
- export class CredentialsService {
- // tslint:disable-next-line:variable-name
- private _credentials: Credentials | null = null;
- constructor() {
- const savedCredentials = sessionStorage.getItem(credentialsKey) || localStorage.getItem(credentialsKey);
- if (savedCredentials) {
- this._credentials = JSON.parse(savedCredentials);
- }
- }
- /**
- * Checks is the user is authenticated.
- * @return True if the user is authenticated.
- */
- isAuthenticated(): boolean {
- return !!this.credentials;
- }
- /**
- * Gets the user credentials.
- * @return The user credentials or null if the user is not authenticated.
- */
- get credentials(): Credentials | null {
- return this._credentials;
- }
- /**
- * Sets the user credentials.
- * The credentials may be persisted across sessions by setting the `remember` parameter to true.
- * Otherwise, the credentials are only persisted for the current session.
- * @param credentials The user credentials.
- * @param remember True to remember credentials across sessions.
- */
- setCredentials(credentials?: Credentials, remember?: boolean) {
- this._credentials = credentials || null;
- if (credentials) {
- const storage = remember ? localStorage : sessionStorage;
- storage.setItem(credentialsKey, JSON.stringify(credentials));
- } else {
- sessionStorage.removeItem(credentialsKey);
- localStorage.removeItem(credentialsKey);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement