Skip to content

Instantly share code, notes, and snippets.

@giosakti
Created March 27, 2013 02:16
Show Gist options
  • Save giosakti/5251023 to your computer and use it in GitHub Desktop.
Save giosakti/5251023 to your computer and use it in GitHub Desktop.
app.service "Session", (sessionStore, UserSession) ->
# Initialize variables (happens every page load)
@signedIn = !!window.sessionStorage.getItem(sessionStore)
@signedOut = not @signedIn
if @signedIn
@currentUser = JSON.parse(window.sessionStorage.getItem(sessionStore)).user
@userSession = new UserSession(
login: ""
password: ""
remember_me: false
auth_token: @currentUser.authentication_token
)
else
@currentUser = null
@userSession = new UserSession(
login: ""
password: ""
remember_me: false
auth_token: ""
)
# Function for storing session data, then update variables accordingly
@putSessionData = (sessionData) ->
window.sessionStorage.setItem(sessionStore, JSON.stringify(sessionData))
@signedIn = true
@signedOut = not @signedIn
@currentUser = sessionData.user
@userSession = new UserSession(
login: ""
password: ""
remember_me: false
auth_token: sessionData.user.authentication_token
)
# Function for clearing session data, then update variables accordingly
@removeSessionData = () ->
window.sessionStorage.removeItem(sessionStore)
@signedIn = false
@signedOut = not @signedIn
@currentUser = null
@userSession = new UserSession(
login: ""
password: ""
remember_me: false
auth_token: ""
)
return this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment