Как сохранить значение cookie в качестве переменной в Ionic 3 WordPress API - - PullRequest
0 голосов
/ 19 ноября 2018

У меня есть ионное приложение, говорящее с моим WordPress. Мне нужно было использовать конкретную конечную точку на определенной странице. Конечной точке требуется 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

...