I have tried so many guides and so many tutorials, but whenever I try to retrieve information that Omniauth is supposed to provide after the user accepts the permissions. Right now I just want a user to log in with Google and access his calendar.
routes.rb
devise_for :users, path_names: {sign_in: "login", sign_out: "logout"},
:controllers => { :omniauth_callbacks => "callbacks" }
devise_scope :user do
get "/auth/google_oauth2/callback" => 'callbacks#google_oauth2'
end
config/initializes/omniauth.rb
module OmniAuth
module Strategies
class GoogleAuth < OmniAuth::Strategies::GoogleOauth2
option :name, 'google_auth'
option :callback_path, '/callbacks/google'
end
end
end
Rails.application.config.middleware.use OmniAuth::Builder do
provider :google_oauth2, '<my client id in string>', '<my client secret in string>', {
access_type: 'offline',
scope: 'https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/calendar',
callback_path:'http://localhost:3000/auth/google_oauth2/callback'
prompt: 'select_account consent'
}
#provider :devloper unless Rails.env.production?
provider :twitter, ENV['TWITTER_KEY'], ENV['TWITTER_SECRET']
end
callbacks_controller.rb
class CallbacksController < Devise::OmniauthCallbacksController
def google_oauth2
@user = User.from_omniauth(request.env["omniauth.auth"])
if user.persisted?
sign_in_and_redirect @user
else
session["devise.google_data"] = request.env["omniauth.auth"]
redirect_to new_user_registration_url
end
end
end
models/user.rb:
def self.from_omniauth(auth)
where(provider: auth.provider, uid: auth.uid).first_or_create do |user|
user.provider = auth.provider
user.uid = auth.uid
user.email = auth.info.email
user.password = Devise.friendly_token[0,20]
end
end
The error this that env["omniauth.auth"] and :provider. How do I change my files so that Omniauth correctly populates with the user's information and so that I can create a devise user for that session. The project is at https://github.com/SafaTinaztepe/teemvu.git.
on your
user.rb
, I think it should be