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:
rails new git_omni
Clicking "Copy Code" button will copy the code into the clipboard - memory. Please paste(Ctrl+V) it in your destination. The code will get pasted. Happy coding from Wikitechy - ruby on rails tutorial - rails guides - ruby rails - rubyonrails - learn ruby on rails - team
- add
gem 'devise'to yourGemfileand install it
bundle install
rails g devise:install
Clicking "Copy Code" button will copy the code into the clipboard - memory. Please paste(Ctrl+V) it in your destination. The code will get pasted. Happy coding from Wikitechy - ruby on rails tutorial - rails guides - ruby rails - rubyonrails - learn ruby on rails - team
- After that we have to create user model
rails g devise user
bundle exec rake db:migrate
Clicking "Copy Code" button will copy the code into the clipboard - memory. Please paste(Ctrl+V) it in your destination. The code will get pasted. Happy coding from Wikitechy - ruby on rails tutorial - rails guides - ruby rails - rubyonrails - learn ruby on rails - team
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
devise_for :users, :controllers => { :omniauth_callbacks => "callbacks" }
Clicking "Copy Code" button will copy the code into the clipboard - memory. Please paste(Ctrl+V) it in your destination. The code will get pasted. Happy coding from Wikitechy - ruby on rails tutorial - rails guides - ruby rails - rubyonrails - learn ruby on rails - team
and create new controller for handle
callback app/controllers/callbacks_controller.rb
class CallbacksController < Devise::OmniauthCallbacksController
def github
@user = User.from_omniauth(request.env["omniauth.auth"])
sign_in_and_redirect @user
end
end
Clicking "Copy Code" button will copy the code into the clipboard - memory. Please paste(Ctrl+V) it in your destination. The code will get pasted. Happy coding from Wikitechy - ruby on rails tutorial - rails guides - ruby rails - rubyonrails - learn ruby on rails - team
- Ok, now we've got provider and uid from github,
- but what about
from_omniauthmethod?!
Add Support Omniauth to Your Mode:
- Add
uidandproviderfields to users table
rails g migration AddColumnsToUsers provider uid
bundle exec rake db:migrate
Clicking "Copy Code" button will copy the code into the clipboard - memory. Please paste(Ctrl+V) it in your destination. The code will get pasted. Happy coding from Wikitechy - ruby on rails tutorial - rails guides - ruby rails - rubyonrails - learn ruby on rails - team
- Add
omniauthablemodule andfrom_omniauthclass method to the model
class User < ActiveRecord::Base
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable,
:omniauthable
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
end
Clicking "Copy Code" button will copy the code into the clipboard - memory. Please paste(Ctrl+V) it in your destination. The code will get pasted. Happy coding from Wikitechy - ruby on rails tutorial - rails guides - ruby rails - rubyonrails - learn ruby on rails - team
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.rband add omiauth config (paste your client ID and secret instead of placeholders)
config.omniauth :github, 'CLIENT_ID', 'APP_SECRET', :scope => 'user:email'
Clicking "Copy Code" button will copy the code into the clipboard - memory. Please paste(Ctrl+V) it in your destination. The code will get pasted. Happy coding from Wikitechy - ruby on rails tutorial - rails guides - ruby rails - rubyonrails - learn ruby on rails - team
- Restart server and go to "/users/sign_in", you will see link for github sign in.