angularjs - Two Step authentication in IONIC -


authentication flow chart

firstly no pro, started ionic framework.

i trying accomplish authentication shown in image above.

what trying do?

firstly, on loading app, checks if user logged in or not. if user logged in, he/she redirected dashboard.

if user not logged in, system check if user waiting verification code.

if user waiting verification code, input verification form displayed.

else, user directed input username form.

what have done far?

have created 3 controllers username input, verification code , dashboard. service handles back-end communication server , stores authentication key , necessary user credentials on local storage.
using php slimframework rest api.

my question

  1. how check if user logged in or waiting verification code, when ionic app loads.

how check if user logged-in

you can check if user logged-in checking if token key exists in local storage so:

     angular.module('starter')      .service('authservice', function($q, $http, user_roles) {         var local_token_key = 'yourtokenkey';         var username = '';         var isauthenticated = false;         var role = '';         var authtoken;      function loadusercredentials() {         var token = window.localstorage.getitem(local_token_key);         if (token) {           usecredentials(token);         }     }      function storeusercredentials(token) {         window.localstorage.setitem(local_token_key, token);         usecredentials(token);     }      function usecredentials(token) {         username = token.split('.')[0];         isauthenticated = true;         authtoken = token;          if (username == 'admin') {           role = user_roles.admin         }         if (username == 'user') {           role = user_roles.public         }          // set token header requests!         $http.defaults.headers.common['x-auth-token'] = token;     }      function destroyusercredentials() {         authtoken = undefined;         username = '';         isauthenticated = false;         $http.defaults.headers.common['x-auth-token'] = undefined;         window.localstorage.removeitem(local_token_key);     }      var login = function(name, pw) {     return $q(function(resolve, reject) {       if ((name == 'admin' && pw == '1') || (name == 'user' && pw == '1')) {         // make request , receive auth token server         storeusercredentials(name + '.yourservertoken');         resolve('login success.');       } else {         reject('login failed.');       }     });     };      var logout = function() {         destroyusercredentials();     };      var isauthorized = function(authorizedroles) {         if (!angular.isarray(authorizedroles)) {           authorizedroles = [authorizedroles];         }         return (isauthenticated && authorizedroles.indexof(role) !== -1);     };          loadusercredentials();          return {         login: login,         logout: logout,         isauthorized: isauthorized,         isauthenticated: function() {return isauthenticated;},         username: function() {return username;},         role: function() {return role;}     }; 

this typical example of user authentication service. here on loading app, check if authorized token exists, if doesn't means user logged out.

you can apply similar technique checking if user waiting verification code.

verification code technique

  1. there field in database set false once verification code has been sent out particular user.
  2. once user gets code , inputs form, checks code against database, if it's valid, marks verified field true.

you might want check out tutorial how handle authentication in ionic app.

if using ionic 2, can check out comprehensive tutorial. auth0 handles 2-factor authentication well.

i hope helps! cheers!


Comments

Popular posts from this blog

Spring Boot + JPA + Hibernate: Unable to locate persister -

go - Golang: panic: runtime error: invalid memory address or nil pointer dereference using bufio.Scanner -

c - double free or corruption (fasttop) -