Skip to content

Commit

Permalink
Handle "deny" oauth flow
Browse files Browse the repository at this point in the history
  • Loading branch information
yogodoshi committed Oct 21, 2017
1 parent 2379294 commit 51463b2
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 0 deletions.
5 changes: 5 additions & 0 deletions app/controllers/sessions_controller.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down
4 changes: 4 additions & 0 deletions config/initializers/omniauth.rb
Original file line number Diff line number Diff line change
@@ -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
}
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
10 changes: 10 additions & 0 deletions spec/controllers/sessions_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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" } }

Expand Down

0 comments on commit 51463b2

Please sign in to comment.