質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.47%
Ruby on Rails 5

Ruby on Rails 5は、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Devise

Deviseとは、Ruby-on-Railsの認証機能を追加するプラグインです。

Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

Google API

Googleは多種多様なAPIを提供していて、その多くはウェブ開発者向けのAPIです。それらのAPIは消費者に人気なGoogleのサービス(Google Maps, Google Earth, AdSense, Adwords, Google Apps,YouTube等)に基づいています。

Q&A

解決済

1回答

884閲覧

sns認証 アカウント未登録時

r.h

総合スコア3

Ruby on Rails 5

Ruby on Rails 5は、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Devise

Deviseとは、Ruby-on-Railsの認証機能を追加するプラグインです。

Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

Google API

Googleは多種多様なAPIを提供していて、その多くはウェブ開発者向けのAPIです。それらのAPIは消費者に人気なGoogleのサービス(Google Maps, Google Earth, AdSense, Adwords, Google Apps,YouTube等)に基づいています。

0グッド

0クリップ

投稿2021/12/21 13:37

前提・実現したいこと

deviseを利用し、sns認証機能を実装しているのですがログイン画面にgoogleでサインインのリンクを配置し、クリックしてgoogleアカウントを選択した時、サイトにアカウント登録がされていればそのままログイン、されていなければ新しくクリエイトされるようにしたいのですがgoogleから必要な情報をひっぱって来れていないようで何も入力されていない新規会員登録が表示されてしまいます。

発生している問題・エラーメッセージ

会員未登録の際googleアカウントの情報を利用して新規会員登録したいができない。

該当のソースコード

class Customer::OmniauthCallbacksController < Devise::OmniauthCallbacksController def google_oauth2 # You need to implement the method below in your model (e.g. app/models/.rb) @customer = Customer.from_omniauth(request.env['omniauth.auth']) if @customer.persisted? flash[:notice] = I18n.t 'devise.omniauth_callbacks.success', kind: 'Google' sign_in_and_redirect @customer, event: :authentication else session['devise.google_data'] = request.env['omniauth.auth'].except(:extra) # Removing extra as it can overflow some session stores redirect_to new_customer_registration_path end end # You should configure your model like this: # devise :omniauthable, omniauth_providers: [:twitter] # You should also create an action method in this controller like this: # def twitter # end # More info at: # https://github.com/heartcombo/devise#omniauth # GET|POST /resource/auth/twitter # def passthru # super # end # GET|POST /users/auth/twitter/callback # def failure # super # end # protected # The path used when OmniAuth fails # def after_omniauth_failure_path_for(scope) # super(scope) # end end
class Customer < ApplicationRecord # Include default devise modules. Others available are: # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable devise :database_authenticatable, :registerable, :recoverable, :rememberable, :validatable, :omniauthable, omniauth_providers: [:google_oauth2] has_many :contributions, dependent: :destroy has_many :favorites, dependent: :destroy attachment :customer_image def self.from_omniauth(access_token) data = access_token.info customer = Customer.where(email: data['email']).first unless customer customer = Customer.create( last_name: data['family_name'], # last_name: data['last_name'], first_name: data['first_name'], email: data['email'], password: Devise.friendly_token[0,20] ) end customer end # def self.from_omniauth(auth) # where(provider: auth.provider, uid: auth.uid).first_or_create do |user| # user.email = auth.info.email # user.password = Devise.friendly_token[0,20] # user.name = auth.info.name # assuming the user model has a name # end # end def already_favorited?(contribution) favorites.where(contribution_id: contribution.id).exists? end def active_for_authentication? super && (!self.is_deleted) end validates :last_name, presence: true validates :first_name, presence: true validates :last_name_kana, presence: true validates :first_name_kana, presence: true end

試したこと

googleの認証画面は表示されるのでdevise.rbの記述等は問題ないと思います。
下のコードのself.from_omniauthメソッド内でunless以下の記述に問題があると思いますがどのように記述すればいいかがわからず教えていただきたいです。

補足情報(FW/ツールのバージョンなど)

rails 5.2.5
devise

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

自己解決

def self.find_or_create_for_oauth(auth)
find_or_create_by!(email: auth.info.email) do |customer|
customer.provider = auth.provider
customer.uid = auth.uid
customer.last_name = auth.info.last_name
customer.first_name = auth.info.first_name

# ↓googleでサインアップする際はフリガナカラムに名前がそのまま代入される customer.last_name_kana = auth.info.last_name customer.first_name_kana = auth.info.first_name customer.email = auth.info.email password = Devise.friendly_token[0..5] logger.debug password customer.password = password end

end

上記メソッドの記述に変更して解決しました。

投稿2021/12/31 10:06

r.h

総合スコア3

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.47%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問