Skip to content

Instantly share code, notes, and snippets.

@seifsallam
Created February 17, 2014 10:49
Show Gist options
  • Save seifsallam/9048476 to your computer and use it in GitHub Desktop.
Save seifsallam/9048476 to your computer and use it in GitHub Desktop.
Authentication initializer using ember-simple-auth — https://github.com/simplabs/ember-simple-auth/
###*
* Authentication Initializer
###
Authorizer = Ember.SimpleAuth.Authorizers.Base.extend(
authorize: (jqXHR, requestOptions) ->
unless Ember.isEmpty @get('session.auth_token')
jqXHR.setRequestHeader 'x-authentication-token', @get('session.auth_token')
)
initializer =
name: 'authentication'
initialize: (container, application) ->
Ember.SimpleAuth.Authenticators.Base.reopen(
serverTokenEndpoint: "#{BASE_URL}/sessions/"
restore: (properties) ->
new Ember.RSVP.Promise((resolve, reject) ->
unless Ember.isEmpty(properties.auth_token)
resolve properties
else
reject()
)
authenticate: (credentials) ->
if Ember.isEmpty(credentials.auth_token)
new Ember.RSVP.Promise (resolve, reject) =>
data =
email: credentials.email
password: credentials.password
@_makeRequest(data).then ((response) ->
Ember.run ->
console.log response
console.log Ember.$.extend(response)
resolve Ember.$.extend(response)
), (xhr, status, error) ->
Ember.run ->
reject xhr.responseText
else
new Ember.RSVP.Promise (resolve, reject) =>
Ember.run ->
resolve Ember.$.extend(credentials)
_makeRequest: (data) ->
Ember.$.ajax
url: @serverTokenEndpoint
type: 'POST'
dataType: 'json'
data: data
)
# customize the session so that it allows access to the account object
Ember.SimpleAuth.Session.reopen
account: (->
accountId = @get('user_id')
unless Ember.isEmpty(accountId)
container.lookup('store:main').find 'profile', accountId
).property('accountId')
Ember.SimpleAuth.setup application,
authenticationRoute: 'signin'
routeAfterAuthentication: 'home'
routeAfterInvalidation: 'landing'
crossOriginWhitelist: [BASE_URL]
authorizer: Authorizer
`export default initializer`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment