Created
February 16, 2016 21:24
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| hello <%= current_user %> | |
| </br> | |
| <% if current_user %> | |
| <%= link_to('Sign out', '/auth/sign_out') %> | |
| <% else %> | |
| <%= link_to('Sign in', '/auth/developer') %> | |
| <% end %> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Rails.application.config.middleware.use OmniAuth::Builder do | |
| unless Rails.env.production? | |
| provider :developer, fields: %i(name email), uid_field: :email | |
| end | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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