Ruby on Rails - Ruby on Rails Omniauth with devise - ruby on rails tutorial - rails guides - rails tutorial - ruby rails
What is Omniauth?
- OmniAuth is a library that standardizes multi-provider authentication for web applications.
- It also contains specifications for passing data to/from provider.
ruby on rails tutorial tags - ruby , rail , ruby on rails , rail forum , ruby on rails tutorial , ruby tutorial , rails guides , rails tutorial , learn ruby
First of all, we create rails app:
- add
gem 'devise'
to yourGemfile
and install it
- After that we have to create user model
How it works?
There are basic steps for user handling by Auth with Omniauth:
- User click on 'auth with github'
- Rails app redirect him to github page
- User sign in on github and grant access to your app
- Github redirect user back to your rails app
- Rails app got at least two things: provider and uid
- Now we can use this info to sign in user in app
In conclusion, we use provider
and uid
returned from github to sign in/register user in our app.
Go Over Omniauth
- we will use
gem 'omniauth-github'
and runbundle install
Add Routes and Controller
- As you remember, after success signing in provider will redirect user to some url in our page.
- We should create route for that and handle them in controller
Add omniauth callbacks to config/routes.rb
and create new controller for handle
callback app/controllers/callbacks_controller.rb
- Ok, now we've got provider and uid from github,
- but what about
from_omniauth
method?!
Add Support Omniauth to Your Mode:
- Add
uid
andprovider
fields to users table
- Add
omniauthable
module andfrom_omniauth
class method to the model
Now we need to create app on provider side and set devise config.
ruby on rails tutorial tags - ruby , rail , ruby on rails , rail forum , ruby on rails tutorial , ruby tutorial , rails guides , rails tutorial , learn ruby
Create Github App:
https://github.com/settings/applications/new
- Set site url to
http://localhost:3000/
and callback url tohttp://localhost:3000/users/auth/github/callback
- When we done, go to the
config/initializers/devise.rb
and add omiauth config (paste your client ID and secret instead of placeholders)
- Restart server and go to "/users/sign_in", you will see link for github sign in.