3

I'm looking for a more detailed description of what is required for my endpoint for Ionic's custom Authentication. Ionic Custom Authentication. If possible a sample project or a tutorial to follow. I've looked extensively however, either my googling skills have died or there's nothing more out there (from what I can see).

I've tested this with the 'basic' setting, but need to connect with my own endpoint through ionic. As seen below:

    Ionic.Auth.login('custom', options, details).then(function (success) {
        console.log('Ionic Login Success:');
        console.log(success);
    }, function (failure) {
        console.error('Ionic Login Failure: ' + failure.response.body.error.message);
    });

Some Background: I have a Ionic Cordova app that can connect/authenticate to my WCF Web API (by doing some hack of a login/authentication process) and then retrieving data for the logged in user. I need to convert this to Ionic Auth. PS. I have no Experience with JWT and have gotten as far as installing the System.IdentityModel.Tokens.Jwt Package on my WCF service.

Any information is appreciated. Thanks in advance!

I know the question lacks some details but I can't give more until I know more

1 Answer 1

2

The Ionic team added three sample projects that demonstrate the custom-auth backends. Those three are express, flask and golang. I managed to set up a backend for CakePHP 2.6.x by looking at the server part of the backend.

You should be able to put together something as it is not magic. I added some PHP snipplets that should show you how you can make it in your specific language:

  • Get the redirect_uri parameter from the url and save it
  • Get the state parameter from the url and save it
  • Get the token parameter from the url and save it
  • Save your shared secret into a variable
  • Decode the JWT token using HS256 algorithm

The structure of the decoded token should look like this:

"decoded_array": {
   "data": {
     "password": "someString",
     "email": "[email protected]"
   },
   "exp": 1457731231,
   "app_id": "your app id"
}
  • Now check if you have a user in your database (do the authentication)
  • If authentication was not successful, return a UnauthorizedException (401)
  • If the authentication was successful, create a new variable called payload. This should contain your user_id from your database:

    $payload = array('user_id' => $user['User']['id']);
    
  • Now encode payload with your sharedSecret string by creating a new JWT token:

$outgoingToken = JWT::encode($payload, $sharedSecret);


Check out this Github-project: https://github.com/driftyco/custom-auth-examples

You can also check out my PHP implementation here: https://www.doonot.com/custom-auth-backend-for-ionic-auth-using-cakephp/

Sign up to request clarification or add additional context in comments.

5 Comments

I followed you tutorial, which is pretty nice btw, and redirected with the php header("Location: $url") - the problem is that i get redirected in the app as well. so i see the "message": "You have successfully implemented custom authentication!" in the app as well
Just remove &redirect_uri=https://api.ionic.io/auth/integrations/custom/success from the url. This is only for debugging. After you remove it, you should be redirected to your app.
@doonot when returning a 401 for auth errors it just seems to open the in app browser and do nothing. Any ideas?
Is it possible to get one with laravel
@ballerz I posted part of a laravel 3 controller method in my question about a problem, similar to the one MattSull is describing: stackoverflow.com/questions/43346272/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.