public
Last active

Basic Auth manager for Ember

  • Download Gist
authManager.coffee
CoffeeScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
App.authManager = Ember.StateManager.create
# The "stack" contains the currently logged-in user
stack: []
# Callbacks to be ran once the user is authenticated
callbacks: {}
# Grabs the current auth object and returns it
# If callback argument is given, will run the callback function with current auth object as first argument
# If there is no authenticated user:
# callback provided: ask for authentication, then run callback
# callback not provided: return undefined
current: (callback) ->
# if callback is given, use callback, invoking login window if needed
if callback
if(current = @current())
callback current
else
@force_login callback
# else, find current authenticated user
# if no user, return undefined
else
# simple case: stack already exists
if @stack.length > 0
_.last(@stack)
# else, stack doesn't have any auths, search in store and rerun function
else
# SEE IF AUTH EXISTS in sessionStorage
# (WILL NEED to modify this to fit your code)
records = App.sessionStore.findAll(App.Auth)
_.each records.get("content"), (element, index) =>
@stack.push records.objectAtContent index
if @stack.length > 0
return _.last(@stack)
else
return undefined
force_login: (callback) ->
@add_callback "after_authenticate", callback
@transitionTo "authenticating"
authenticate: (user) ->
# check if the user is valid, probably by sending credentials via AJAX request
# CALLBACKS
add_callback: (type, callback) ->
@callbacks[type] = [] unless typeof @callbacks[type] is "object"
@callbacks[type].push callback
do_callbacks: (type, args) ->
_.each @callbacks[type], (_callback) ->
_callback args
@clear_callbacks type
clear_callbacks: (type) ->
@callbacks[type] = []
# STATES
authenticating: Em.State.create
enter: (manager) ->
App.get("router").get('applicationController').connectOutlet
controller: App.get("router").get('authController')
outletName: 'modal'
viewClass: App.NewAuthView
context: App.Auth.createRecord()
exit: (manager) ->
App.get("router").get("applicationController").get("modal").close ->
App.get("router").get('applicationController').disconnectOutlet("modal")
authenticated: Em.State.create
enter: (manager) ->
App.authManager.do_callbacks "after_authenticate", _.last(App.authManager.stack)
exampleFunction.coffee
CoffeeScript
1 2 3 4 5 6
App.authManager.current (auth) =>
# This function is only ran once auth is available (the user logs in)
# So you are safe to do functions that requires a logged in user
 
# In my implementation, this method is part of my ember-data restful-api adapter, and simply
# overrides the default ajax method, wrapping it inside this calback...

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.