I've been trying for days to make an authentication & registration with Ember and Rails. I'm using torii and ember-simple-auth in my front-end, and Devise with omniauth-google-oauth2 in my back-end. Things seems to be working just fine in my front-end, but for some reason I get a "redirect_uri_mismatch" error when the server is doing a callback. I configured my Google API, setting all possible Authorized redirect URIs.
I guess it's a problem of Omniauth configuration, but I can't figure out what. Do you have any idea?
Here's the error I get:
I, [2016-04-18T20:00:43.521451 #23779] INFO -- omniauth: (google_oauth2) Callback phase initiated.
E, [2016-04-18T20:00:43.676108 #23779] ERROR -- omniauth: (google_oauth2) Authentication failure! invalid_credentials: OAuth2::Error, redirect_uri_mismatch:
{
"error" : "redirect_uri_mismatch"
}
Processing by Users::OmniauthCallbacksController#failure as JSON
Parameters: {"code"=>"4/bN-2ay_LyhRHBr9FggEhRTCpNACKMoSvnW-2Olulw2Y"}
Here's my Omniauth config:
require "omniauth-google-oauth2"
Rails.application.config.middleware.use OmniAuth::Builder do
provider :google_oauth2, ENV["ID_client"], ENV["secret"],
{
:name => "google",
:scope => "profile",
:prompt => "select_account",
:image_aspect_ratio => "square",
:image_size => 50,
:redirect_uri => 'http://localhost:3000/users/auth/google_oauth2',
setup: (lambda do |env|
request = Rack::Request.new(env)
env['omniauth.strategy'].options['token_params'] = {:redirect_uri => 'http://localhost:3000/users/auth/google_oauth2'}
end)
}
end
My Omniauth controller:
class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController
def google_oauth2
logger.debug 'Omniauth callback called'
@user = User.find_for_google_oauth2(request.env["omniauth.auth"], current_user)
if @user.persisted?
flash[:notice] = I18n.t "devise.omniauth_callbacks.success", :kind => "Google"
sign_in_and_redirect @user, :event => :authentication
else
session["devise.google_data"] = request.env["omniauth.auth"]
redirect_to new_user_registration_url
end
end
end
For my front-end, my torii configuration in environment.js:
ENV.torii = {
providers: {
sessionServiceName: 'session',
'google-oauth2': {
apiKey: 'ID_client',
scope: 'profile',
redirectUri: 'http://localhost:4200'
}
}
};
And my torii authenticator:
import Ember from 'ember';
import ToriiAuthenticator from 'ember-simple-auth/authenticators/torii';
import raw from 'ic-ajax';
const { RSVP } = Ember;
export default ToriiAuthenticator.extend({
torii: Ember.inject.service(),
authenticate() {
return new RSVP.Promise((resolve, reject) => {
this._super(...arguments).then((data) => {
raw({
url: 'http://localhost:4200/users/auth/google_oauth2/callback',
type: 'POST',
dataType: 'json',
data: { 'code': data.authorizationCode }
}).then((response) => {
resolve({
access_token: response.access_token,
provider: response.provider
});
}, reject);
}, reject);
});
}
});
Aucun commentaire:
Enregistrer un commentaire