Skip to content

Instantly share code, notes, and snippets.

@johnallen3d
Created February 16, 2016 21:24
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save johnallen3d/265598d1896063925acf to your computer and use it in GitHub Desktop.
Save johnallen3d/265598d1896063925acf to your computer and use it in GitHub Desktop.
Simplest implementation of the OmniAuth developer strategy (OmniAuth::Strategies::Developer). http://www.rubydoc.info/github/intridea/omniauth/OmniAuth/Strategies/Developer
class ApplicationController < ActionController::Base
# Prevent CSRF attacks by raising an exception.
# For APIs, you may want to use :null_session instead.
protect_from_forgery with: :exception
def current_user=(user)
session[:user_id] = user.uid
end
def current_user
session[:user_id]
end
helper_method :current_user
end
class SessionsController < ApplicationController
skip_before_action :verify_authenticity_token, only: :create
def create
self.current_user = auth_hash
redirect_to '/'
end
def delete
session[:user_id] = nil
redirect_to '/'
end
protected
def auth_hash
request.env['omniauth.auth']
end
end
hello <%= current_user %>
</br>
<% if current_user %>
<%= link_to('Sign out', '/auth/sign_out') %>
<% else %>
<%= link_to('Sign in', '/auth/developer') %>
<% end %>
Rails.application.config.middleware.use OmniAuth::Builder do
unless Rails.env.production?
provider :developer, fields: %i(name email), uid_field: :email
end
end
Rails.application.routes.draw do
match '/auth/:provider/callback', to: 'sessions#create', via: %i(get post)
match '/auth/sign_out', to: 'sessions#delete', via: %i(get)
root 'welcome#index'
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment