Headers Cakephp 3 with Ionic 3

0

Update: Trying to debug a bit more I found this return as print below. But I followed the steps of the documentation of the plugin git itself. Anyone who can help from now on thanks.

**CAKEPHPCode3**

<?php

/** *CakePHP(tm):RapidDevelopmentFramework( link )  * Copyright (c) Cake Software Foundation, Inc. ( link )  *  * Licensed under The MIT License  * For full copyright and license information, please see the LICENSE.txt  * Redistributions of files must retain the above copyright notice.  *  * @copyright Copyright (c) Cake Software Foundation, Inc. ( link )  * @link link CakePHP (tm) Project  * @since 0.2.9  * @license link MIT License  * / namespace App \ Controller;

use Cake \ Controller \ Controller; use Cake \ Event \ Event;

/ **  * Application Controller  *  * Add your application-wide methods in the class below, your controllers  * will inherit them.  *  * @link link  * / class AppController extends Controller {     public $ helpers = ['CkEditor.Ck'];

/**
 * Initialization hook method.
 *
 * Use this method to add common initialization code like loading components.
 *
 * e.g. '$this->loadComponent('Security');'
 *
 * @return void
 */
public function initialize()
{
    parent::initialize();

    $this->loadComponent('RequestHandler');
    $this->loadComponent('Flash');
    $this->loadComponent('Auth', [
        'loginRedirect' => [
            'controller' => 'Releases', 'action'=>'index'
        ],
        'logoutRedirect' => [
            'controller' => 'Users',
            'action' => 'login'
        ],
        'authError' => 'Faça o login primeiramente',
        'authenticate' => [
            'Form' => [
                'fields' => ['username' => 'email', 'password' => 'password']
            ],
            'ADmad/JwtAuth.Jwt' => [
                'userModel' => 'Users',
                'fields' => [
                    'username' => 'id',
                ],
                 'parameter' => 'token',
            ],
        ],
        'unauthorizedRedirect' => false,

    ]);

    $this->Auth->allow(['edit', 'login','auth']);

    /*
     * Enable the following components for recommended CakePHP security settings.
     * see https://book.cakephp.org/3.0/en/controllers/components/security.html
     */
    //$this->loadComponent('Security');
    //$this->loadComponent('Csrf');
}

/**
 * Before render callback.
 *
 * @param \Cake\Event\Event $event The beforeRender event.
 * @return \Cake\Http\Response|null|void
 */
public function beforeRender(Event $event)
{
    //var_dump($this->request->getHeaders());die;

    if ($this->request->is('json') || $this->request->is('options') || !array_key_exists('_serialize', $this->viewVars) &&
        in_array($this->response->type(), ['application/json'])
    ) {

        $this->setCors();

        $this->response->withHeader('Access-Control-Allow-Origin', '*');
        $this->response->withHeader('Access-Control-Allow-Credentials', 'true');
        $this->response->withHeader("Access-Control-Allow-Headers", "Content-Type, Access-Control-Allow-Origin, Access-Control-Allow-Headers, Authorization, X-Requested-With");
        $this->response->withHeader('Access-Control-Allow-Methods', 'POST, GET, OPTIONS, PUT');
        $this->response->withHeader('Access-Control-Max-Age','172800');
        $this->response->withHeader('Accept','application/json');
        $this->response->withHeader('Content-Type','application/json');
        $this->set('_serialize', true);
        return parent::beforeRender($event);
        //$this->RequestHandler->renderAs($this, 'json');
    }
}

public function beforeFilter(Event $event)
{

    if($this->request->params['_ext'] === 'json' || $this->request->is('options'))
    {
        $this->setCors();
        $this->response->withHeader('Access-Control-Allow-Origin', '*');
        $this->response->withHeader('Access-Control-Allow-Credentials', 'true');
        $this->response->withHeader("Access-Control-Allow-Headers", "*");
        $this->response->withHeader('Access-Control-Allow-Methods', 'POST, GET, OPTIONS, PUT');
        $this->response->withHeader('Access-Control-Max-Age','172800');
        $this->response->withHeader('Accept','application/json');
        $this->response->withHeader('Content-Type','application/json');

    }
    return parent::beforeFilter($event);

}

public function setCors(){

        return  $this->response->cors($this->request)
        ->allowOrigin(['http://localhost:8100 always'])
        ->allowMethods(['*'])
        ->allowHeaders(['authorization'])
        ->allowCredentials(['false'])
        ->exposeHeaders(['Link'])
        ->maxAge(172800)
        ->build();


}

}

Good evening,

Next I did the authentication with JWT + CAKEPHP 3 works successfully returning the token however on the next request it would be to bring the user's personal data or another link that needs to pass the token is returning just link . I tested on Postmam by running the token successfully returned the .

Code

getUser(){
    console.log(this.token);
    if(this.user!=null){
        this.storage.get("token").then((val) =>{
            this.headers= this.headers.set("Authorization", 'Bearer ${val}').set("Content-type", "application/json")
    .set('Access-Control-Allow-Origin', '*')
    .set('Access-Control-Allow-Methods', '*');;
        });

        console.log(this.headers);
        return new Promise(resolve => {
            this.http.get(this.url+"users/"+ this.user.id +".json", {responseType: 'json'}, {headers:this.headers})
            //.map(res => res.json().data)
            .subscribe(data => {
              data = data;
              resolve(data);
              console.log('success');
            })


          });
    }

    
asked by anonymous 10.04.2018 / 03:06

0 answers