Created
March 7, 2018 13:54
-
-
Save ilake/70651e8d40ca721dc2c697f3b88745ab to your computer and use it in GitHub Desktop.
This file contains 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
# 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