Google/Facebook OAuth2 in Rails Front-end app with rails-api as webservice
Asked Answered
S

2

8

I have my front-end rails app that uses AngularJs too and I also have an api backend also written in Rails (used rails-api gem). I have implemented login system for this system. Now I want to implement login with facebook/google. I have searched through internet and so far I found this blog. From this blog, I got the idea of implementing google OAuth 2 in Android with Rails as backend. But In my case I want to implement google OAuth 2 in my front-end app (Rails + Angular) with Rails as backend.

For implementation of google OAuth 2, Front-end app need to request the token to google oauth2 authorization server and I have no idea on how to do it.

Also, Is it good to use rails for requesting the token or I can use Angular for that part. What would be the best practice for this purpose?

Search answered 3/7, 2015 at 7:8 Comment(1)
Try to use hello.js in your front-end then validate the token in your backend.Hanes
Z
2

First, you need to implement the google login in front-end. Here is google documentation on implementing google sign-in in javascript. google will provide you with information on user's profile. After you got the information, you can send it to you service. On server side/Backend you can do whatever with this information. That's on you.

Additionally, this documentation might be helpful for your case.

Zebadiah answered 14/7, 2015 at 4:47 Comment(0)
G
1

You could use omniauth-google-oauth2 gem

Main idea is to implement this into Devise.

Good solution You could find this.

With this solution, it is no problem if You use frontend/backend. So, You should pay attention on next steps:

User Model:

devise :database_authenticatable, ... , :omniauthable

Devise.rb:

config.omniauth :google_oauth2, ENV['GP_key'], ENV['GP_secret'],
    {
                  name: 'google',
                  scope: 'plus.login, userinfo.email, userinfo.profile,
                          ...
                          youtubepartner, youtubepartner-channel-audit',
                  prompt: "select_account"
    }

Next steps as in solutions..

In my project it is workable with frontend/backend too. So, if you find difficulties, I will be glad do advice for it solving...

Update: for Android versions of frontend. You should not do authentication/authorization frontend on Google. You should do it on backend side. You could use webtools or other suitable ways for walking around Devise and Google auth pages...

Godden answered 7/7, 2015 at 9:54 Comment(2)
Can you please elaborate with some working examples. I got more confused with this.Search
You wrote: For implementation of google OAuth 2, Front-end app need to request the token to google oauth2 authorization server and I have no idea on how to do it. --- In my opinion, it is not good because You should do auth. process on backend side. Why? As default in RoR we provide user auth with Devise. We could explaine this Devise's auth to omniauth via Google. Please, read article sourcey.com/… for it's good solution. In workable.Duodecimal

© 2022 - 2024 — McMap. All rights reserved.