### File

`src/app/core/services/auth.service.ts`

### Constructor

`constructor(http: HttpService, actions: AuthActions, store: Store)`
Defined in src/app/core/services/auth.service.ts:11
Creates an instance of AuthService. Parameters : http : `HttpService` actions : `AuthActions` store : `Store`

### Methods

login
`login(data: any)`
Defined in src/app/core/services/auth.service.ts:37
Parameters : data Returns: `Observable<any>` *

register
`register(data: any)`
Defined in src/app/core/services/auth.service.ts:70
Parameters : data Returns: `Observable<any>` *

authorized
`authorized()`
Defined in src/app/core/services/auth.service.ts:102
Returns: `Observable<any>` *

logout
`logout()`
Defined in src/app/core/services/auth.service.ts:119
Returns: `void` *

Private setTokenInLocalStorage
`setTokenInLocalStorage(user_data: any)`
Defined in src/app/core/services/auth.service.ts:137
Parameters : user_data Returns: `void`

    import { Injectable } from '@angular/core';
    import { environment } from '../../../environments/environment';
    import { Observable } from 'rxjs/Observable';
    import { Response } from '@angular/http';
    import { HttpService } from './http';
    import { AppState } from '../../interfaces';
    import { Store } from '@ngrx/store';
    import { AuthActions } from '../../auth/actions/auth.actions';

    @Injectable()
    export class AuthService {

      /**
       * Creates an instance of AuthService.
       * @param {HttpService} http
       * @param {AuthActions} actions
       * @param {Store<AppState>} store
       *
       * @memberof AuthService
       */
      constructor(
        private http: HttpService,
        private actions: AuthActions,
        private store: Store<AppState>
      ) {

      }

      /**
       *
       *
       * @param {any} data
       * @returns {Observable<any>}
       *
       * @memberof AuthService
       */
      login(data): Observable<any> {
        return this.http.post(
          'spree/login.json',
          { spree_user: data }
        ).map((res: Response) => {
          data = res.json();
          if (!data.error) {
            // Setting token after login
            this.setTokenInLocalStorage(data);
            this.store.dispatch(this.actions.loginSuccess());
          } else {
            this.http.loading.next({
              loading: false,
              hasError: true,
              hasMsg: 'Please enter valid Credentials'
            });
          }
          return data;
        });
        // catch should be handled here with the http observable
        // so that only the inner obs dies and not the effect Observable
        // otherwise no further login requests will be fired
        // MORE INFO https://youtu.be/3LKMwkuK0ZE?t=24m29s
      }

      /**
       *
       *
       * @param {any} data
       * @returns {Observable<any>}
       *
       * @memberof AuthService
       */
      register(data): Observable<any> {
        return this.http.post(
          'api/account',
          { spree_user: data }
        ).map((res: Response) => {
          data = res.json();
          if (!data.errors) {
            // Setting token after login
            this.setTokenInLocalStorage(res.json());
            this.store.dispatch(this.actions.loginSuccess());
          } else {
            this.http.loading.next({
              loading: false,
              hasError: true,
              hasMsg: 'Please enter valid Credentials'
            });
          }
          return res.json();
        });
        // catch should be handled here with the http observable
        // so that only the inner obs dies and not the effect Observable
        // otherwise no further login requests will be fired
        // MORE INFO https://youtu.be/3LKMwkuK0ZE?t=24m29s
      }

      /**
       *
       *
       * @returns {Observable<any>}
       *
       * @memberof AuthService
       */
      authorized(): Observable<any> {
        return this.http
          .get('spree/api/v1/users')
          .map((res: Response) => res.json());
        // catch should be handled here with the http observable
        // so that only the inner obs dies and not the effect Observable
        // otherwise no further login requests will be fired
        // MORE INFO https://youtu.be/3LKMwkuK0ZE?t=24m29s
      }

      /**
       *
       *
       * @returns
       *
       * @memberof AuthService
       */
      logout() {
        return this.http.get('spree/logout.json')
          .map((res: Response) => {
            // Setting token after login
            localStorage.removeItem('user');
            this.store.dispatch(this.actions.logoutSuccess());
            return res.json();
          });
      }

      /**
       *
       *
       * @private
       * @param {any} user_data
       *
       * @memberof AuthService
       */
      private setTokenInLocalStorage(user_data): void {
        const jsonData = JSON.stringify(user_data);
        localStorage.setItem('user', jsonData);
      }
    }

# results matching ""

# No results matching ""