redirect almost working

This commit is contained in:
Jon Lambson 2017-08-10 16:33:54 -06:00
parent 3abdf1a606
commit 9788e73afb
3 changed files with 29 additions and 19 deletions

View File

@ -1,4 +1,5 @@
var app = angular.module('launchpad', ['ui.router', 'LocalStorageModule']); var app = angular.module('launchpad', ['ui.router', 'LocalStorageModule']);
var redirectedURL;
app.config(['$stateProvider', '$urlRouterProvider', 'localStorageServiceProvider', function($stateProvider, $urlRouterProvider, localStorageServiceProvider){ app.config(['$stateProvider', '$urlRouterProvider', 'localStorageServiceProvider', function($stateProvider, $urlRouterProvider, localStorageServiceProvider){
localStorageServiceProvider.setPrefix('launchpad').setStorageType('sessionStorage'); localStorageServiceProvider.setPrefix('launchpad').setStorageType('sessionStorage');
@ -9,13 +10,15 @@ app.config(['$stateProvider', '$urlRouterProvider', 'localStorageServiceProvider
.state('splash-page', { .state('splash-page', {
data: { 'requiresLogin': false }, data: { 'requiresLogin': false },
params: { params: {
'toState': 'launchpad-home', // default state to proceed to after login // 'toState': 'launchpad-home',
'toParams': {} 'toParams': {
}
}, },
url: '/splash-page', url: '/splash-page',
templateUrl: '/templates/splash-page.html', templateUrl: '/templates/splash-page.html',
}) })
.state('app',{ .state('app',{
data: { 'requiresLogin': true },
url: '/launchpad-', url: '/launchpad-',
views: { views: {
'header': { 'header': {
@ -33,11 +36,6 @@ app.config(['$stateProvider', '$urlRouterProvider', 'localStorageServiceProvider
templateUrl: '/templates/partials/notifications.html' templateUrl: '/templates/partials/notifications.html'
} }
}, },
data: { 'requiresLogin': true },
params: {
'toState': '',
'toParams': {}
}
}) })
.state('app.home', { .state('app.home', {
url: 'home', url: 'home',
@ -129,7 +127,6 @@ app.config(['$stateProvider', '$urlRouterProvider', 'localStorageServiceProvider
app.run(['$rootScope', '$state', 'Auth', function($rootScope, $state, Auth) { app.run(['$rootScope', '$state', 'Auth', function($rootScope, $state, Auth) {
// Change title based on the `data` object in routes
$rootScope.$on('$stateChangeStart', function(event, toState, toParams, fromState, fromParams) { $rootScope.$on('$stateChangeStart', function(event, toState, toParams, fromState, fromParams) {
console.log('toState', toState); console.log('toState', toState);
console.log('toParams', toParams); console.log('toParams', toParams);
@ -138,17 +135,20 @@ app.run(['$rootScope', '$state', 'Auth', function($rootScope, $state, Auth) {
var requiresLogin = toState.data.requiresLogin; var requiresLogin = toState.data.requiresLogin;
if (requiresLogin && !Auth.isLoggedIn()) { redirectedURL = toState.url;
event.preventDefault();
$state.go('splash-page', {'toState': toState.name, 'toParams': toParams}); console.log("%c" + redirectedURL, "color: yellow; font-size: 72px;");
if (Auth.isLoggedIn()) {
console.log('yes');
} else { } else {
// if ($state.params.toState === undefined && $state.params.toParams === undefined) { console.log('not logged in');
// console.log('set a route');
// }
console.log('TOSTATE: ',$state.params.toState);
console.log('TOPARAMS: ',$state.params.toParams);
// $state.go($state.params.toState, $state.params.toParams);
} }
if (requiresLogin && !Auth.isLoggedIn()) {
event.preventDefault();
$state.go('splash-page');
// $state.go('splash-page', { 'toState': toState.name, 'toParams': toParams });
}
}); });
}]); }]);

View File

@ -10,5 +10,6 @@ app.controller('SignInController', ['$scope', 'Auth', '$location', 'localStorage
return localStorageService.set('userAuth', JSON.stringify(userInfo)); return localStorageService.set('userAuth', JSON.stringify(userInfo));
}(); }();
}; };
}]); }]);

View File

@ -1,8 +1,17 @@
app.factory('Auth', ['localStorageService', function(localStorageService){ app.factory('Auth', ['localStorageService', '$location', function(localStorageService, $location) {
var user; var user;
return{ return{
setUser : function(currentUser){ setUser : function(currentUser){
user = currentUser; user = currentUser;
console.log("%c" + redirectedURL, "color: red; font-size: 72px;");
if (redirectedURL === '/splash-page') {
$location.path('/launchpad-home');
} else {
$location.path('/launchpad-' + redirectedURL);
}
}, },
isLoggedIn : function(){ isLoggedIn : function(){
user = JSON.parse(localStorageService.get('userAuth')); user = JSON.parse(localStorageService.get('userAuth'));