import { Injectable } from '@angular/core' import { HttpClient } from '@angular/common/http' import { environment } from 'src/environments/environment' /** * * This service takes care of following actions: * - Log in users. * - Log out users. * - Verify redirect uri. * @export */ @Injectable({ providedIn: 'root', }) export class SessionService { url = '' constructor(private httpClient: HttpClient) { this.url = environment.environmentUrl } /** * Redirect to invalid page if the url is not correct , * else continue with the login page * * @param redirectUri Uri to redirect the browser afte login success. * @returns Returns true or false depending on the url validity. */ verifyRedirectUri(redirectUri: string): Promise { return this.httpClient .get(this.url + '/auth/verify_redirect_uri', { params: { redirect_uri: redirectUri }, }) .toPromise() .then( (response: { msg: string }) => response.msg === 'Valid redirect_uri', ) } /** * Login service with credentials Email and password * * @param email User email * @param password User password * @returns IdToken and AccessToken */ login( email: string, password: string, ): Promise<{ IdToken: string; AccessToken: string; RobotinuumToken: string }> { return this.httpClient .post(this.url + '/auth/login', { data: { user: email, password, }, }) .toPromise() .then((response: { IdToken: string; AccessToken: string; RobotinuumToken: string }) => response) } /** * Register user with email and password * * @param {string} email * @param {string} password * @returns {Promise<{ user: any}>} * @memberof SessionService */ signUp( userCreate:any ): Promise<{ user: any}> { return this.httpClient .post(this.url + '/admin/users', userCreate) .toPromise() .then((response: any) => response) } }