-
-
Save pama/c42315d5a3593e1f6631 to your computer and use it in GitHub Desktop.
This file contains 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 | |
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 |
This file contains 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
<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> |
This file contains 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 HomeController < ApplicationController | |
before_action :require_login, except: [:index] | |
before_action :set_auth | |
def index | |
end | |
def profile | |
end | |
private | |
def set_auth | |
@auth= session[:omniauth] if session[:omniauth] | |
end | |
end |
This file contains 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 | |
root 'home#index' | |
get 'home/profile' | |
get 'auth/:provider/callback', to: "sessions#create" | |
delete 'sign_out', to: "sessions#destroy", as: "sign_out" | |
end |
This file contains 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 | |
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