Seems like I'm not the only person who has had this issue with other OAuth providers & Devise. I'm trying to authenticate a user with their Tumblr account, store their nickname as the user's username, and store their oauth token & secret so my app can automatically post to Tumblr when they take certain actions.
I've followed various, articles, about, omniauth, and devise (my code is based of the Railscast #235 episode), but I can't quite get the user's data stored in the database. The first user who tries to authenticate "works" fine (it creates a username, but nothing else), but any further users who try to login/authenticate, always get assigned that first user's credentials (and no entry in the database).
I'm using Rails 4.0.0 and Ruby 2.0.0-p195. Do I need to do something with Strong Parameters?
Something is going on behind the scenes with no error messages, and I just don't know how to figure out where to look. Here's my code:
# user.rb
def self.from_omniauth(auth)
if user = User.find_by_username(auth.info.name)
user.provider = auth.provider
user.uid = auth.uid
user
else
where(auth.slice(:provider, :uid)).first_or_create do |user|
user.provider = auth.provider
user.uid = auth.uid
user.username = auth.info.nickname
user.oauth_token = auth.info.credentials.token
user.oauth_token_secret = auth.info.credentials.secret
end
end
end
My controller (basically identical to every other tutuorial I've read)
class OmniauthCallbacksController < Devise::OmniauthCallbacksController
def tumblr
user = User.from_omniauth(request.env["omniauth.auth"])
if user.persisted?
flash.notice = "Signed in Through Tumblr!"
sign_in_and_redirect user
else
session["devise.user_attributes"] = user.attributes
# flash.notice = "You are almost Done! Please provide a password to finish setting up your account"
redirect_to root_url
end
end
end
If anyone has ANY insight into what I may be doing wrong, or even how to figure out how to figure out (I don't mind looking, just out of ideas), I'd really appreciate it.
Thanks!
It appears the answer was fairly simple, but absolutely nothing that I suspected. When I put the ! on my create method, I got an error about my Devise-created index in my User model named: index_users_on_email
.
So I created this migration to remove it:
class RemoveIndexUsersOnEmailFromUsers < ActiveRecord::Migration
def self.up
remove_index(:users, :name => 'index_users_on_email')
end
def self.down
add_index(:users, :name => 'index_users_on_email')
end
end
Voila! Hope that helped someone.