HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavascriptMinor

Ionic REST + Authentication

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
ionicauthenticationrest

Problem

I have an app that makes calls to the Podio API. The Podio Api uses OAuth2 protocol , after user's authenticate with their podio username and password an access and refresh token is provided for subsequent requests. Podio has a Javascript SDK that handles authentication and API calls. I wrote a simple Session Manager utilizing the $localStorage object.

The app does the following:

  • Allows Users to Login with their Podio credentials



  • Makes an API call to get events list and allows users to click on event to get event details



  • Makes an API call to get attendee list for specific event and attendee details



  • Can add attendees to an event by scanning a Barcode Number



The app currently works and uses the Cordova Barcode Scanner Plugin. I am currently using Podio's authentication check everytime I transition from state to state to see if User is logged in. If not I send them to the login screen. This is placed in every controller. I am currently making an API call everytime a controller is loaded as well. I am looking for best practices for code construction, design, and how I could possibly make the code more readable in angular. Also, if my current way of checking authentication every time is correct and ways to reduce API calls. The latest code can be seen in my github repository here. But here are the main files (not including the events and attendees services):

Main app.js file

```
/**
* The main Freedom Nation app module
*
* @type {angular.Module}
*/

angular.module('freedomnation', ['ionic', 'ngCordova','ngMessages', 'freedomnation.controllers', 'freedomnation.services','freedomnation.filters'])

.run(function($ionicPlatform,$rootScope,$state,$stateParams, $http,$localstorage) {

/*
Local Storage Object Used/
$localstorage.get('password', function (token) {
$http.defaults.headers.common['Authorization'] = 'OAuth2 ' + token.accessToken;
});

$rootScope.$state = $state;
$rootScope.$stateParams = $statePa

Solution

The code looks pretty good to me.

I am rather new to AngularJS myself, but I did some a spot where you could get rid of some redundancy in your attendee.controller.client.js

change this

$scope.attending = true;
    if($stateParams.attending == false) {
        $scope.attending = false;
    }


to a single line of code

$scope.attending = $stateParams.attending;

Code Snippets

$scope.attending = true;
    if($stateParams.attending == false) {
        $scope.attending = false;
    }
$scope.attending = $stateParams.attending;

Context

StackExchange Code Review Q#111952, answer score: 4

Revisions (0)

No revisions yet.