Skip to content

Instantly share code, notes, and snippets.

@corradt
Created September 28, 2015 12:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save corradt/014659d201845ac6d17b to your computer and use it in GitHub Desktop.
Save corradt/014659d201845ac6d17b to your computer and use it in GitHub Desktop.
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
private
def current_user
@current_user ||= User.find(session[:user_id]) if session[:user_id]
end
def require_login
if session[:user_id] == nil
redirect_to root_path
end
end
helper_method :require_login
helper_method :current_user
end
<nav class="navbar navbar-default">
<div class="container-fluid">
</div>
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<ul class="nav navbar-nav navbar-right">
<% if current_user %>
<li>signed in as: <%= current_user.name %> - <%= image_tag(@auth['info']['image'],width: '50',height:'50', class:'img-circle') %> </li>
<li><%= link_to "Sign Out", 'sign_out', method: :delete %> </li>
<% else %>
<li><%= link_to "auth/facebook" do %><%=image_tag('facebook.png',width: '40',height:'40', class:'img-circle') %> <% end %> </li>
<% end %>
</ul>
</div>
</nav>
class HomeController < ApplicationController
before_action :set_auth
def index
end
def profile
end
private
def set_auth
@auth=session[:omniauth] if session[:omniauth]
end
end
Rails.application.routes.draw do
root 'home#index'
get 'home/profile'
get 'auth/:provider/callback', to: "sessions#create"
delete 'sign_out', to: "sessions#destroy", as: "sign_out"
end
class SessionsController < ApplicationController
def create
auth = request.env["omniauth.auth"]
session[:omniauth] = auth.except('extra')
user = User.sign_in_from_omniauth(auth)
session[:user_id] = user.id
redirect_to root_url, notice: "Signed In"
end
def destroy
session.delete(:user_id)
session.delete(:omniauth)
redirect_to root_url, notice: "Signed Out"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment