import 'rxjs/add/operator/toPromise' ; import { Injectable } from '@angular/core' ; import { Storage } from '@ionic/storage' ; import { Api } from '../api/api' ; /**
 * Most apps have the concept of a User. This is a simple provider
 * with stubs for login/signup/etc.
 *
 * This User provider makes calls to our API at the `login` and `signup` endpoints.
 *
 * By default, it expects `login` and `signup` to return a JSON object of the shape:
 *
 * ```json
 * {
 *   status: 'success',
 *   data: {
 *     // User fields your app needs, like "id", "name", "email", etc.
 *   }
 * }Ø
 * ```
 *
 * If the `status` field is not `success`, then an error is detected and returned.
 */ @ Injectable ( ) export class User { _user : any ; _token : string ; constructor ( public api , private storage ) { this . init ( ) ; } /**
     * Send a POST request to our login endpoint with the data
     * the user entered on the form.
     */ login ( accountInfo ) { let seq = this . api . get ( 'login' , accountInfo ) . share ( ) ; seq . subscribe ( ( res ) => { if ( res . success ) { this . _loggedIn ( res ) ; } } , err => { console . error ( 'ERROR' , err ) ; } ) ; return seq ; } /**
     * Send a POST request to our signup endpoint with the data
     * the user entered on the form.
     */ signup ( accountInfo ) { let seq = this . api . post ( 'signup' , accountInfo ) . share ( ) ; seq . subscribe ( ( res ) => { if ( res . success ) { this . _loggedIn ( res ) ; } } , err => { console . error ( 'ERROR' , err ) ; } ) ; return seq ; } /**
     * Log the user out, which forgets the session
     */ logout ( ) { this . _user = null ; } /**
     * Process a login/signup response to store user data
     */ _loggedIn ( resp ) { this . _user = resp . data ; this . _token = resp . data . token ; this . storage . set ( "user" , this . _user ) ; } getToken ( ) { return this . storage . get ( "user" ) . then ( res => { if ( res ) { return res . token ; } } ) ; } init ( ) { this . storage . get ( "user" ) . then ( res => { if ( res ) { this . _user = res ; this . _token = res . token ; } } ) ; } refreshToken ( ) { } }