2. How to Use Omniauth to Authenticate Our
Users?
We Needs to Sign Up For Various Applications and
Sites Separately.
Why?
We have already sign up on many, using different
usernames, that going back to one of them and
trying to remember our credentials is sometimes
impossible.
3. Now a days, most of the sites offering alternative
way to sign up,
By allowing us to use other sites accounts.
Like
• Google Account
• Twitter
• GitHub
• Facebook
It feels like a long and arduous task.
But fear not, Omniauth is here to help.
4. Omniauth allows us to easily
integrate more than sixty
authentication providers!
6. Creating a Provider
How add a provider to Omniauth?
We need to sign up as a developer on
providers site.
Then just we will have to make two settings in
our application regarding
Username and Password.
7. If you want to use Facebook authentication
head over to
Create new
developers.facebook.com/apps app here
and
click on “Create New App”
9. Add Provider To The App
Create a new file under config/initializers called
omniauth.rb.
Rails.application.config.middleware.use OmniAuth::Builder do
provider :facebook, YOUR_APP_ID, YOUR_APP_SECRET
end
Create the Controller Session
$> rails generate controller sessions new create failure
10. Set path in routes.rb
get '/login', :to => 'sessions#new', :as => :login
match '/auth/:provider/callback', :to => 'sessions#create'
match '/auth/failure', :to => 'sessions#failure'
In Controller app/controllers/sessions_controller.rb just add
def Create
auth_hash = request.env['omniauth.auth']
render :text => auth_hash.inspect
end
11. Now make sure everything is working.
Point the browser to
localhost:3000/auth/facebook/
Then,
we’ll be redirected to Facebook so we can authorize our app.
We will Authorize it, and we will be redirected back to our app
and see a hash with some information.
In between will be our name, our Facebook user id, and our email, among other things.
If error occurred then,
12. Change Some Setting in Facebook/apps for site
redirection
Blank App
Domains
Change site
URL