I'm trying to implement login from google functionality in my Rails API.
I added this line to my device.rb
config.omniauth :google_oauth2, Rails.application.credentials.dig(:google, :google_client_id),
Rails.application.credentials.dig(:google, :google_client_secret), scope: 'userinfo.email,userinfo.profile'
I also added client id & secret in my credentials.
Here is my Google function
def google
@user = User.from_omniauth(request.env["omniauth.auth"])
end
Here is my from_omniauth function from 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
As I'm implementing an API, I don't know should I use request.env["omniauth.auth"]
or hardcode the Access-token here I got from OAuthPlayground.
If I hardcode the token how can I access the user information from that token?
I'm also using Devise for Authentication in my application, following this tutorial: https://www.digitalocean.com/community/tutorials/how-to-configure-devise-and-omniauth-for-your-rails-application