How to pre-populate a Rails 4 application with a Google user?

Im using Rails 4.2.5 with the stone "omniauth-google-oauth2". In my application, the only way users can log in is to log in to their Google or Facebook logins. I would like to pre-populate my application with the initial user, me (email = ' davea@gmail.com ), with the administrator role. It would be nice to do this programmatically so that when I roll back from it in other environments I can use the same code.

There are roles in my roles table (via db / seeds.rb)

Admin User 

and my app / model / user.rb file has

 def self.from_omniauth(auth) where(auth.slice(:provider, :uid)).first_or_initialize.tap do |user| user.provider = auth.provider user.uid = auth.uid user.name = auth.info.name user.oauth_token = auth.credentials.token user.oauth_expires_at = Time.at(auth.credentials.expires_at) user.save! end end 

I'm not sure how to do what I want, and some tips are appreciated.

+6
source share
2 answers

Now suppose you have a Google uid . Just create a user from your seeds, for example:

 user = User.new( provider: "google", uid: "your-google-id", email: " davea@gmail.com ", name: "Your name" ) user.roles << admin_role # Replace this line with your role assignment user.save # Perhaps use save(validate: false) if there're validations for other fields 

At the same time, when you log in using Google, omniauth logic should be able to find the seven user, which means that you can act as an administrator.

Please note that this assumes that you do not need the Google oauth token for further operation, since you do not have it, and it is not saved from your from_omniauth if the user record already exists.

PS from your sample code, Oauth information is stored directly in the User model ( provider and uid ). However, I am afraid that the user will not be able to log in with Facebook and Google at the same time, since both of them will want to save these two fields.

Update: inserting a model from my code base, which is a separate model from User , which allows several providers to log in. Of course, the controller needs to be updated to use Authorization instead of User . Just in case, this helps.

 class Authorization < ActiveRecord::Base belongs_to :user def self.from_omniauth(auth) authorization = where(auth.slice(:provider, :uid)).first_or_create return authorization if authorization.user if user = User.where(email: auth.info.email).first authorization.bind_user(user) else user = authorization.create_user(auth.info) end authorization end def bind_user(user) self.user = user save end def create_user(info) user = User.new( email: info.email, password: Devise.friendly_token[0, 20], first_name: info.first_name, last_name: info.last_name, ) user.save(validate: false) bind_user(user) user end end 
+2
source

You will need to run the request through the controller through the seed.rb file in order to execute the OAuth2 process.

Since you will most likely have to enter credentials or select your google account from the GUI, I suggest running a system command in the seed.rb file, which opens a browser for the URL of your authorization action.

 # Mac: system("open <url_to_authorize_action>") 

If it is necessary for serialization, immediately after that, add a while loop that checks the DB every N time threshold to find out if this user is allowed.

 while <user_not_authorized> do sleep <N seconds> end 

You can turn this around in multiple development environments, but obviously not into production.

+1
source

All Articles