Skip to content

Instantly share code, notes, and snippets.

@yhsiang
Last active August 29, 2015 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yhsiang/9148107 to your computer and use it in GitHub Desktop.
Save yhsiang/9148107 to your computer and use it in GitHub Desktop.
Simple toke-based angular auth module with Livescript
app = angular.module 'myApp' <[myAuth]>
# miss routing policy here, write it by urself
app.controller 'MenuCtrl' <[AuthService $scope]> ++ (AuthService, $scope) ->
$scope.login = ->
# redirect to Login Page
$scope.logout = -
$scope.login = ->
AuthService.logout!
$scope.message = 'ByeBye'
app.controller 'LoginCtrl' <[AuthService $scope]> ++ (AuthService, $scope) ->
$scope.message = ''
$scope.user = {}
$scope.isAuthenticated = ->
AuthService.isAuthenticated!
$scope.login = ->
AuthService.login $scope.user, ->
$scope.message = 'Welcome'
auth = angular.module 'myAuth' <[]>
auth.factory 'AuthService' <[$http]> ++ ($http) ->
return do
_profile: {}
is-authenticate: ->
return true if $window.sessionStorage.token
return false
login: (user, cb)->
$http.get '/auth', user
.success (data) ->
$window.sessionStorage.token = data.token
# TODO: save to _profile
cb!
.error
delete $window.sessionStorage.token
# handle error here
logout: ->
delete $window.sessionStorage.token
doctype html
html(ng-app='myapp')
head
// ignore
body
div(ng-controller="MenuCtrl")
a(href='',ng-click='logout()',ng-show='isAuthenticated()')
a(href='',ng-click='login()')
.message {{message}}
ng-view
div(ng-controller='LoginCtrl')
input(ng-model='user.username', type='text, placeholder='Username')
input(ng-model='user.password, type='password', placeholder='Password')
a(href='',ng-click='login()')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment