Created
September 16, 2014 14:42
-
-
Save tukan/c0caad46833ea008de98 to your computer and use it in GitHub Desktop.
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
module.exports = SessionProvider = () -> | |
currentUser = null | |
$get: ($log, $rootScope, $location, $state, $injector, localStorageService) -> | |
authService = $injector.get('jwtAuthService') | |
# Default login starting logic. | |
loginStart = (next, nextParams) -> | |
$log.debug "SessionProvider: redirecting to /login" | |
$state.go('auth.login') | |
# Redirects the user to the redirect search term if it exists. | |
loginSuccess = () -> | |
if $rootScope.requestedState? | |
$state.go($rootScope.requestedState, $rootScope.requestedStateParams) | |
$rootScope.requestedState = null | |
$rootScope.requestedStateParams = null | |
else | |
$location.path "/" | |
# Redirect to / after logout | |
logoutSuccess = () -> | |
$log.debug "SessionProvider: redirecting to /" | |
$location.path "/" | |
# Check authentication before changing route state | |
stateChange = (event, toState, toParams, fromState, fromParams) -> | |
$log.debug "SessionProvider: attempt to access #{toState.name} state" | |
if authService.isAuthenticated() or toState.public | |
$log.debug "SessionProvider: #{if toState.public then "guest" else "authorized "} access to #{toState.name} state (allowed)" | |
else | |
$log.debug "SessionProvider: access to #{toState.name} state (denied)" | |
event.preventDefault() | |
# Saving current state to redirect there after successful login | |
$rootScope.requestedState = toState | |
$rootScope.requestedStateParams = toParams | |
loginStart toState, toParams | |
# Check authentication before changing route state | |
$rootScope.$on "$stateChangeStart", (event, toState, toParams, fromState, fromParams) -> | |
stateChange event, toState, toParams, fromState, fromParams | |
getToken: -> | |
localStorageService.get('auth_token') | |
# Proxy auth service getCurrentUser | |
getCurrentUser: -> | |
authService.getCurrentUser() | |
bindCurrentUserTo: (scope, varName) -> | |
currentUser = this.getCurrentUser() | |
# Setting current user to the scope at first place | |
scope[varName] = currentUser | |
# updating currentUser from the scope to local storage | |
scope.$watchCollection varName, (user) -> | |
localStorageService.set('current_user', angular.toJson(user)) | |
# updating currentUser in scope | |
scope.$watchCollection authService.getCurrentUser, (user) -> | |
scope[varName] = user of false | |
# updates current user preferences, if current user is not present throws an exception | |
setCurrentUserPreferences: (preferences) -> | |
currentUser = this.getCurrentUser() | |
throw new Error("Unable to set current user preferences for non existent user") if not this.isAuthenticated() | |
currentUser.use_expected_winnings = preferences.use_expected_winnings if preferences.use_expected_winnings? | |
# Proxies is isAuthenticated to auth service | |
isAuthenticated: -> | |
authService.isAuthenticated() | |
login: (credentials) -> | |
authService.login(credentials).then (token) -> | |
loginSuccess() if token | |
logout: -> | |
return unless authService.isAuthenticated() | |
authService.logout() | |
logoutSuccess() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment