Authentication in Ionic/Cordova App

AngularjsAuthenticationCouchdbAngular UiIonic Framework

Angularjs Problem Overview


First off, I'm no pro.

In my quest to become a better developer I am trying to understand what is needed and how to accomplish creating a sign-up/login for an Ionic-Framework app.

Most of the single-page-applications (SPAs) handle authentication on a node server that is also serving up the HTML for the client. In my case the phone itself will be serving up the HTML so I am guessing I may be going up against some CORs issues.

I understand that the Ionic-Framework uses states and based angular-client-side-auth repo I should be authenticating whenever I am changing states in my app.

I have an initial app setup but now I am kind of confused where to go from here.

The tools I have at my disposal:

  • Node.JS Server -Thanks DigitalOcean (Should I be using this as a proxy to my DB?)
  • CouchDB server (Full stack here we come)

Questions of mine:

  1. What is the standard approach for authenticating when using hybrid apps?
  2. Should I be using Node.JS as a proxy to the database?
  3. Should I skip node.js and authenticate directly with the CouchDB server? (I've heard of this)
  4. Am I going about this all the wrong way?
  5. What are my potential road-blocks?
  6. How does CORS work with hybrid applications?
  7. Anything I'm missing?

Thanks for helping me become a better developer.

Angularjs Solutions


Solution 1 - Angularjs

nathvarun gave a very complete answer, but I'd like to share the steps I do for authentication in my app.

  1. Send email + password via ajax to the server
  2. Generate a token in the server and send it back to the app
  3. Store email + token in localStorage
  4. For every single request I make to the server I send email + token via POST
  5. In the server I verify authenticity of that user with that token, if true the method is executed, if false I send back to the app an error (401)
  6. If app receives success, then it's ok, if receives error I redirect to login screen.

Nice thing is that when the app is open, you can get the email + token from localStorage, send to the server, if that token is ok for that user, redirect to main screen, otherwise redirect to login. Then whenever user clears the cache of the app, he is redirected to login screen.

Solution 2 - Angularjs

I actually needed something like that for a few apps I'm working on. I spent quite some time investigating this and was able to achieve that.

I'm pretty happy with the result, in addition to email/password authentication I've added some social authentication which works in the same way.

  1. open url on client side with the provider's (facebook/twitter/instagram) url for login
  2. the user logs in and is redirected to the server's callback url (my server is written in nodejs)
  3. once I've got the access token from the provider. I save this token and then create a token for the client to reuse every time the user wants to access a protected ressource.

Download the apk and test it.

If this is what you're looking for you can checkout both the client side code at : https://github.com/malikov/Authenticate.me-client-cordova-ionic

And the server side code at : https://github.com/malikov/Authenticate.me-Node-Server

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionTyMaynView Question on Stackoverflow
Solution 1 - AngularjsLucas GarciaView Answer on Stackoverflow
Solution 2 - AngularjsmalikovView Answer on Stackoverflow