Connecting an Android app with CakePhp website

Is it possible to link an Android app with cakePhp and share data? If possible, I want to create an application that can enter the site; I doubt:

  • How to transfer username and password from our application to cakephp login page? Can someone show me an example program?

  • How does the cakephp controller handle this request and respond to this request? Please show me an example program?

(I am new to android and cakephp.)

+4
source share
3 answers

Use Admad JWT Auth Plugin

If you are using cakephp3, change your login function as follows:

public function token() { $user = $this->Auth->identify(); if (!$user) { throw new UnauthorizedException('Invalid username (email) or password'); } $this->set([ 'success' => true, 'data' => [ 'token' => JWT::encode([ 'sub' => $user['id'], 'exp' => time() + 604800 ], Security::salt()) ], '_serialize' => ['success', 'data'] ]); } 

You can read this tutorial about REST Api and JWT Auth Implementation.

http://www.bravo-kernel.com/2015/04/how-to-add-jwt-authentication-to-a-cakephp-3-rest-api/

+2
source

The quick answer is YES!

We just finished pushing the Android app to the market, which makes it that way. Here's how we did it:

1) Download and learn about using Cordova PhoneGap (2.2.0 - latest version) in Eclipse. This makes it easy to work with just some HTML and lots of Javascript.

2) In your JS, create methods that push out the login data using AJAX parameters. Example:

 document.addEventListener('deviceready', onDeviceReady, false); function onDeviceReady() { $("#login").click(function() { $email = $("#UserEmail").val(); $pass = $("#UserPassword").val(); $.ajax({ url : yourURL + 'api/users/login', async : false, data : { 'email' : $email, 'password' : $pass }, dataType : 'json', type : 'post', success : function(result) { /** * do your login redirects or * use localStorage to store your data * on the phone. Keep in mind the limitations of * per domain localStorage of 5MB */ // you are officially "logged in" window.location.href = "yourUrl.html"; return; }, error : function(xhr, status, err) { // do your stuff when the login fails } }); } } 

3) In Cake / PHP, your user controller here will use the username and password data in the AJAX call and use it to authenticate it.

 <?php class UsersController extends AppController { public $name = 'Users'; public function beforeFilter() { parent::beforeFilter(); $this->Auth->allow('api_login'); } public function api_login() { $this->autoRender = false; if ($this->request->data && isset($this->request->data['email']) && isset($this->request->data['password'])) { $arrUser = $this->User->find('all',array( 'conditions'=>array( 'email'=> $this->request->data['email'], 'password' => $this->Auth->password($this->request->data['password']), ) ) ); if (count($arrUser) > 0) { $this->Session->write('Auth.User',$arrUser[0]['User']); // Do your login functions $arrReturn['status'] = 'SUCCESS'; $arrReturn['data'] = array( 'loginSuccess' => 1,'user_id' => $arrUser[0]['User']['id'] ); } else { $arrReturn['status'] = 'NOTLOGGEDIN'; $arrReturn['data'] = array( 'loginSuccess' => 0 ); } } else { $arrReturn['status'] = 'NOTLOGGEDIN'; $arrReturn['data'] = array( 'loginSuccess' => 0 ); } echo json_encode($arrReturn); } } ?> 

This is pretty much the case. You are now authorized by CakePHP.

You do not need to use "api_", you can use any function name that you want, but this helped us to cope with what we allowed mobile users to do with web users.

Now these are just building blocks. You basically need to create an entire version of your site over the phone using HTML and Javascript, so depending on your application it might be easier to just create a responsive design on your site and allow viewing on mobile devices.

NTN!

+8
source

if you rebuild most of the browsing pages in cakephp in ajax, it seems to overcome the goals of using cakephp as such.

-1
source

All Articles