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

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

新規登録して質問してみよう
ただいま回答率
85.50%
Google API

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

Facebook

Facebookは、実名登録制のSNS(ソーシャル・ネットワーキング・サービス)です。開発者用のデベロッパーサイトが存在し、一般ユーザーによるFacebook向けアプリケーション開発が可能です。

Ruby on Rails

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

API

APIはApplication Programming Interfaceの略です。APIはプログラムにリクエストされるサービスがどのように動作するかを、デベロッパーが定めたものです。

Q&A

0回答

1015閲覧

SNS認証後(facebook,Google)一旦ログインさせずに追加のユーザー情報を登録後に新規登録させたい

ruorin

総合スコア5

Google API

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

Facebook

Facebookは、実名登録制のSNS(ソーシャル・ネットワーキング・サービス)です。開発者用のデベロッパーサイトが存在し、一般ユーザーによるFacebook向けアプリケーション開発が可能です。

Ruby on Rails

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

API

APIはApplication Programming Interfaceの略です。APIはプログラムにリクエストされるサービスがどのように動作するかを、デベロッパーが定めたものです。

0グッド

0クリップ

投稿2019/12/27 05:36

#前提・実現したいこと
メルカリの新規登録時のSNS認証の様に認証後一旦ログインさせずに追加のユーザー情報登録させた後に新規登録させたい。
SNS認証での登録自体は完了。
#発生している問題
私の勉強不足でロジックが思いつきません。
色々調べてはおりますが、これといったものが見当たらないため
大変恐縮ではございますがお力添えいただけると幸いです。宜しくお願い致します。
#該当のソースコード
以下は認証用に書いたコード
⬇︎OmniauthCallコントローラー

class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController def facebook callback_for(:facebook) end def google_oauth2 callback_for(:google) end def callback_for(provider) @user = User.find_oauth(request.env["omniauth.auth"]) if @user.persisted? sign_in @user redirect_to api_signup_index_path set_flash_message(:notice, :success, kind: "#{provider}".capitalize) if is_navigational_format? else session["devise.#{provider}_data"] = request.env["omniauth.auth"].except("extra") redirect_to new_user_registration_path end end def failure redirect_to root_path end end ``` ⬇️ユーザーモデル ```ここに言語を入力 class User < ApplicationRecord # Include default devise modules. Others available are: # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable validate :email_presence has_one :profile, dependent: :destroy has_one :address, dependent: :destroy has_many :items, dependent: :destroy accepts_nested_attributes_for :profile accepts_nested_attributes_for :address devise :database_authenticatable, :registerable, :recoverable, :rememberable, :validatable, :omniauthable, omniauth_providers: [:facebook, :google_oauth2] # Userモデルをomniauthableにすると、config/routes.rbにdevise_for :usersを記述することで以下のURLメソッドがDeviseによって作成されます。 # user_omniauth_authorize_path(provider) # user_omniauth_callback_path(provider) has_many :sns_credential validates :nickname, presence: true, length: { maximum: 20 } VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+.[a-z]+\z/i validates :nickname, presence: true, length: { maximum: 20 } validates :email, presence: true, uniqueness: true, format: { with: VALID_EMAIL_REGEX } validates :password, presence: true,length: { minimum: 6 , maximum: 128 } def email_presence return if email.present? errors.add(:base, "このメールアドレスは既に使われております。") end # コールバックされた値のproviderおよびuidからDBを検索し、条件に合わせてコントローラに返す。 # controllerのcallback_for(provider)内から呼び出される。 # controllerのrequest.env("omniauth.auth")が引数で引き継がれる。env() def self.find_oauth(auth) uid = auth.uid provider = auth.provider snscredential = SnsCredential.where(uid: uid, provider: provider).first if snscredential.present? user = User.where(id: snscredential.user_id).first else user = User.where(email: auth.info.email).first if user.present? SnsCredential.create( uid: uid, provider: provider, user_id: user.id ) else user = User.create( nickname: auth.info.name, email: auth.info.email, password: Devise.friendly_token[0, 20], ) SnsCredential.create( uid: uid, provider: provider, user_id: user.id ) end end return user end end ``` ⬇︎SNS認証後にこのコントローラーのパラメーターを認証情報に追加させて登録させたい ```ここに言語を入力 コードclass SignupController < ApplicationController before_action :save_to_session, only: :step2 def top end def step1 #ユーザー・本名、誕生日(Proifile)入力画面 @user = User.new @user.build_profile end def api #メールアドレス,facebook,google @user = User.new @user.build_profile end def save_to_session #email重複時のバリテーションエラー用 session[:nickname] = user_params[:nickname] session[:email] = user_params[:email] session[:password] = user_params[:password] session[:profile_attributes_after_step1] = user_params[:profile_attributes] @user = User.new( nickname: user_params[:nickname], email: user_params[:email], password: user_params[:password], ) render '/signup/step1' unless @user.valid? end def step2 #携帯番号入力画面 @user = User.new @user.build_profile end def step3 #住所入力画面 session[:profile_attributes_after_step2] = user_params[:profile_attributes] session[:profile_attributes_after_step2].merge!(session[:profile_attributes_after_step1]) @user = User.new @user.build_address end def step4 #クレジットカード登録画面 session[:address_attributes_after_step3] = user_params[:address_attributes] @user = User.new @user.build_profile @user.build_address end def done #登録完了画面 sign_in User.find(session[:id]) unless user_signed_in? #登録後ユーザーIDをsessionで持たせてログイン状態にさせる end def create session[:profile_attributes_after_step4] = user_params[:profile_attributes] session[:profile_attributes_after_step4].merge!(session[:profile_attributes_after_step2]) @user = User.new( email: session[:email], password: session[:password], nickname: session[:nickname], ) @user.build_address(session[:address_attributes_after_step3]) @user.build_profile(session[:profile_attributes_after_step4]) if @user.save session[:id] = @user.id redirect_to done_signup_index_path else render '/signup/step1' #登録に不備があれば最初から入力し直す end end private def user_params params.require(:user).permit( :email, :password, :nickname, profile_attributes: [:family_name, :first_name, :family_name_kana , :first_name_kana, :birthday, :mobile_phone,:card_number, :expiration_date, :security_code], address_attributes: [:zip_code, :prefecture, :city, :block, :building, :home_phone] ) end end ```

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

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

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

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

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

guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

会員登録して回答してみよう

アカウントをお持ちの方は

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

ただいまの回答率
85.50%

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

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

質問する

関連した質問