Created
April 16, 2016 22:57
-
-
Save AlphaNerd/4de859c30a0bba69fbbcbdb7a7f618d9 to your computer and use it in GitHub Desktop.
Solid framework for secure content, network connection status, and general route setup
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Ionic Starter App | |
// angular.module is a global place for creating, registering and retrieving Angular modules | |
// 'starter' is the name of this angular module example (also set in a <body> attribute in index.html) | |
// the 2nd parameter is an array of 'requires' | |
// 'starter.controllers' is found in controllers.js | |
angular.module('starter', ['ionic', 'starter.controllers', 'starter.services', 'ngCordova']) | |
.run(function($ionicPlatform, $rootScope, $cordovaNetwork, $state, $authservice) { | |
$ionicPlatform.ready(function() { | |
// Hide the accessory bar by default (remove this to show the accessory bar above the keyboard | |
// for form inputs) | |
if (window.cordova && window.cordova.plugins.Keyboard) { | |
cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true); | |
cordova.plugins.Keyboard.disableScroll(true); | |
} | |
if (window.StatusBar) { | |
// org.apache.cordova.statusbar required | |
StatusBar.styleDefault(); | |
} | |
////// ACCESS CONTROL //////// | |
if (!$authservice.isAuthenticated()){ | |
// User isn’t authenticated | |
$state.transitionTo("login"); | |
event.preventDefault(); | |
} | |
$rootScope.$on("$stateChangeStart", function(event, toState, toParams, fromState, fromParams){ | |
if (toState.authenticate && !$authservice.isAuthenticated()){ | |
// User isn’t authenticated | |
console.log("You are not permitted to access this resource.") | |
$state.transitionTo("login"); | |
event.preventDefault(); | |
} | |
}); | |
///// NETWORK STATUS ////// | |
var type = $cordovaNetwork.getNetwork() | |
var isOnline = $cordovaNetwork.isOnline() | |
var isOffline = $cordovaNetwork.isOffline() | |
// listen for Online event | |
$rootScope.$on('$cordovaNetwork:online', function(event, networkState){ | |
var onlineState = networkState; | |
console.log(onlineState) | |
}) | |
// listen for Offline event | |
$rootScope.$on('$cordovaNetwork:offline', function(event, networkState){ | |
var offlineState = networkState; | |
console.log(offlineState) | |
}) | |
}); | |
}) | |
.config(function($stateProvider, $urlRouterProvider) { | |
$stateProvider | |
.state('login', { | |
url: '/login', | |
templateUrl: 'templates/login.html', | |
controller: 'LoginCtrl', | |
authenticate: false | |
}) | |
.state('app', { | |
url: '/app', | |
abstract: true, | |
templateUrl: 'templates/menu.html', | |
controller: 'AppCtrl', | |
authenticate: true | |
}) | |
.state('app.search', { | |
url: '/search', | |
views: { | |
'menuContent': { | |
templateUrl: 'templates/search.html' | |
} | |
}, | |
authenticate: true | |
}) | |
.state('app.browse', { | |
url: '/browse', | |
views: { | |
'menuContent': { | |
templateUrl: 'templates/browse.html', | |
controller: 'BrowseCtrl' | |
} | |
}, | |
authenticate: true | |
}) | |
.state('app.playlists', { | |
url: '/playlists', | |
views: { | |
'menuContent': { | |
templateUrl: 'templates/playlists.html', | |
controller: 'PlaylistsCtrl' | |
} | |
}, | |
authenticate: true | |
}) | |
.state('app.single', { | |
url: '/playlists/:playlistId', | |
views: { | |
'menuContent': { | |
templateUrl: 'templates/playlist.html', | |
controller: 'PlaylistCtrl' | |
} | |
}, | |
authenticate: true | |
}); | |
// if none of the above states are matched, use this as the fallback | |
$urlRouterProvider.otherwise('/app/browse'); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment