M
M
Max2016-03-03 20:22:32
Ruby on Rails
Max, 2016-03-03 20:22:32

Why doesn't logout (OmniAuth) work?

Hello, I am registering on the site for the lesson. The entrance to the site works, but the exit does not. Please tell me what to fix. Menu items do not change, and it is written that the user is still on the site.
Application.html.erb

<ul class="nav navbar-nav navbar-right">
    		<% if current_user %>
    			<li>Signed in as: <%= current_user.name %></li>
    			<li><%= link_to "Sign out", 'sign_out', method: :delete %></li>
    		<% else %>
    			<li><%= link_to "auth/facebook" do %>Facebook<% end %></li>
    			<li><%= link_to "auth/vkontakte" do %>VK<% end %>
    		<% end %>
      </ul>

home_controller.rb
before_action :set_auth
  
  def index
  end

  def profile
  end

  private 

  def set_auth
  	@auth = session[:omniauth] if session[:omniauth]
  end

Application_controller.rb
protect_from_forgery with: :exception

  private
 
  def current_user
     @current_user ||= User.find_by(uid: session[:user_id]) if session[:user_id]
  end

  helper_method :current_user

User.rb(model)
def self.sign_in_from_omniauth(auth)
   	 find_by(provider: auth['provider'], uid: auth['uid']) || create_user_from_omniauth(auth)
  end

    def self.create_with_omniauth(auth)
      create! do |user|
        user.provider = auth["provider"]
        user.uid = auth["uid"]
        user.name = auth["info"]["name"]
      end
    end

Session_controller.rb
def create
    auth = request.env["omniauth.auth"]
    session[:omniauth] = auth.except('extra')
    user = User.sign_in_from_omniauth(auth)
    session[:uid] = user.uid
    redirect_to root_url, notice: "SIGNED IN"
  end

  def destroy
    session[:uid] = nil
    session[:omniauth] = nil
    redirect_to root_url, notice: "SIGNED OUT"
  end

route.rb
Rails.application.routes.draw do

  root to: "home#index"
  get "/auth/:provider/callback" => "sessions#create"
  # get "/sign_out" => "sessions#destroy", :as => :sign_out
  delete 'sign_out', to: 'sessions#destroy'

end

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anton Misyagin, 2016-03-03
@sunnmas

application.html.haml

# The default HTTP method used to sign out a resource. Default is :delete.
  config.sign_out_via = :get

nothing is needed in routes.rb on this issue, I have this:
devise_for :users, :controllers => { :omniauth_callbacks => 'users/omniauth_callbacks', 
    :registrations => 'users/registrations', 
    :confirmations => 'users/confirmations',
    :sessions=> 'users/sessions' },
    :path => '',
    :path_names => { :sign_in => 'вход', :sign_out => 'выход', :sign_up => 'регистрация'}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question