Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import {Injectable, Inject} from '@angular/core';
- // import {Http} from '@angular/http';
- import {Observable} from 'rxjs/Observable';
- import {BehaviorSubject} from 'rxjs/BehaviorSubject';
- import 'rxjs/add/observable/throw';
- import 'rxjs/add/observable/fromPromise';
- import 'rxjs/add/operator/map';
- import 'rxjs/add/operator/mergeMap';
- import 'rxjs/add/operator/toPromise';
- import 'rxjs/add/operator/catch';
- import {API_ENDPOINTS, IApiEndpoints} from "../config/API_ENDPOINTS.config";
- import {APPLICATION_SETTINGS, IApplicationSettings} from "../config/APPLICATION_SETTINGS.config";
- import{AuthHttpService} from '../authHttp/authHttp.service'
- import {User} from './user.model';
- @Injectable()
- export class AuthenticationService {
- private STORAGE_IDENTIFIER: string;
- public TOKEN: any;
- private _currentUser: BehaviorSubject<any>;
- public currentUser: Observable<any>;
- constructor(@Inject(API_ENDPOINTS) private endpoints: IApiEndpoints,
- @Inject(APPLICATION_SETTINGS) private appSettings: IApplicationSettings,
- // private http: Http,
- // private storage: Storage,
- public authHttpService: AuthHttpService) {
- this.STORAGE_IDENTIFIER = appSettings.storageIdentifier + 'AUTH';
- this._currentUser = new BehaviorSubject(null);
- this.currentUser = this._currentUser.asObservable();
- const token = localStorage.getItem(this.STORAGE_IDENTIFIER + '_TOKEN');
- if (token) {
- this.TOKEN = token;
- let base64Url = this.TOKEN.split('.')[1];
- let base64 = base64Url.replace('-', '+').replace('_', '/');
- JSON.parse(window.atob(base64));
- console.log('base64', JSON.parse(window.atob(base64)))
- }
- //
- // this.storage.get(this.STORAGE_IDENTIFIER).then(auth => {
- // if(auth) {
- // this.TOKEN = auth.token;
- // console.log(auth, 'wru token');
- // }
- // });
- }
- /**
- * Login
- *
- * @param email {string}
- * @param password {string}
- * @returns {Observable<R>}
- */
- login(email: string, password: string): Observable<any> {
- let url = [
- this.endpoints.root.production,
- this.endpoints.usersProperties.login
- ].join('/');
- let payload = {
- email: email,
- password: password,
- };
- return this.authHttpService.post(url, payload)
- .map(response => {
- let body = response.json();
- console.log(body);
- this._setUser(body);
- console.log('body', body);
- return body;
- // return Observable.fromPromise(this._setUser(body));
- }
- )
- }
- // /**
- // * Logout
- // */
- // logout(): void {
- // this._clearUser();
- // }
- resetPassword(email: string) {
- let url = [
- this.endpoints.root.production,
- this.endpoints.usersProperties.resetPassword
- ].join('/');
- let payload = {email: email};
- return this.authHttpService.post(url, payload)
- .map(response => {
- let body = response.json();
- return body;
- })
- }
- registerUser(name, password, email, locale, country) {
- console.log('inside auth service');
- let url = [
- this.endpoints.root.production,
- this.endpoints.usersProperties.createUser
- ].join('/');
- let payload = {email: email, password: password, name: name, locale: locale, country: country};
- return this.authHttpService.post(url, payload)
- .map(response => {
- let body = response.json();
- return body;
- })
- }
- /**
- *
- * @returns {Observable<R>}
- */
- me(): Observable<any> {
- if (!this.TOKEN) {
- return Observable.throw(new Error('MISSING_TOKEN'));
- }
- let url = [
- this.endpoints.root.production,
- this.endpoints.usersProperties.me
- ].join('/');
- return this.authHttpService.get(url, this.TOKEN)
- .map(response => {
- let body = response.json();
- this._setUser(body);
- return body;
- })
- // .catch(this._handleError.bind(this));
- }
- //
- // updateMe(user: User) {
- //
- // if(!this.TOKEN) {return}
- //
- // let url = [
- // this.endpoints.root.production,
- // // this.endpoints.frontendUser.editUser
- // ].join('/');
- //
- // return this.authHttpService.post(url, user)
- // .map(response => {
- //
- // let body = response.json();
- //
- // this._setUser(body);
- //
- // return body;
- //
- // })
- // .catch(this._handleError.bind(this));
- //
- // }
- // updatePassword(user: User){
- // if(!this.TOKEN) {return}
- //
- // let url = [
- // this.endpoints.root.production,
- // // this.endpoints.frontendUser.editPassword
- // ].join('/');
- //
- //
- // return this.authHttpService.patch(url, user)
- // .map(response => {
- //
- // let body = response.json();
- //
- // this._setUser(body);
- //
- // return body;
- //
- // })
- // .catch(this._handleError.bind(this));
- //
- //
- //
- // }
- /**
- * Check if the user is authenticated based on their auth token being present in Storage
- * @returns {Promise<boolean>|PromiseLike<boolean>}
- */
- isAuthenticated() {
- const token = localStorage.getItem(this.STORAGE_IDENTIFIER + '_TOKEN');
- return !!token;
- }
- /**
- * Helper method for updating user and authentication state
- * @param data
- * @returns {Observable<T>}
- * @private
- */
- private _setUser(data) {
- let user = Object.assign(new User(), data);
- this.TOKEN = user.token;
- this._currentUser.next(user);
- localStorage.setItem(this.STORAGE_IDENTIFIER + '_TOKEN', this.TOKEN);
- }
- /**
- * Refresh auth token
- * @returns {any}
- */
- public refreshToken() {
- if (!this.TOKEN) {
- return Observable.throw(new Error('MISSING_TOKEN'));
- }
- let url = [
- this.endpoints.root.production,
- this.endpoints.usersProperties.refreshToken
- ].join('/');
- return this.authHttpService.patch(url,'', this.TOKEN)
- .map(response => {
- let body = response.json();
- console.log('body refresh token', body);
- // this._setUser(body);
- return body;
- })
- }
- //
- // /**
- // * Helper method for clearing user and authentication state
- // * @private
- // */
- // private _clearUser() {
- //
- // this.TOKEN = undefined;
- // // localStorage.removeItem(this.STORAGE_IDENTIFIER + '_TOKEN');
- // this._currentUser.next(null);
- //
- // }
- //
- // /**
- // * Helper method for handling error responses
- // * @param error
- // * @returns {ErrorObservable}
- // * @private
- // */
- // private _handleError(error) {
- //
- // let body = error.json();
- //
- // let response = body.errors || {};
- //
- // console.error(response);
- //
- // return Observable.throw(response);
- //
- // }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement