Strategy to authenticate with Zeus WPI via OAuth2 in OmniAuth, powered by Zauth.
Add to your Gemfile
gem 'omniauth-oauth2'
gem 'omniauth-zeuswpi'
gem 'omniauth-rails_csrf_protection'
And run bundle install
Note: Change User
to your specific User model
- Add the strategy to the omniauth config:
config.omniauth :zeuswpi,
Rails.application.credentials.omniauth_client_id,
Rails.application.credentials.omniauth_client_secret,
token_params: { parse: :json }
- Add a callbacks controller:
class CallbacksController < Devise::OmniauthCallbacksController
# See https://github.com/omniauth/omniauth/wiki/FAQ#rails-session-is-clobbered-after-callback-on-developer-strategy
skip_before_action :verify_authenticity_token, only: :zeuswpi
def zeuswpi
@user = User.from_omniauth(request.env["omniauth.auth"])
sign_in_and_redirect @user, event: :authentication
end
def after_omniauth_failure_path_for(scope)
root_path
end
end
- Add the Devise Omniauth routes:
devise_for :users, controllers: {
omniauth_callbacks: 'callbacks'
}
- Make your User model omniauthable:
devise :omniauthable, omniauth_providers: %i[zeuswpi]
- Add the
from_omniauth
helper to yourUser
model:
def self.from_omniauth(auth)
where(name: auth.uid).first_or_create do |user|
user.name = auth.uid
end
end
- Add a button to your website:
<%= button_to "Log in with Zeus WPI",
user_zeuswpi_omniauth_authorize_path %>