1
3

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.

Rails + deviseで会員登録APIを自前で構築する時の肝

Posted at
controller/api/user_controller.rb
def create
  user = where(email: user_params[:email]).first
    if user
      if user.confirmed?
        fail "already exists"
      else
        user.password = user_params[:password]
        user.send(:generate_confirmation_token!) # protectedメソッドなのでsendを使っている
      end
    else
      user = User.new(
        email: user_params[:email],
        password: user_params[:password]
      )
      user.skip_confirmation_notification!
      user.save!
    end
  confirmation_token = user.instance_variable_get('@raw_confirmation_token')
  confirmation_url = api_user_confirmation_url(user, confirmation_token: confirmation_token)
  UserAppMailer.confirmation_instruction(user, confirmation_url).deliver
  render :success
end

def user_params
  params.require(:user).permit(:email, :password)
end
  1. 新しく作成したUserモデルのインスタンスからconfirmation_tokenを取り出します
  2. userインスタンスと上で取り出したconfirmation_tokenを使ってconfirmationアクションへのurlを作ります
  3. userにconfirmationアクションへのurlをメールで送信します
  4. 成功した旨のJSONレスポンスを返します

これでいけた^ ^

1
3
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
1
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?