I am trying to allow users to sign in through Twitter but I am getting the error: Type error in SessionsController. I get this error whenever I am redirected to: https://api.twitter.com/oauth/authenticate so the user can be authenticated. I should be redirected to the Twitter "Authorize App" page after being authenticated, but I am receiving the full error: "TypeError in SessionsController#create: exception class/object expected". I am using the omniauth_twitter and devise gems for this functionality. Am I missing something that I need to do for devise in the routes.rb file possibly?
Also it makes it through the create method(in the SessionController) as it does end up redirecting to the root page, but I don't see the user created in the database.
Session Controller
class SessionsController < ApplicationController
def create
@user = User.find_or_create_from_auth_hash(auth_hash)
session[:user_id] = @user.id
redirect_to root_path
end
protected
def auth_hash
request.env['omniauth.auth']
end
end
User Model
class User < ActiveRecord::Base
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
before_create :generate_auth_token
def self.find_or_create_from_auth_hash(auth_hash)
user = where(provider: auth_hash.provider, uid: auth_hash.uid).first_or_create
user.update(
name: auth_hash.info.name,
avatar: auth_hash.info.image,
token: auth_hash.credentials.token,
secret: auth_hash.credentials.secret)
user
end
def generate_auth_token
loop do
self.auth_token = SecureRandom.base64(64)
break unless User.find_by(auth_token)
end
end
end
Routes.rb
Rails.application.routes.draw do
get 'auth/:provider/callback', to: 'sessions#create'
resources :tweets
get 'signup/new'
get 'signup/create'
devise_for :users
resources :contacts
end
Also the User is not showing in the database it should be saved down
Loading production environment (Rails 4.2.1)
[1] pry(main)> User.all
User Load (3.2ms) SELECT "users".* FROM "users"
=> []
You're seeing this error because 1) you're raising an exception in SessionsController#create (
raise :test
) and 2)raise
is not expecting a symbol, which is why you're seeing that cryptic error message.You should remove the call to raise if it's not actually supposed to be there or, if it is, call
raise
with a legitimate argument (string, exception instance, etc.) andrescue
accordingly.