-
-
Save qustosh/ff44780f09a452e2f12d to your computer and use it in GitHub Desktop.
Basic auth module for angular
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
auth = angular.module 'auth' | |
auth.directive\ | |
( 'hljs' | |
, [ -> | |
restrict: 'A' | |
scope: src: '&hljs' | |
link: ($scope, $elem, attr) -> | |
(hl = (src) -> | |
$elem.html src | |
hljs.highlightBlock $elem[0])($scope.$eval $scope.src) | |
$scope.$watch $scope.src, hl | |
]) | |
auth.controller\ | |
( 'AuditCtrl' | |
, ['$scope', 'hits', ($scope, hits) -> | |
$scope.hits = hits | |
$scope.stage = 0 | |
$scope.selected = hits[0] | |
$scope.select = (hit) -> | |
$scope.selected = hit | |
]) | |
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
#/////////////////////////////////////////////////////////////////////// | |
# Auth.coffee | |
#/////////////////////////////////////////////////////////////////////// | |
auth = angular.module 'auth' | |
auth.factory\ | |
( 'Auth' | |
, ['$q', '$http', '$window', '$state', ($q, $http, $window, $state) -> | |
deferred = null | |
class Auth | |
@user = null | |
@isMe: (user) -> | |
user?._id == @user?._id | |
@whoami: (force) -> | |
if !deferred? or force | |
deferred = $q.defer() | |
if !force and @user? then deferred.resolve @user | |
else | |
req = $http({ | |
url: '/api/whoami' | |
cache: false # must be for force | |
}) | |
req | |
.success (data, status) -> | |
deferred.resolve (Auth.user = JSON.parse data) | |
.error (data) -> Auth.user = null | |
deferred.promise | |
@login: (user, pass) -> | |
deferred = $q.defer() | |
$http | |
.post '/authenticate', {user: user, pass: pass} | |
.success (data, status) -> | |
console.log 'Success: Authenticated' | |
$window.localStorage.token = data.token | |
Auth.user = data.user | |
deferred.resolve data | |
.error (data, status) -> | |
console.log 'Error: Invalid user/pass' | |
delete $window.localStorage.token | |
Auth.user = null | |
deferred.reject null | |
return deferred.promise | |
@logout: -> | |
deferred = $q.defer() | |
delete $window.localStorage.token | |
Auth.user = null | |
tapped = Auth.whoami true | |
tapped.then (id) -> deferred.resolve id | |
deferred.promise | |
]) |
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
auth = angular.module 'auth' | |
auth.controller\ | |
( 'AuthCtrl' | |
, [ 'Auth', '$scope', '$http', '$window', '$state' | |
(Auth, $scope, $http, $window, $state) -> | |
$scope.input = | |
user: null | |
pass: null | |
$scope.denied = false | |
$scope.btnMssg = -> | |
if $scope.denied then 'Invalid, try again' else 'Login' | |
$scope.submit = -> | |
$scope.waiting = true | |
authed = Auth.login $scope.input.user, $scope.input.pass | |
authed.then (data) -> | |
if $window.blockedHash? | |
$window.location = $window.blockedHash | |
$window.blockedHash = null | |
else $state.transitionTo 'app.dashboard' | |
authed.catch -> | |
$scope.denied = true | |
$scope.waiting = false | |
]) | |
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
auth = angular.module 'auth' | |
auth.directive\ | |
( 'authLogout' | |
, ['Auth', (Auth) -> | |
restrict: 'AC' | |
link: ($scope, $elem, attr) -> | |
$elem.click -> | |
Auth.logout() | |
]) | |
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
auth = angular.module('auth') | |
auth.factory\ | |
( 'authInterceptor' | |
, ['$rootScope', '$q', '$window', ($rootScope, $q, $window) -> | |
request: (config) -> | |
config.headers = config.headers || {} | |
if $window.localStorage.token | |
config.headers.Authorization = | |
"Bearer #{$window.localStorage.token}" | |
return config || $q.when config | |
responseError: (response) -> | |
if response.status is 401 | |
console.log 'User is not authed' | |
if not /login/.test $window.location | |
$window.blockedHash ?= $window.location.hash | |
$window.location = '#/login' | |
return $q.reject response | |
]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment