Last active
July 4, 2017 23:18
-
-
Save marcelitocs/50de8d4ca562a16f92bb7fe480b76530 to your computer and use it in GitHub Desktop.
Ember Authentication
This file contains hidden or 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 DS from 'ember-data'; | |
import Ember from 'ember'; | |
import ENV from 'myapp/config/environment'; | |
import appVersion from '../utils/app-version'; | |
export default DS.JSONAPIAdapter.extend({ | |
host: ENV.APP.API_URL, | |
init() { | |
this.set('appVersion', appVersion()); | |
}, | |
auth: Ember.inject.service('auth'), | |
routing: Ember.inject.service('-routing'), | |
headers: Ember.computed('auth.isAuthenticated', 'auth.accessToken', 'appVersion', function() { | |
let headers = {}; | |
if(this.get('auth.isAuthenticated')) { | |
headers['Authorization'] = `JWT ${this.get("auth.accessToken")}`; | |
} | |
headers['Version'] = this.get('appVersion'); | |
return headers; | |
}), | |
handleResponse(status) { | |
if (status === 401 && this.get('auth.isAuthenticated')) { | |
this.get('auth').logout(); | |
this.get('routing').transitionTo('auth.login'); | |
} | |
return this._super(...arguments); | |
} | |
}); |
This file contains hidden or 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'; | |
const { inject: { service }, Mixin } = Ember; | |
export default Mixin.create({ | |
auth: service('auth'), | |
beforeModel(transition) { | |
if (!this.get('auth.isAuthenticated')) { | |
this.set('auth.attemptedTransition', transition); | |
return this.transitionTo("/auth/login"); | |
} else { | |
return this._super(...arguments); | |
} | |
} | |
}); |
This file contains hidden or 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 AjaxService from 'ember-ajax/services/ajax'; | |
import appVersion from '../utils/app-version'; | |
import ENV from 'myapp/config/environment'; | |
export default AjaxService.extend({ | |
host: ENV.APP.API_URL, | |
init() { | |
this.set('appVersion', appVersion()); | |
}, | |
auth: Ember.inject.service(), | |
headers: Ember.computed('auth.isAuthenticated', 'auth.accessToken', 'appVersion', { | |
get() { | |
let headers = {}; | |
if(this.get('auth.isAuthenticated')) { | |
headers['Authorization'] = `JWT ${this.get("auth.accessToken")}`; | |
} | |
headers['Version'] = this.get('appVersion'); | |
return headers; | |
} | |
}) | |
}); |
This file contains hidden or 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 DS from 'ember-data'; | |
import ENV from 'myapp/config/environment'; | |
import appVersion from 'myapp/utils/app-version'; | |
const { RSVP: { Promise } } = Ember; | |
export default Ember.Service.extend({ | |
accessToken: null, | |
currentUserId: null, | |
isAuthenticated: Ember.computed.bool('accessToken'), | |
cookies: Ember.inject.service('cookies'), | |
store: Ember.inject.service('store'), | |
ajax: Ember.inject.service('ajax'), | |
init(){ | |
this._super(...arguments); | |
let cookies = this.get('cookies'); | |
let accessToken = cookies.read('accessToken'); | |
let currentUserId = cookies.read('currentUserId'); | |
if(accessToken && currentUserId){ | |
this.set('accessToken', accessToken); | |
this.set('currentUserId', currentUserId); | |
} | |
}, | |
login(username, password, rememberme) { | |
return this.get('ajax').request('/auth/login', { | |
method: "POST", | |
data: {username: username, password: password, rememberme: rememberme}, | |
contentType: "application/vnd.api+json" | |
}).then(response => { | |
this.set('currentUserId', response.userId); | |
this.set('accessToken', response.accessToken); | |
}); | |
}, | |
logout() { | |
this.set('accessToken', null); | |
this.set('currentUserId', null); | |
this.get('store').unloadAll(); | |
}, | |
currentUser: Ember.computed('isAuthenticated', 'currentUserId', function() { | |
if (this.get('isAuthenticated')) { | |
let promise = this.get('store').findRecord('user', this.get('currentUserId')); | |
return DS.PromiseObject.create({ promise: promise }); | |
}else{ | |
return null; | |
} | |
}), | |
tokenChanged: (function() { | |
let cookies = this.get('cookies'); | |
if (Ember.isEmpty(this.get('accessToken'))) { | |
cookies.clear('accessToken'); | |
cookies.clear('currentUserId'); | |
} else { | |
cookies.write('accessToken', this.get('accessToken'), {path: '/'}); | |
cookies.write('currentUserId', this.get('currentUserId'), {path: '/'}); | |
} | |
}).observes('accessToken') | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment