http://localhost:3000/auth/auth0/callback
.http://localhost:3000
.http://localhost:3000
.omniauth-auth0
, a custom OmniAuth strategy, to handle the authentication flow.Add the following dependencies to your Gemfile
:bundle install
../config/auth0.yml
to specify your Auth0 domain, client ID, and client
secret values located in your Auth0 Dashboard under application Settings../config/initializers/auth0.rb
and configure the OmniAuth middleware with the configuration
file you created in the previous step.Ensure that callback_path
matches the value given in the “Allowed Callback URLs” setting in your
Auth0 application.logout
action, and methods for
constructing the logout URL.Run the command:
rails generate controller auth0 callback failure logout --skip-assets --skip-helper --skip-routes --skip-template-engine
.Inside the callback method, assign the hash of user information - returned as
request.env['omniauth.auth']
- to the active session.To configure logout, clear all the objects stored within the session by calling the reset_session
method within the logout
action. Then, redirect to the Auth0 logout endpoint. To learn more about
reset_session
, read Ruby on Rails ActionController documentation../config/routes.rb
file.Routes must be in place so Rails knows how to route the various Auth0 callback URLs to the Auth0 controller you
created in the previous step./auth/auth0
endpoint.link_to
or
button_to
helper methods with the :post
method./auth/auth0
endpoint when
selected. Observe that you redirect to Auth0 to log in, and then back to your app after successful
authentication.auth/logout
action, which redirects them
to the Auth0 logout endpoint./auth/logout
endpoint when
selected. Verify that you redirect to Auth0 and then quickly back to your application, and that you are no
longer logged in.session[:userinfo]
as in the following example:Secured
concern to your app and then include it in the controller that requires an
authenticated user to access it. Verify that an authenticated user has access to actions within that
controller and that unauthenticated users are redirected to Auth0 for authentication.