moke2017/04/05 10:23Oauth認証で、ふらっと立ち寄る人も多いだろうし、一々ユーザーに登録する必要ないよねー。ってことで、デフォルトではユーザーが作成されません、。リンク先のページのように自主的にユーザーを作成して登録してください。
NishidaRyu416_2017/04/05 10:52はい。 app/models/user.rb class User < ActiveRecord::Base # Include default devise modules. Others available are: # :confirmable, :lockable, :timeoutable and :omniauthable devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable,:omniauthable has_many :words has_many :groups has_many :tests has_many :following_relationships, class_name: "Relationship", foreign_key: "follower_id", dependent: :destroy has_many :following_users, through: :following_relationships, source: :followed has_many :followed_relationships, class_name: "Relationship", foreign_key: "followed_id", dependent: :destroy has_many :followed_users, through: :followed_relationships, source: :follower has_many :favorites, dependent: :destroy has_many :favorite_users, through: :favorites, source: :word has_many :tests, dependent: :destroy has_many :tests_users, through: :favorites, source: :word has_many :relation_groups,dependent: :destroy has_many :relation_users,through: :relation_groups,source: :word # 他のユーザーをフォローする def follow(other_user) following_relationships.find_or_create_by(followed_id: other_user.id) end # フォローしているユーザーをアンフォローする def unfollow(other_user) following_relationship = following_relationships.find_by(followed_id: other_user.id) following_relationship.destroy if following_relationship end # あるユーザーをフォローしているかどうか? def following?(other_user) following_users.include?(other_user) end def self.find_for_facebook_oauth(auth, signed_in_resource=nil) user = User.where(:provider => auth.provider, :uid => auth.uid).first unless user user = User.create(name: auth.extra.raw_info.name, provider: auth.provider, uid: auth.uid, email: auth.info.email, password: Devise.friendly_token[0,20] ) end user end def self.find_for_twitter_oauth(auth, signed_in_resource=nil) user = User.where(:provider => auth.provider, :uid => auth.uid).first unless user user = User.create(name: auth.info.nickname, provider: auth.provider, uid: auth.uid, email: User.create_unique_email, password: Devise.friendly_token[0,20] ) end user end # 通常サインアップ時のuid用、Twitter OAuth認証時のemail用にuuidな文字列を生成 def self.create_unique_string SecureRandom.uuid end # twitterではemailを取得できないので、適当に一意のemailを生成 def self.create_unique_email User.create_unique_string + "@example.com" end end こんな感じで、ユーザーをいちいち作成しているので、idが、何故付与されないのか、疑問に思っているのです。
NishidaRyu416_2017/04/05 16:53本当に不思議で、emailアドレス元にUserを作成したところidに2が付与されていて、きちんとページにアクセスできました。しかし、Oauth認証で作られた方にはなぜかきちんとアクセスできません。 どのような理由が考えられますか?
NishidaRyu416_2017/04/05 16:57なぜか,twitterだとうまくいきます. facebookだとhttp://localhost:3000/#_=_というURLが帰ってくるのでこれが原因かもしれません
moke2017/04/06 00:55やはり気になるのがidが付与されないという点です、データベース上にはidの無いデータが登録されているのですか? そんなわけなくてcreateが失敗しているのですよね。 createをcreate!に変更してエラーコードを書くか 一番怪しい auth.extra.raw_info.nameを一旦削って登録してみて下さい あと、余談ですが create_unique_string等はインスタンスメソッドで良いのでは?
NishidaRyu416_2017/04/06 03:47idが付与されてました User.find(1) User Load (0.3ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 1]] => #<User id: 1, email: "786721651482495-facebook@example.com", created_at: "2017-04-04 14:30:25", updated_at: "2017-04-06 03:45:17", uid: "786721651482495", provider: "facebook", name: nil, location: nil, profile: nil>
moke2017/04/07 01:09auth.extra.raw_info.name が原因であっていたみたいですね。 facebookのauth構造が変わっているのかもしれません。 あとはご自分で調べてみてください。 問題の切り分けができて良かったですね。
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/04/05 10:15
2017/04/05 10:23
2017/04/05 10:52
2017/04/05 12:15
2017/04/05 16:53
2017/04/05 16:57
2017/04/06 00:55
2017/04/06 03:47
2017/04/06 05:36
2017/04/06 08:11
2017/04/06 08:21
2017/04/07 01:09
2017/04/07 07:25