Skip to content

Instantly share code, notes, and snippets.

@lawrencejones
Last active August 29, 2015 14:01
Show Gist options
  • Save lawrencejones/9265cdaaa9fcf987a135 to your computer and use it in GitHub Desktop.
Save lawrencejones/9265cdaaa9fcf987a135 to your computer and use it in GitHub Desktop.
Basic auth module for angular
auth = angular.module 'auth'
auth.factory\
( 'Auth'
, [ '$q', '$http', '$window', '$state'
($q, $http, $window, $state) ->
deferred = null
class Auth
@user = null
@isMe: (user) ->
user?.is @user # or whatever custom equality
@whoami: (force) ->
return deferred.promise if deferred? and !force
deferred = $q.defer()
if !force and @user? then deferred.resolve @user
else $http({
url: '/api/whoami' # identity route on server
cache: false # must be for force
})\
.success (data, status) ->
deferred.resolve (Auth.user = JSON.parse data)
.error (data) ->
deferred.reject (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
deferred.reject (Auth.user = null)
return deferred.promise
@logout: ->
deferred = $q.defer()
delete $window.localStorage.token
Auth.user = null
Auth.whoami true # force whoami
])
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.waiting = false
$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' # or whatever home
authed.catch ->
$scope.denied = true
$scope.waiting = false
])
auth = angular.module 'auth'
auth.directive\
( 'authLogout'
, ['Auth', (Auth) ->
restrict: 'AC'
link: ($scope, $elem, attr) ->
$elem.click ->
Auth.logout()
])
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' # or whatever login state
return $q.reject response
])
mixin email-pass
input.user-input.form-control.input-lg(
type='email'
ng-model!='input.user'
placeholder='Login/Email address', required='')
input.password-input.form-control.input-lg(
type='password'
ng-model!='input.pass'
placeholder='Password', required='')
.pane.login
form.form-login(ng-controller='AuthCtrl')
.row
.col-md-12
+email-pass()
.buffer
.row.unselectable
.col-md-12
button.submit.btn.btn-default.btn-lg(ng-click!='submit()', ng-class!="{'btn-warning': denied}") Login
.row.unselectable.hide
.col-md-8
label.remember
input(type='checkbox', ng-model!='input.remember')
span Remember me
hr
.row
.col-md-6.forgotten
a Forgotten password?
.col-md-6.register
a Register
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment