0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

APIを使った登録方法

Posted at

google,facebook,twitterでユーザー登録?

僕たちが使用してしているwebサイトのほとんどがこのAPI機能を使ってユーザー登録機能を実装しています。

なぜなら、ほとんどの人たちは上記3つのアカウントを保持しているからです。

わざわざ会員登録に毎回、メールアドレスやパスワードを入力するのは面倒ですよね。。。
ユーザーの負担を減らすためにもAPIを使用したユーザー登録をできるようになっておいた方がいいですね。

しかも、めちゃくちゃ簡単ですからね。

まずは各社のAPIの設定からしてみよう!

リファレンス通りにやればめっちゃ簡単にできます!!!

だから、省略!!!

重要なのはキーの設定だけ!

rails v5.2以降はcredential.ymlというファイルに環境変数を設定できるようになりました。

そのため、今回作成したいずれかのアクセスキーをcredential.ymlに記述するだけで、準備は完璧です!!

コード載せたいのですが、アクセスコードなので割愛。。。

そのあとのコードは以下。

class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController
  def google_oauth2
    # You need to implement the method below in your model (e.g. app/models/user.rb)
    session["devise.sns_auth"] = User.from_omniauth(request.env['omniauth.auth'])

    if session["devise.sns_auth"][:user].persisted?
      sign_in_and_redirect session["devise.sns_auth"][:user], event: :authentication
    else
      redirect_to new_regist_path
    end
  end
end

要は、request.env['omniauth.auth']っていうキーバリューの形で送られてきて、
それをsessionに入れ直しているだけです!

そもそもgemなんでめちゃくちゃ楽にできますね・・・

リファレンス見れば全部書いてあります。

あとは条件を指定して、作りたいアプリを作るだけです。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?