diff --git a/app/controllers/sessions_controller.rb b/app/controllers/sessions_controller.rb index 4411820..ca76729 100644 --- a/app/controllers/sessions_controller.rb +++ b/app/controllers/sessions_controller.rb @@ -1,6 +1,11 @@ # frozen_string_literal: true class SessionsController < ApplicationController + def auth_failure + flash[:error] = 'Não foi possível obter suas informações do twitter, precisamos dela para twittar em seu nome' + redirect_to root_path + end + def create user = User.find_or_create_from_auth_hash(auth_hash) session[:user_id] = user.id diff --git a/config/initializers/omniauth.rb b/config/initializers/omniauth.rb index 34f2fe6..dcd22d9 100644 --- a/config/initializers/omniauth.rb +++ b/config/initializers/omniauth.rb @@ -1,3 +1,7 @@ Rails.application.config.middleware.use OmniAuth::Builder do provider :twitter, ENV['TWITTER_API_KEY'], ENV['TWITTER_API_SECRET'] end + +OmniAuth.config.on_failure = Proc.new { |env| + OmniAuth::FailureEndpoint.new(env).redirect_to_failure +} diff --git a/config/routes.rb b/config/routes.rb index ca32840..4a35c27 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -4,5 +4,6 @@ patch '/editar', to: 'users#update' get '/auth/:provider/callback', to: 'sessions#create' + get '/auth/failure', to: 'sessions#auth_failure' delete '/sair', to: 'sessions#destroy', as: :logout end diff --git a/spec/controllers/sessions_controller_spec.rb b/spec/controllers/sessions_controller_spec.rb index a163f5e..0ecc0b3 100644 --- a/spec/controllers/sessions_controller_spec.rb +++ b/spec/controllers/sessions_controller_spec.rb @@ -3,6 +3,16 @@ require 'rails_helper' RSpec.describe SessionsController, type: :controller do + describe 'GET #auth_failure' do + subject! { get :auth_failure } + + it 'redirects to root_path with an error message' do + subject + expect(flash[:error]).to eq('Não foi possível obter suas informações do twitter, precisamos dela para twittar em seu nome') + expect(response).to redirect_to(root_path) + end + end + describe 'GET #create' do let!(:params) { { "oauth_token"=>"biVSlAAAAAAA2xQzAAABXxy0pKU", "oauth_verifier"=>"p858cQzJ7ibg4L2v8qPE69OIjp3kJAKu", "controller"=>"sessions", "action"=>"create", "provider"=>"twitter" } }