I have a strange issue with torii facebook provider. I implemented all of the required methods to make it work but still something is wrong. I overrided torii-providers/facebook.coffee (I am using coffeescript) this way:
`import FacebookOauth2Provider from 'torii/providers/facebook-oauth2'`
FacebookProvider = FacebookOauth2Provider.extend
fetch: (data) ->
data
`export default FacebookProvider`
authenticators/torii.coffee:
`import Ember from 'ember'`
`import Torii from 'ember-simple-auth/authenticators/torii'`
ToriiAuthenticator = Torii.extend
torii: Ember.inject.service()
ajax: Ember.inject.service()
authenticate: ->
ajax = @get('ajax')
self = @
@_super(arguments...).then (data) ->
ajax.request(window.MatebyFrontendNew.API_HOST + '/api/v1/accounts/facebook',
type: 'POST'
dataType: 'json'
data: 'code': data.authorizationCode
).then (response) ->
response
`export default ToriiAuthenticator`
I have checked what is stored in localStorage before and after and after refresh all data are cleared.
So after some research I implemented restore method in torii provider:
restore: (data) ->
if data.access_token && data.account_id
Promise.resolve(data)
else
Promise.reject()
Now localStorage contains required data but I can't log out after refresh. That's what I get when invalidating session:
torii.js:13 Uncaught Error: Expected a provider named 'null' , did you forget to register it?
You don't need the restore method in the torii provider. What does the session store contain before you refresh?
ember_simple_auth:session: "{"authenticated":{"authenticator":"authenticator:torii","access_token":"token_here","refresh_token":"refresh_token_here","token_type":"bearer","expires_in":7200,"scope":"public","created_at":1469096608,"account_type":"user","account_id":27}}"
Ah, I see - there's no provider in the session data. You need to make sure you resolve with that as well.
Hi @marcoow , I have the same issue, my authenticator is resolving with the following data:
return ajax.post(url, { headers: headers, data: post_data }).then((response) => {
console.log('things look okay');
return {
access_token: response.data.attributes['access-token'], // jwt
user_id: response.data.attributes.user,
account_id: response.data.attributes.account,
provider: data.provider
};
}).catch((error) => {
console.log('something went wrong!');
});
where data.provider contains facebook-oauth2 which is being called by the torii authenticator. The session has all that data upon authenticating but all of it is lost when the page is refreshed.
Before refresh, session store has the following:
ember_simple_auth:session:"{"secure":{},"authenticated":{"authenticator":"authenticator:torii","access_token":"eyJhbGciOiJIUzUx...5kN5adMw","user_id":33,"account_id":44,"provider":"facebook-oauth2"}}"
And after refresh is contains only this:
ember_simple_auth:session:"{"secure":{},"authenticated":{"authenticator":"authenticator:torii","provider":"facebook-oauth2"}}"
I'm also extending the SessionService like so:
import DS from 'ember-data';
import Session from 'ember-simple-auth/services/session';
export default Session.extend({
store: Ember.inject.service(),
currentUser: Ember.computed('isAuthenticated', function() {
if (this.get('isAuthenticated')) {
console.log('we are authenticated');
console.log(this.get('data.authenticated'));
return this.get('data.authenticated.user_id');
// const promise = this.get('store').queryRecord('user', {});
// return DS.PromiseObject.create({ promise: promise });
}
})
});
Would really appreciate if you could help me find where I'm going wrong.
I think you should change provider name from facebook-oauth2 to facebook here
return {
access_token: response.data.attributes['access-token'], // jwt
user_id: response.data.attributes.user,
account_id: response.data.attributes.account,
provider: 'facebook'
};
@szsoppa thanks, this helped, I also had to extend Torii's facebook-oauth2's with a fetch method as @marcoow mentioned in #811 (as mentioned in the docs here)
Thanks to both of you guys.
Most helpful comment
Ah, I see - there's no
providerin the session data. You need to make sure you resolve with that as well.