rails RailsでFirebaseUserの作成/取得など

本稿について

本稿はサイト運営者が学んだことなどを記したメモの内容です。
全てが正確な情報とは限りません。ご注意ください。また修正するべき点は適時修正していきます
RubyにはFirebase SDKなどのライブラリは 2020年5月現在存在しないが、FIrebaseUserの作成や取得ができたのでメモ

Gem
“google-api-client” を利用する
“google-cloud-bigquery” でも “google-api-client” がインストールされる

gem 'google-api-client’
~~ or ~~
gem 'google-cloud-bigquery'


GCP_AUTH_KEY_FILEはGoogle Cloud Platformより取得

require 'google/apis/identitytoolkit_v3'

class FirebaseAuth
  def self.create(email, password)
    service = Google::Apis::IdentitytoolkitV3::IdentityToolkitService.new
    auth = Google::Auth::ServiceAccountCredentials.make_creds(
      json_key_io: File.open(Rails.root.join('config/firebase_admin', ENV['GCP_AUTH_KEY_FILE'])),
      scope: ['https://www.googleapis.com/auth/identitytoolkit']
    )
    service.authorization = auth

    user_data = {
      email: email,
      email_verified: true,
      password: password,
      disabled: false
    }
    request = Google::Apis::IdentitytoolkitV3::SignupNewUserRequest.new(user_data)
    service.signup_new_user(request)
  end
end


[参考]

Back