login and save localstorage

This commit is contained in:
Jon Lambson 2017-08-09 11:28:36 -06:00
parent a6344ce60c
commit de8798f01b
8 changed files with 37 additions and 28 deletions

View File

@ -29,6 +29,7 @@
<script src="/js/lib/angular/angular.min.js" charset="utf-8"></script> <script src="/js/lib/angular/angular.min.js" charset="utf-8"></script>
<script src="/js/lib/angular/angular-local-storage.js" charset="utf-8"></script> <script src="/js/lib/angular/angular-local-storage.js" charset="utf-8"></script>
<script src="/js/lib/angular/angular-ui-router.min.js" charset="utf-8"></script> <script src="/js/lib/angular/angular-ui-router.min.js" charset="utf-8"></script>
<!-- Core Files -->
<script src="/js/app.js" charset="utf-8"></script> <script src="/js/app.js" charset="utf-8"></script>
<!-- Services --> <!-- Services -->
<script src="/js/services/contacts.js" charset="utf-8"></script> <script src="/js/services/contacts.js" charset="utf-8"></script>

View File

@ -1,5 +1,3 @@
'use strict';
var app = angular.module('launchpad', ['ui.router', 'LocalStorageModule']); var app = angular.module('launchpad', ['ui.router', 'LocalStorageModule']);
app.config(['$stateProvider', '$urlRouterProvider', 'localStorageServiceProvider', function($stateProvider, $urlRouterProvider, localStorageServiceProvider){ app.config(['$stateProvider', '$urlRouterProvider', 'localStorageServiceProvider', function($stateProvider, $urlRouterProvider, localStorageServiceProvider){
@ -131,7 +129,6 @@ app.config(['$stateProvider', '$urlRouterProvider', 'localStorageServiceProvider
app.run(['$rootScope', '$location', 'Auth', function ($rootScope, $location, Auth) { app.run(['$rootScope', '$location', 'Auth', function ($rootScope, $location, Auth) {
$rootScope.$on('$routeChangeStart', function (event) { $rootScope.$on('$routeChangeStart', function (event) {
if (!Auth.isLoggedIn()) { if (!Auth.isLoggedIn()) {
console.log('DENY'); console.log('DENY');
event.preventDefault(); event.preventDefault();

View File

@ -1,12 +1,19 @@
app.controller('HomeController', ['$scope', 'Auth', 'localStorageService', '$location', function ($scope, Auth, localStorageService, $location) { app.controller('HomeController', ['$scope', 'Auth', 'localStorageService', '$location', function ($scope, Auth, localStorageService, $location) {
var vm = this;
// $scope.$watch(Auth.isLoggedIn, function (value, oldValue) { vm.signOut = function () {
// if(value) { localStorageService.remove('userAuth');
// console.log("LOGGED IN!!!!"); $location.path('/splash-page');
// } else if (!value && !oldValue) { }
// console.log("NOT LOGGED IN!!!");
// $location.path('/splash-page'); function userAuth() {
// } return JSON.parse(localStorageService.get('userAuth'));
// }, true); }
$scope.$watch(Auth.isLoggedIn, function (value, oldValue) {
if (!value && !oldValue) {
$location.path('/splash-page');
}
}, true);
}]); }]);

View File

@ -1,11 +1,18 @@
app.controller('SignInController', ['$scope', 'Auth', '$location', function ($scope, Auth, $location) { app.controller('SignInController', ['$scope', 'Auth', '$location', 'localStorageService', function ($scope, Auth, $location, localStorageService) {
var vm = this; var vm = this;
vm.sign_in = function () { vm.signIn = function () {
// Ask to the server, do your job and THEN set the user
var user = { var userInfo = {
email: "john@doe.com" email: vm.userAuthEmail
}; };
Auth.setUser(user); //Update the state of the user in the app
Auth.setUser(userInfo);
var userAuthenticated = function() {
return localStorageService.set('userAuth', JSON.stringify(userInfo));
}();
$location.path('/launchpad-home'); $location.path('/launchpad-home');
}; };
}]); }]);

View File

@ -1,12 +1,12 @@
app.factory('Auth', [function(){ app.factory('Auth', ['localStorageService', function(localStorageService){
var user; var user;
return{ return{
setUser : function(aUser){ setUser : function(currentUser){
user = aUser; user = currentUser;
}, },
isLoggedIn : function(){ isLoggedIn : function(){
return(user)? user : false; user = JSON.parse(localStorageService.get('userAuth'));
return (user) ? user : false;
} }
}; };
}]); }]);

View File

@ -1,3 +0,0 @@
app.factory('LoginService', [function(){
}]);

View File

@ -19,13 +19,13 @@
</form> </form>
<ul class="nav navbar-nav navbar-right"> <ul class="nav navbar-nav navbar-right">
<li class="dropdown"> <li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">{{ vm.message }} <i class="fa fa-user" aria-hidden="true"></i> <span class="caret"></span></a> <a class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">{{ vm.message }} <i class="fa fa-user" aria-hidden="true"></i> <span class="caret"></span></a>
<ul class="dropdown-menu"> <ul class="dropdown-menu">
<li><a href="feed.html" target="_blank">Preview My Public Site</a></li> <li><a href="feed.html" target="_blank">Preview My Public Site</a></li>
<li><a>Account Settings</a></li> <li><a>Account Settings</a></li>
<li role="separator" class="divider"></li> <li role="separator" class="divider"></li>
<li><a>Switch User</a></li> <li><a>Switch User</a></li>
<li><a>Logout</a></li> <li ng-click="vm.signOut()"><a>Logout</a></li>
</ul> </ul>
</li> </li>
</ul> </ul>

View File

@ -9,6 +9,6 @@
<label for="exampleInputPassword1">Password</label> <label for="exampleInputPassword1">Password</label>
<input type="password" class="form-control" placeholder="Password" ng-model="vm.userAuthPassword"> <input type="password" class="form-control" placeholder="Password" ng-model="vm.userAuthPassword">
</div> </div>
<button type="submit" class="btn btn-default" ng-click="vm.sign_in()">Submit</button> <button type="submit" class="btn btn-default" ng-click="vm.signIn()">Submit</button>
</form> </form>
</section> </section>