Last active
March 24, 2016 08:55
-
-
Save endel/fd2652384f9dfcc34270 to your computer and use it in GitHub Desktop.
ng-admin authentication with hook https://github.com/doubleleft/hook
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
var app = angular.module('admin', ['ng-admin']), | |
hook = new Hook.Client({...}); | |
var headerNav, pageWrapper, restangular; | |
if (hook.auth.currentUser && hook.auth.currentUser.role != 'admin') { | |
hook.auth.logout(); | |
} | |
function hideMenu() { | |
headerNav = document.querySelector('#header-nav'); | |
pageWrapper = document.querySelector('#page-wrapper'); | |
headerNav.style.display = "none"; | |
pageWrapper.style.marginLeft = "0"; | |
pageWrapper.style.backgroundColor = "inherit"; | |
} | |
function showMenu() { | |
headerNav = document.querySelector('#header-nav'); | |
pageWrapper = document.querySelector('#page-wrapper'); | |
headerNav.style.display = "block"; | |
pageWrapper.style.marginLeft = "250px"; | |
pageWrapper.style.backgroundColor = "#fff"; | |
} | |
// Go to login form if user isn't logged in | |
app.run(function($rootScope, $location, $state){ | |
// listen to logout | |
hook.auth.on('logout', function() { | |
hideMenu(); | |
// why $location.path doesn't work, freaking angular?! | |
location.hash = '#/login'; | |
}); | |
$rootScope.$on("$locationChangeStart", function (event, next, current) { | |
if (!hook.auth.currentUser && !next.match(/login$/)) { | |
$location.path('/login'); | |
} | |
}); | |
}); | |
app.controller('AuthenticationController', function ($scope, $rootScope, $location) { | |
$scope.title = 'Authentication'; | |
$scope.success = false; | |
$scope.login = function (data) { | |
hook.auth.login(data).then(function(auth) { | |
showMenu(); | |
$scope.$apply(function(){ | |
$scope.success = true; | |
}); | |
$location.path('/dashboard'); | |
$location.reload(); | |
}).otherwise(function(data) { | |
// $apply is weird | |
$scope.$apply(function(){ | |
$scope.error = data.error; | |
}); | |
}); | |
}; | |
$scope.$on('$viewContentLoaded', function() { | |
hideMenu(); | |
}); | |
}); | |
app.config(function($stateProvider) { | |
$stateProvider | |
.state('auth', { | |
parent: 'main', | |
url: '/login', | |
controller: 'AuthenticationController', | |
controllerAs: 'listController', | |
template: document.querySelector('#login-template').innerHTML | |
}); | |
}) |
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
<html> | |
<body ng-app="admin" ng-controller="main"> | |
<div class="ui-view"></div> | |
<script type="text/html" id="login-template"> | |
<div class="container" ng-hide="success"> | |
<div class="row"> | |
<div class="col-md-4 col-md-offset-4"> | |
<div class="login-panel panel panel-default"> | |
<div class="panel-heading"> | |
<h3 class="panel-title">Please Sign In</h3> | |
</div> | |
<div class="panel-body"> | |
<form role="form" ng-submit="login(auth)"> | |
<fieldset> | |
<div class="form-group"> | |
<input class="form-control" placeholder="E-mail" ng-model="auth.email" type="email" autofocus> | |
</div> | |
<div class="form-group"> | |
<input class="form-control" placeholder="Password" ng-model="auth.password" type="password" value=""> | |
</div> | |
<!--div class="checkbox"> | |
<label><input name="remember" type="checkbox" value="Remember Me">Remember Me</label> | |
</div--> | |
<div class="alert alert-danger" ng-show="error"> | |
Invalid user or password. | |
</div> | |
<input type="submit" value="Login" class="btn btn-lg btn-success btn-block" /> | |
</fieldset> | |
</form> | |
</div> | |
</div> | |
</div> | |
</div> | |
</div> | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment