login and save localstorage
This commit is contained in:
parent
a6344ce60c
commit
de8798f01b
|
@ -29,6 +29,7 @@
|
|||
<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-ui-router.min.js" charset="utf-8"></script>
|
||||
<!-- Core Files -->
|
||||
<script src="/js/app.js" charset="utf-8"></script>
|
||||
<!-- Services -->
|
||||
<script src="/js/services/contacts.js" charset="utf-8"></script>
|
||||
|
|
|
@ -1,5 +1,3 @@
|
|||
'use strict';
|
||||
|
||||
var app = angular.module('launchpad', ['ui.router', 'LocalStorageModule']);
|
||||
|
||||
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) {
|
||||
$rootScope.$on('$routeChangeStart', function (event) {
|
||||
|
||||
if (!Auth.isLoggedIn()) {
|
||||
console.log('DENY');
|
||||
event.preventDefault();
|
||||
|
|
|
@ -1,12 +1,19 @@
|
|||
app.controller('HomeController', ['$scope', 'Auth', 'localStorageService', '$location', function ($scope, Auth, localStorageService, $location) {
|
||||
var vm = this;
|
||||
|
||||
// $scope.$watch(Auth.isLoggedIn, function (value, oldValue) {
|
||||
// if(value) {
|
||||
// console.log("LOGGED IN!!!!");
|
||||
// } else if (!value && !oldValue) {
|
||||
// console.log("NOT LOGGED IN!!!");
|
||||
// $location.path('/splash-page');
|
||||
// }
|
||||
// }, true);
|
||||
vm.signOut = function () {
|
||||
localStorageService.remove('userAuth');
|
||||
$location.path('/splash-page');
|
||||
}
|
||||
|
||||
function userAuth() {
|
||||
return JSON.parse(localStorageService.get('userAuth'));
|
||||
}
|
||||
|
||||
$scope.$watch(Auth.isLoggedIn, function (value, oldValue) {
|
||||
if (!value && !oldValue) {
|
||||
$location.path('/splash-page');
|
||||
}
|
||||
}, true);
|
||||
|
||||
}]);
|
||||
|
|
|
@ -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;
|
||||
vm.sign_in = function () {
|
||||
// Ask to the server, do your job and THEN set the user
|
||||
var user = {
|
||||
email: "john@doe.com"
|
||||
vm.signIn = function () {
|
||||
|
||||
var userInfo = {
|
||||
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');
|
||||
};
|
||||
|
||||
}]);
|
||||
|
|
|
@ -1,11 +1,11 @@
|
|||
app.factory('Auth', [function(){
|
||||
app.factory('Auth', ['localStorageService', function(localStorageService){
|
||||
var user;
|
||||
|
||||
return{
|
||||
setUser : function(aUser){
|
||||
user = aUser;
|
||||
setUser : function(currentUser){
|
||||
user = currentUser;
|
||||
},
|
||||
isLoggedIn : function(){
|
||||
user = JSON.parse(localStorageService.get('userAuth'));
|
||||
return (user) ? user : false;
|
||||
}
|
||||
};
|
||||
|
|
|
@ -1,3 +0,0 @@
|
|||
app.factory('LoginService', [function(){
|
||||
|
||||
}]);
|
|
@ -19,13 +19,13 @@
|
|||
</form>
|
||||
<ul class="nav navbar-nav navbar-right">
|
||||
<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">
|
||||
<li><a href="feed.html" target="_blank">Preview My Public Site</a></li>
|
||||
<li><a>Account Settings</a></li>
|
||||
<li role="separator" class="divider"></li>
|
||||
<li><a>Switch User</a></li>
|
||||
<li><a>Logout</a></li>
|
||||
<li ng-click="vm.signOut()"><a>Logout</a></li>
|
||||
</ul>
|
||||
</li>
|
||||
</ul>
|
||||
|
|
|
@ -9,6 +9,6 @@
|
|||
<label for="exampleInputPassword1">Password</label>
|
||||
<input type="password" class="form-control" placeholder="Password" ng-model="vm.userAuthPassword">
|
||||
</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>
|
||||
</section>
|
||||
|
|
Loading…
Reference in New Issue