Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 985
SujitS Avatar asked Jul 03 '15 07:07

SujitS


2 Answers

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.

like image 99
ingnam Avatar answered Nov 16 '22 11:11

ingnam


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...

like image 35
Сергій Назаревич Avatar answered Nov 16 '22 10:11

Сергій Назаревич