存储不存储(AuthHttp)

时间:2018-03-30 15:50:54

标签: angular ionic2 ionic3 storage authhttp

存储不再设置我的id_token

它一直在工作,我试图从api插入一个图像,过了一会儿就停止了工作。 我没有得到任何错误,并尝试过一切。 我的console.log在this.storage.get('id_token')中。然后(token => {...});做不了。

在overview.ts文件中,我执行相同的检查(从存储中获取'id_token')以进入(ionViewCanEnter)。

它只适用于我的Android设备上的离子开发应用程序(推送页面),但.apk和浏览器版本似乎以某种方式被破坏。

以下是所需的代码块

LoginService.ts

import { Http } from '@angular/http';
import { Storage } from '@ionic/storage';
import { Injectable } from '@angular/core';
import { App } from 'ionic-angular';
import 'rxjs/add/operator/map';

@Injectable()
export class LoginServiceProvider {

apiUrl = 'https://api.example.be';  

constructor(public http: Http, public storage: Storage, public appCtrl: App) {
}

login(body) {

    return new Promise((resolve, reject) => {
        this.http.post(this.apiUrl+'/get_token', body)
        .subscribe(data => {

            let token = data.json();
            this.storage.set('id_token', token);

            this.storage.get('id_token').then(token => {
                    console.log('got token');
                if(token) {
                    console.log('token exists');
                } else {
                    console.log('token does not exist');
                }
            });

        })
    });
  }

}

Login.ts

import { LoginServiceProvider } from './../../providers/login-service/login-service';
import { Component } from '@angular/core';
import { NavController } from 'ionic-angular';
import { RegisterPage } from '../register/register';
import { OverviewPage } from '../overview/overview';
import { Storage } from '@ionic/storage';
import { LoadingController } from 'ionic-angular';

@Component({
  selector: 'page-login',
  templateUrl: 'login.html'
})
export class LoginPage {
  userData;
  apiUrl = 'https://api.example.be';  

  constructor(public navCtrl: NavController, private loginService: LoginServiceProvider, public storage: Storage, public loadingCtrl: LoadingController) {

  }

  login() {

    this.loginService.login({email: "email@example.com", password: "examplepass"});

    return this.storage.get('id_token').then(token => {

     this.loadingCtrl.create({

        content: 'Logging in...',
        duration: 1000,
        dismissOnPageChange: true

      }).present();

      setTimeout(() => {

          // Does not work anymore
          this.navCtrl.push(OverviewPage).catch(err => {
          console.log('not allowed!');
        });

      }, 1000);


    });
  }

  goToRegister() {
    this.navCtrl.push(RegisterPage);
  }

  forgotPassword() {

  }
}

login.html(重要的是)

<button ion-button full color="orange" (click)="login()">Login</button>

app.module.ts

import { BrowserModule } from '@angular/platform-browser';
import { ErrorHandler, NgModule } from '@angular/core';
import { IonicApp, IonicErrorHandler, IonicModule } from 'ionic-angular';
import { SplashScreen } from '@ionic-native/splash-screen';
import { StatusBar } from '@ionic-native/status-bar';

import { MyApp } from './app.component';
import { OverviewPage } from '../pages/overview/overview';
import { LoginPage } from '../pages/login/login';
import { ProfilePage } from '../pages/profile/profile';
import { ProfilePageModule } from '../pages/profile/profile.module';



import { AuthHttp, AuthConfig } from 'angular2-jwt';
import { Http } from '@angular/http';
import { HttpModule } from '@angular/http';
import { HttpClientModule } from '@angular/common/http';
import { Storage, IonicStorageModule } from '@ionic/storage';
import { LoginServiceProvider } from '../providers/login-service/login-service';


export function getAuthHttp(http, storage) {

  return new AuthHttp(new AuthConfig({
    noJwtError: true,
    globalHeaders: [{'Accept': 'application/json'}],
    tokenGetter: (() => storage.get('id_token')),
  }), http);
}

@NgModule({
  declarations: [
    MyApp,
    LoginPage,
    OverviewPage  
  ],
  imports: [
    BrowserModule,
    IonicModule.forRoot(MyApp),
    HttpModule,
    HttpClientModule,
    IonicStorageModule.forRoot(),
    ProfilePageModule
  ],
  bootstrap: [IonicApp],
  entryComponents: [
    MyApp,
    OverviewPage,
    LoginPage,
    ProfilePage
  ],
  providers: [
    StatusBar,
    SplashScreen,
    {provide: ErrorHandler, useClass: IonicErrorHandler},
    {
      provide: AuthHttp,
      useFactory: getAuthHttp,
      deps: [Http, Storage]
    },
    LoginServiceProvider
  ]
})
export class AppModule {}

1 个答案:

答案 0 :(得分:0)

[解决]

存储令牌可能存在问题,一切都可以在不调整1行代码的情况下再次运行。 令牌可能已过期,存储设置了一个新的,很奇怪,但我很高兴它再次起作用。