Omniauth GemAll, Every and Authentication Everywhere Omni + auth By Sumit Munot
How to Use Omniauth to Authenticate Our Users?We Needs to Sign Up For Various Applications andSites Separately.Why?We have already sign up on many, using differentusernames, that going back to one of them andtrying to remember our credentials is sometimesimpossible.
Now a days, most of the sites offering alternativeway to sign up,By allowing us to use other sites accounts.Like• Google Account• Twitter• GitHub• FacebookIt feels like a long and arduous task.But fear not, Omniauth is here to help.
Omniauth allows us to easilyintegrate more than sixty authentication providers!
Creating a ProviderHow add a provider to Omniauth?We need to sign up as a developer onproviders site.Then just we will have to make two settings inour application regardingUsername and Password.
If you want to use Facebook authenticationhead over to Create newdevelopers.facebook.com/apps app hereandclick on “Create New App”
Getting our App id/ API key and App Secret from settings
Add Provider To The App Create a new file under config/initializers called omniauth.rb.Rails.application.config.middleware.use OmniAuth::Builder doprovider :facebook, YOUR_APP_ID, YOUR_APP_SECRETend Create the Controller Session $> rails generate controller sessions new create failure
Set path in routes.rbget /login, :to => sessions#new, :as => :loginmatch /auth/:provider/callback, :to => sessions#creatematch /auth/failure, :to => sessions#failureIn Controller app/controllers/sessions_controller.rb just adddef Createauth_hash = request.env[omniauth.auth]render :text => auth_hash.inspectend
Now make sure everything is working.Point the browser tolocalhost: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 appand 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,
Change Some Setting in Facebook/apps for siteredirection Blank App Domains Change site URL