src/app/auth/auth.service.ts
Service used to request and handle tokens and authorization
Properties |
Methods |
constructor(http: HttpClient)
|
||||||||
Defined in src/app/auth/auth.service.ts:46
|
||||||||
Builds an instance of the service
Parameters :
|
changeLoginStatus | ||||||||
changeLoginStatus(status: boolean)
|
||||||||
Defined in src/app/auth/auth.service.ts:60
|
||||||||
Changes the loginStatus and sneds the corresponding update in the loginStatusStream
Parameters :
Returns :
void
|
getAllowedActions |
getAllowedActions()
|
Defined in src/app/auth/auth.service.ts:188
|
Returns the user permission over the specified action stored in the local storage
Returns :
boolean
the permission as a boolean |
getHttpHeaders |
getHttpHeaders()
|
Defined in src/app/auth/auth.service.ts:69
|
Builds and returns HttpHeaders for the requests, including the token for requests
Returns :
HttpHeaders
http headers |
getToken |
getToken()
|
Defined in src/app/auth/auth.service.ts:162
|
Returns the token stored in the local storage
Returns :
string | undefined
the token as a string, or undefined if there is no token |
getUser |
getUser()
|
Defined in src/app/auth/auth.service.ts:175
|
Returns the user stored in the local storage
Returns :
string | undefined
the user name as a string, or undefined if there is no user |
hasToken |
hasToken()
|
Defined in src/app/auth/auth.service.ts:86
|
Checks wether or not the user has a token, which is true if there is a valid token
Returns :
Observable<boolean>
true if there is a valid token, false if not |
hasValidToken |
hasValidToken()
|
Defined in src/app/auth/auth.service.ts:96
|
Checks wether or not the user is logged-in (has a valid token), which is true if there is a valid token
Returns :
Observable<boolean>
true if there is a valid token, false if not |
login | ||||||||||||
login(username: string, password: string)
|
||||||||||||
Defined in src/app/auth/auth.service.ts:127
|
||||||||||||
Requests a token from the server, sending user and password information. Saves the token in the local storage
Parameters :
Returns :
Observable<boolean>
observable of true if the login is successful, observable of false if not |
logout |
logout()
|
Defined in src/app/auth/auth.service.ts:152
|
Logs out of the server by deleting the token from the local storage
Returns :
void
|
removeAllowedActions |
removeAllowedActions()
|
Defined in src/app/auth/auth.service.ts:214
|
Deletes the allowed_actions from the local storage
Returns :
void
|
removeToken |
removeToken()
|
Defined in src/app/auth/auth.service.ts:200
|
Deletes the token from the local storage
Returns :
void
|
removeUser |
removeUser()
|
Defined in src/app/auth/auth.service.ts:207
|
Deletes the user from the local storage
Returns :
void
|
storeAllowedActions | ||||||
storeAllowedActions(allowed_actions: Object)
|
||||||
Defined in src/app/auth/auth.service.ts:240
|
||||||
Stores the user allowed_actions in the local storage, replacing the previous allowed_actions, if any
Parameters :
Returns :
void
|
storeToken | ||||||||
storeToken(token: string)
|
||||||||
Defined in src/app/auth/auth.service.ts:222
|
||||||||
Stores a given the token in the local storage, replacing the previous token, if any
Parameters :
Returns :
void
|
storeUser | ||||||||
storeUser(user: string)
|
||||||||
Defined in src/app/auth/auth.service.ts:231
|
||||||||
Stores a given the user in the local storage, replacing the previous user, if any
Parameters :
Returns :
void
|
ACTIONS_STORAGE_NAME |
Type : string
|
Default value : 'IAS-ACTIONS'
|
Defined in src/app/auth/auth.service.ts:30
|
Name for the browser to use in order to save the allowed actions in the local storage |
loginStatus |
Default value : false
|
Defined in src/app/auth/auth.service.ts:41
|
Store if the user is logged in or not |
Public loginStatusStream |
Default value : new BehaviorSubject<boolean>(null)
|
Defined in src/app/auth/auth.service.ts:46
|
Stream of notifications when the user logs in. Sends true, if the user is logged in, and false if not |
redirectUrl |
Type : string
|
Defined in src/app/auth/auth.service.ts:35
|
Store the URL so we can redirect after logging in |
TOKEN_STORAGE_NAME |
Type : string
|
Default value : 'IAS-TOKEN'
|
Defined in src/app/auth/auth.service.ts:20
|
Name for the browser to use in order to save the token in the local storage |
USER_STORAGE_NAME |
Type : string
|
Default value : 'IAS-USER'
|
Defined in src/app/auth/auth.service.ts:25
|
Name for the browser to use in order to save the user in the local storage |
import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { Observable, of, BehaviorSubject } from 'rxjs';
import { catchError, map } from 'rxjs/operators';
import { BackendUrls } from '../settings';
import { environment } from '../../environments/environment';
/**
* Service used to request and handle tokens and authorization
*/
@Injectable({
providedIn: 'root'
})
export class AuthService {
/**
* Name for the browser to use in order to save the token in the local storage
*/
TOKEN_STORAGE_NAME = 'IAS-TOKEN';
/**
* Name for the browser to use in order to save the user in the local storage
*/
USER_STORAGE_NAME = 'IAS-USER';
/**
* Name for the browser to use in order to save the allowed actions in the local storage
*/
ACTIONS_STORAGE_NAME = 'IAS-ACTIONS';
/**
* Store the URL so we can redirect after logging in
*/
redirectUrl: string;
/**
* Store if the user is logged in or not
*/
loginStatus = false;
/**
* Stream of notifications when the user logs in. Sends true, if the user is logged in, and false if not
*/
public loginStatusStream = new BehaviorSubject<boolean>(null);
/**
* Builds an instance of the service
* @param {HttpClient} http Angular HttpClient used to request the token for authentication
*/
constructor(
private http: HttpClient
) { }
/**
* Changes the {@link loginStatus} and sneds the corresponding update in the {@link loginStatusStream}
* @param {boolean} status the new login status
*/
changeLoginStatus(status: boolean) {
this.loginStatus = status;
this.loginStatusStream.next(status);
}
/**
* Builds and returns HttpHeaders for the requests, including the token for requests
* @returns {HttpHeaders} http headers
*/
getHttpHeaders(): HttpHeaders {
if (this.getToken()) {
return new HttpHeaders({
'Content-Type': 'application/json',
'Authorization': 'Token ' + this.getToken()
});
} else {
return new HttpHeaders({
'Content-Type': 'application/json',
});
}
}
/**
* Checks wether or not the user has a token, which is true if there is a valid token
* @returns { Observable<boolean>} true if there is a valid token, false if not
*/
hasToken(): Observable<boolean> {
const hasToken = this.getToken() !== undefined && this.getToken() !== null;
this.changeLoginStatus(hasToken);
return of(hasToken);
}
/**
* Checks wether or not the user is logged-in (has a valid token), which is true if there is a valid token
* @returns { Observable<boolean>} true if there is a valid token, false if not
*/
hasValidToken(): Observable<boolean> {
if (!this.getToken()) {
this.changeLoginStatus(false);
return of(false);
} else {
const url = `${environment.httpUrl}${BackendUrls.VALIDATE_TOKEN}`;
return this.http.get(url, {headers: this.getHttpHeaders()} ).pipe(
map((response: any) => {
const user_data = response['user_data'];
const allowed_actions = response['allowed_actions'];
this.storeUser(user_data['username']);
this.storeAllowedActions(allowed_actions);
this.changeLoginStatus(true);
return true;
}),
catchError( error => {
console.error(error);
this.logout();
return of(false);
}
));
}
}
/**
* Requests a token from the server, sending user and password information.
* Saves the token in the local storage
* @param {string} username the name of the user
* @param {string} password the password of the user
* @returns {Observable<boolean>} observable of true if the login is successful, observable of false if not
*/
login(username: string, password: string): Observable<boolean> {
const url = `${environment.httpUrl}${BackendUrls.TOKEN}`;
return this.http.post(url, {
username: username,
password: password
}).pipe(map((response: any) => {
// console.log('Login response:', response);
const token = response['token'];
if (token) {
this.storeToken(token);
this.storeUser(username);
this.changeLoginStatus(true);
const allowed_actions = response['allowed_actions'];
this.storeAllowedActions(allowed_actions);
return true;
} else {
this.changeLoginStatus(false);
return false;
}
}));
}
/**
* Logs out of the server by deleting the token from the local storage
*/
logout(): void {
this.changeLoginStatus(false);
this.removeToken();
this.removeUser();
}
/**
* Returns the token stored in the local storage
* @returns {string | undefined} the token as a string, or undefined if there is no token
*/
getToken(): string | undefined {
const token = localStorage.getItem(this.TOKEN_STORAGE_NAME);
if (token === null) {
return undefined;
} else {
return JSON.parse(token);
}
}
/**
* Returns the user stored in the local storage
* @returns {string | undefined} the user name as a string, or undefined if there is no user
*/
getUser(): string | undefined {
const user = localStorage.getItem(this.USER_STORAGE_NAME);
if (user === null) {
return undefined;
} else {
return JSON.parse(user);
}
}
/**
* Returns the user permission over the specified action stored in the local storage
* @returns {boolean | undefined} the permission as a boolean
*/
getAllowedActions(): boolean {
const allowed_actions = localStorage.getItem(this.ACTIONS_STORAGE_NAME);
if (allowed_actions === null) {
return undefined;
} else {
return JSON.parse(allowed_actions);
}
}
/**
* Deletes the token from the local storage
*/
removeToken() {
localStorage.removeItem(this.TOKEN_STORAGE_NAME);
}
/**
* Deletes the user from the local storage
*/
removeUser() {
localStorage.removeItem(this.USER_STORAGE_NAME);
}
/**
* Deletes the allowed_actions from the local storage
*/
removeAllowedActions() {
localStorage.removeItem(this.ACTIONS_STORAGE_NAME);
}
/**
* Stores a given the token in the local storage, replacing the previous token, if any
* @param {string} token the token to be stored
*/
storeToken(token: string) {
localStorage.removeItem(this.TOKEN_STORAGE_NAME);
localStorage.setItem(this.TOKEN_STORAGE_NAME, JSON.stringify(token));
}
/**
* Stores a given the user in the local storage, replacing the previous user, if any
* @param {string} user the user to be stored
*/
storeUser(user: string) {
localStorage.removeItem(this.USER_STORAGE_NAME);
localStorage.setItem(this.USER_STORAGE_NAME, JSON.stringify(user));
}
/**
* Stores the user allowed_actions in the local storage, replacing the previous allowed_actions, if any
* @param {string} permission the user allowed_actions to be stored
*/
storeAllowedActions(allowed_actions: Object) {
this.removeAllowedActions();
localStorage.setItem(this.ACTIONS_STORAGE_NAME, JSON.stringify(allowed_actions));
}
}