如何在Ionic 3 WordPress API中将Cookie值存储为变量-

时间:2018-11-19 12:41:28

标签: wordpress ionic-framework ionic3 wordpress-rest-api

我有我的离子应用程序正在与WordPress对话。我需要在特定页面上使用特定端点。端点需要 cookie ,我已经使用端点生成了它。我设法将cookie存储在离子存储中,也可以将其作为字符串变量进行检索。身份验证产生的挑战是 cookie cookie_name 。我只想获取下图所示的cookie值,以便可以将其与另一个页面中的另一个端点一起使用。

此外,我想在烤面包上打印错误(如果有的话)。 如果有其他最好的方法,我会很感激。

import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams, ToastController, AlertController, Events } from 'ionic-angular';
import { HttpClient } from '@angular/common/http';
import { Storage } from '@ionic/storage';

/**
 * Generated class for the LoginPage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

@IonicPage()
@Component({
  selector: 'page-login',
  templateUrl: 'login.html',
})
export class LoginPage {

  username: string;
  password: string;
  cooks: any;

  constructor(
    public navCtrl: NavController, 
    public navParams: NavParams, 
    public http: HttpClient, 
    public toastCtrl: ToastController, 
    public storage: Storage, 
    public alertCtrl: AlertController, 
    public events: Events
    ) {

    this.username = "";
    this.password = "";

  }

  login(){
//this will generate and uthenticate cookie

    this.http.get("http://academy.soutechapps.com/api/auth/generate_auth_cookie/?insecure=cool&username=" + this.username + "&password=" + this.password)
    .subscribe(data => {
      console.log(data);

      let response = data;
      this.res = response;
      console.log("this is expected cookie", this.res);


//I want to throw error on toast controller here
/*       if(response.error){
        this.toastCtrl.create({
          message: response.error,
          duration: 5000
        }).present();
        return;
      } */


      this.storage.set("userLoginInfo", response).then( (data) =>{

        this.alertCtrl.create({
          title: "Login Successful",
          message: "You have been logged in successfully.",
          buttons: [{
            text: "OK",
            handler: () => {

              this.events.publish("updateMenu");

              if(this.navParams.get("next")){
                this.navCtrl.push(this.navParams.get("next"));
              } else {
                this.navCtrl.pop();
              }             
            }
          }]
        }).present();

     })


    });

  }


I want to use the cookie with this endpoint but in another page
/*   ionViewDidLoad(){
    this.http.get("http://academy.soutechapps.com/api/auth/get_currentuserinfo/?cookie=" + this.res)
  .subscribe(usercookie=> {
    console.log(usercookie);

    //let response = data;
  }
  )} */
}

enter image description here

0 个答案:

没有答案