module Api
module V1
module Auth
class RegistrationsController < DeviseTokenAuth::RegistrationsController
private
def sign_up_params
params.permit(:email,:password, :password_confirmation)
end
def account_update_params
params.permit(:email)
end
end
end
end
end
class DeviseTokenAuthCreateUsers < ActiveRecord::Migration[6.0]
def change
create_table(:users) do |t|
## Required
t.string :provider, :null => false, :default => "email"
t.string :uid, :null => false, :default => ""
## Database authenticatable
t.string :encrypted_password, :null => false, :default => ""
## Recoverable
t.string :reset_password_token
t.datetime :reset_password_sent_at
t.boolean :allow_password_change, :default => false
## Rememberable
t.datetime :remember_created_at
## Confirmable
# t.string :confirmation_token
# t.datetime :confirmed_at
# t.datetime :confirmation_sent_at
# t.string :unconfirmed_email # Only if using reconfirmable
## Lockable
# t.integer :failed_attempts, :default => 0, :null => false # Only if lock strategy is :failed_attempts
# t.string :unlock_token # Only if unlock strategy is :email or :both
# t.datetime :locked_at
## User Info
# t.string :name
# t.string :nickname
# t.string :image
t.string :email
## Tokens
t.json :tokens
t.timestamps
end
add_index :users, :email, unique: true
add_index :users, [:uid, :provider], unique: true
add_index :users, :reset_password_token, unique: true
# add_index :users, :confirmation_token, unique: true
# add_index :users, :unlock_token, unique: true
end
end
new_api_v1_user_session GET /api/v1/auth/sign_in(.:format) devise_token_auth/sessions#new
api_v1_user_session POST /api/v1/auth/sign_in(.:format) devise_token_auth/sessions#create
destroy_api_v1_user_session DELETE /api/v1/auth/sign_out(.:format) devise_token_auth/sessions#destroy
new_api_v1_user_password GET /api/v1/auth/password/new(.:format) devise_token_auth/passwords#new
edit_api_v1_user_password GET /api/v1/auth/password/edit(.:format) devise_token_auth/passwords#edit
api_v1_user_password PATCH /api/v1/auth/password(.:format) devise_token_auth/passwords#update
PUT /api/v1/auth/password(.:format) devise_token_auth/passwords#update
POST /api/v1/auth/password(.:format) devise_token_auth/passwords#create
cancel_api_v1_user_registration GET /api/v1/auth/cancel(.:format) api/v1/auth/registrations#cancel
new_api_v1_user_registration GET /api/v1/auth/sign_up(.:format) api/v1/auth/registrations#new
edit_api_v1_user_registration GET /api/v1/auth/edit(.:format) api/v1/auth/registrations#edit
api_v1_user_registration PATCH /api/v1/auth(.:format) api/v1/auth/registrations#update
PUT /api/v1/auth(.:format) api/v1/auth/registrations#update
DELETE /api/v1/auth(.:format) api/v1/auth/registrations#destroy
POST /api/v1/auth(.:format) api/v1/auth/registrations#create
api_v1_auth_validate_token GET /api/v1/auth/validate_token(.:format) devise_token_auth/token_validations#validate_token
概要
Rails APIモードを使用し、devise token authというgemを利用し、ユーザ認証機能を実装する
Rails APIモードとは?
View機能ではなく、JSONレスポンスを返すRailsサーバのようなもの https://railsguides.jp/api_app.html 実装時参考にした記事 https://qiita.com/k-penguin-sato/items/adba7a1a1ecc3582a9c9
実装
devise token authを利用し、token認証によるユーザ認証機能を実装する。 https://github.com/lynndylanhurley/devise_token_auth 実装時参考にした記事 https://kenny27.hatenablog.com/entry/2019/01/29/014725 https://qiita.com/tomokazu0112/items/5fdd6a51a84c520c45b5 https://qiita.com/Masahiro_T/items/6bc49a625b437a7c2f45
結果
Controller、Model、Routingを以下のように実装した