https://github.com/authress/rails-devise-starter-kit
Authress Starter Kit: Ruby on Rails with Devise
https://github.com/authress/rails-devise-starter-kit
Last synced: about 1 month ago
JSON representation
Authress Starter Kit: Ruby on Rails with Devise
- Host: GitHub
- URL: https://github.com/authress/rails-devise-starter-kit
- Owner: Authress
- Created: 2023-05-25T12:43:39.000Z (about 3 years ago)
- Default Branch: main
- Last Pushed: 2024-02-21T10:48:38.000Z (over 2 years ago)
- Last Synced: 2025-12-28T01:50:39.744Z (5 months ago)
- Language: Ruby
- Size: 39.1 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Authress Starter Kit: Ruby Rails using Devise
A repository that contains a Ruby on Rails example that uses [Authress](https://authress.io) via the Devise gem to login.
## Getting Started
The repository is a simple example using Ruby on Rails. Using the Devise gem and some additional ones the repo add Login with Authress. If you already have a Rails project running with Devise, you can use this repo as an example template and jump down to the [Configuration Section](#configuration) to see what updates should be made to your project to get it from just using Devise to using Devise with Authress.
### Running this project
This repo uses ruby `bundler` to install dependencies:
```sh
gem install bundler rails
bundle install
# Starts the server
bundle exec rails server
```
## Configuration
### 0. Authress Account
Just quickly hop over to the [Authress](https://authress.io/app/#/signup) to get a free account if you don't have one already.
### 1. Install Omniauth and Authress SDK
Add to your Gemfile
```rb
gem "devise"
gem "omniauth"
gem "authress-sdk"
gem 'omniauth-rails_csrf_protection'
```
### 2. Include `:omniauthable` attribute to the app model
Where ever you have defined `devise` add the `:omniauthable` attribute to the configuration. This should already be set, but if it isn't, remember to add the necessary attributes:
* include `omniauth_providers: %i[authress]`
```rb
class User < ApplicationRecord
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :validatable, :omniauthable, omniauth_providers: %i[authress]
end
```
In `config/routes.rb`:
```rb
Rails.application.routes.draw do
devise_for :users, controllers: { omniauth_callbacks: 'users/omniauth_callbacks' }
root to: "home#index"
end
```
### 3. Add the Authress OmniAuth configuration
Create the `config/initializers/authress.rb` file:
```rb
require 'authress-sdk'
AuthressSdk.configure do |config|
# create an instance of the API class during service initialization
# Replace the base_url with the custom Authress domain for your account
# https://authress.io/app/#/settings?focus=domain
config.base_url = 'https://login.company.com'
end
if AuthressSdk::AuthressClient.default.base_url == 'https://login.company.com'
raise "Please set the Authress base_url in the authress.rb initializer to your custom domain. The custom domain can be configured at https://authress.io/app/#/settings?focus=domain"
end
Rails.application.config.middleware.use OmniAuth::Builder do
# Application ID generated in Authress dashboard (https://authress.io/app/#/settings?focus=applications)
# You can either use the default app `app_default` or create a new one
provider :authress, :application_id => 'app_default'
end
```
### 4. Add a callback controller
Login success! But now we need to populate our internal devise User model. Depending on how you implemented this it might be as easy as calling `User.from_omniauth(...)`
In `app/controllers/users/omniauth_callbacks_controller.rb`:
```rb
class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController
def authress
# The user has successfully logged in now with omniauth, but needs to be converted to your user model.
# So implement this method in your User Model (e.g. app/models/user.rb) so that the @user is populated with the data that you need
if false
@user = User.from_omniauth(request.env['omniauth.auth'])
if @user.persisted?
flash[:notice] = I18n.t 'devise.omniauth_callbacks.success', kind: 'Authress'
sign_in_and_redirect @user, event: :authentication
else
session['devise.authress_user_data'] = request.env['omniauth.auth'].except('extra') # Removing extra as it can overflow some session stores
redirect_to new_user_registration_url, alert: @user.errors.full_messages.join("\n")
end
end
render inline: \
<<~HTML
You have successfully logged in using Authress
User Data Hash:
#{JSON.pretty_generate(request.env['omniauth.auth'])}
HTML
end
def failure
# Handles failed authentication -- Show a failure page (you can also handle with a redirect)
render inline: \
<<~HTML
You reached this page due to an error in your OmniAuth configuration. Check the server logs
Strategy: #{params['strategy']}
Message: #{params['message']}
Url Querystring Data: #{params}
<%= button_to "Try Again", user_authress_omniauth_authorize_path, method: :post %>
HTML
end
end
```
### 5. Add a login button your webpage
In any view where you would like a login button add it directly there:
```rb
<%= button_to "Sign in with your Corporate identity using Authress", user_authress_omniauth_authorize_path, method: :post %>
```