LoginSignup
0
1

More than 3 years have passed since last update.

sorceryを使用して、ユーザー機能を作成する

Posted at

自分用です!!

sorceryを使ってログイン機能を作成していきます。

sorceryをインストール

Gemfile
gem sorcery

bundele installします。

ここで、生成されるpasswordとpassword_confirmationはcrypted_passwordカラムに対する仮想属性となっています。

user.rbにif: -> { new_record? || changes[:crypted_password] }が記載されています。
これにより、登録したユーザーがパスワード以外のプロフィール項目を更新したい場合に、パスワードの入力を省略させることができます。

ajax通信にしない場合、
form_withでは、local: trueが指定されていること。

ログインしているか、の判定では、<% if current_user %>ではなく、sorceryで使える、
<% if logged_in %>を使っていく。

新規登録ページのルーティングにはresourcesを使う。

routes.rb
resources :users, only: %i[new create]

user_sessions controllerでは、newアクションと、createアクションを作成
ここでは、loginというsorceryで使えるメソッドを使いログインします。

user_sessions_controller.rb
 def create 

     @user = login(params[:email], params[:password]) 
     if @user 
       redirect_back_or_to root_path 
     else 
       render :new 
     end 
   end 
0
1
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
1