Skip to content

Instantly share code, notes, and snippets.

@ilake
Created March 7, 2018 13:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ilake/70651e8d40ca721dc2c697f3b88745ab to your computer and use it in GitHub Desktop.
Save ilake/70651e8d40ca721dc2c697f3b88745ab to your computer and use it in GitHub Desktop.
# config/initializers/devise.rb
# for web
config.omniauth :google_oauth2, ENV['GOOGLE_CLIENT_ID'], ENV['GOOGLE_CLIENT_SECRET'],
{ access_type: "offline",
prompt: "consent",
select_account: true,
provider_ignores_state: true,
scope: "plus.profile.emails.read,calendar,contacts" }
# for api
config.omniauth :google_oauth2, ENV['GOOGLE_CLIENT_ID'], ENV['GOOGLE_CLIENT_SECRET'],
{
name: "google_oauth2_for_api",
access_type: "offline",
prompt: "consent",
select_account: true,
provider_ignores_state: true,
scope: "plus.profile.emails.read,calendar,contacts"
}
# callback controller
class CallbacksController < Devise::OmniauthCallbacksController
include Devise::Controllers::Rememberable
def google_oauth2
outcome = ::Users::FromOmniauth.run(auth: request.env["omniauth.auth"])
if outcome.valid?
user = outcome.result
remember_me(user)
sign_in(user)
redirect_to root_path
else
redirect_to new_user_registration_url
end
end
def google_oauth2_for_api
outcome = ::Users::FromOmniauth.run(auth: request.env["omniauth.auth"])
if outcome.valid?
user = outcome.result
auth_token = user.generate_auth_token # because I use https://github.com/gonzalo-bulnes/simple_token_authentication
render json: {
success: true,
auth_token: auth_token,
auth_email: user.email
}
else
render json: { success: false, message: "Invalid credentials"}, status: :unprocessable_entity
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment