Created
May 6, 2015 12:46
-
-
Save jacojoubert/f59d18c229b9a72c032d to your computer and use it in GitHub Desktop.
Ember Sample Authentication Service
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
`import Ember from 'ember'` | |
`import ajax from 'ic-ajax'` | |
`import config from 'bonjour-client/config/environment'` | |
storeProxy = Ember.computed (key, value) -> | |
key = "bonjour:#{key}" | |
if arguments.length == 1 | |
window.localStorage.getItem key | |
else | |
if Ember.isNone value | |
window.localStorage.removeItem key | |
else | |
window.localStorage.setItem key, value | |
value | |
.volatile() | |
Authentication = Ember.Service.extend | |
token: storeProxy | |
userId: storeProxy | |
isSignedIn: Ember.computed.and 'token', 'userId' | |
currentUser: Ember.computed 'userId', -> | |
if @get('isSignedIn') | |
store = this.container.lookupFactory('service:store') | |
return store.find('user', @get('userId')) | |
else | |
return null | |
# Create new account | |
create: (credentials) -> | |
ajax | |
url: "#{config.API_BASE}/users", | |
type: 'POST', | |
dataType: 'json', | |
data: credentials | |
.then (payload) => | |
@set 'token', payload.access_token.token | |
@set 'userId', payload.access_token.user_id | |
delete payload.access_token | |
@store.pushPayload payload | |
, (error) -> | |
Ember.RSVP.reject (try JSON.parse error.jqXHR.responseText) || {} | |
# Logs user in | |
login: (credentials) -> | |
ajax | |
url: "#{config.API_BASE}/access_tokens", | |
type: 'POST', | |
dataType: 'json', | |
data: credentials | |
.then (payload) => | |
@set 'token', payload.access_token.token | |
@set 'userId', payload.access_token.user_id | |
@get('store').push('user', payload.users[0]) | |
, (error) -> | |
Ember.RSVP.reject (try JSON.parse error.jqXHR.responseText) || {} | |
# Logs user out and delete session data | |
logout: -> | |
ajax | |
url: "#{config.API_BASE}/access_tokens", | |
type: 'DELETE', | |
beforeSend: (xhr) => | |
xhr.setRequestHeader 'ACCESS_TOKEN', @get 'token' | |
@setProperties 'userId': null, 'token': null | |
# Reload app to ensure all sensitive data is removed | |
window.location = window.location.protocol + '//' + window.location.host | |
# Request account password reset | |
requestPasswordReset: (identifier) -> | |
requestData = | |
password_reset_email: | |
email: identifier | |
ajax | |
url: "#{config.API_BASE}/users/password_reset_email", | |
type: 'POST', | |
dataType: 'json', | |
data: requestData | |
.then -> | |
return | |
, (error) -> | |
Ember.RSVP.reject (try JSON.parse error.jqXHR.responseText) || {} | |
# Update password using in from from requestPasswordReset email | |
resetPassword: (email, temporary_password, new_password) -> | |
requestData = | |
access_token: | |
email: email | |
temporary_password: temporary_password | |
new_password: new_password | |
ajax | |
url: "#{config.API_BASE}/access_tokens", | |
type: 'POST', | |
dataType: 'json', | |
data: requestData | |
.then (payload) => | |
@set 'token', payload.access_token.token | |
@set 'userId', payload.access_token.user_id | |
@get('store').push('user', payload.users[0]) | |
, (error) -> | |
Ember.RSVP.reject (try JSON.parse error.jqXHR.responseText) || {} | |
# Update password using in from from requestPasswordReset email | |
changePassword: (params) -> | |
data = | |
user: | |
user_id: params.userId | |
password: params.password | |
new_password: params.newPassword | |
ajax | |
url: "#{config.API_BASE}/users/#{params.userId}", | |
type: 'PATCH', | |
dataType: 'json', | |
data: data | |
beforeSend: (xhr) => | |
xhr.setRequestHeader 'ACCESS_TOKEN', @get 'token' | |
.then (payload) => | |
@get('store').pushPayload payload | |
, (error) -> | |
Ember.RSVP.reject (try JSON.parse error.jqXHR.responseText) || {} | |
`export default Authentication` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment