yuyanegishi / issue_check

0 stars 0 forks source link

★Rails Turtorial:ログイン時の挙動 #2

Open yuyanegishi opened 5 years ago

yuyanegishi commented 5 years ago
def create
    user = User.find_by(email: params[:session][:email].downcase)
    if user && user.authenticate(params[:session][:password])
      if user.activated?
        log_in user
        params[:session][:remember_me] == '1' ? remember(user) : forget(user)
        redirect_back_or user
      else
        message  = "Account not activated. "
        message += "Check your email for the activation link."
        flash[:warning] = message
        redirect_to root_url
      end
    else
      flash.now[:danger] = 'Invalid email/password combination'
      render 'new'
    end
end
yuyanegishi commented 5 years ago
user = User.find_by(email: params[:session][:email].downcase)

内容

リファレンス確認 ※Ruby on Rails APIにてfind_byで検索

yuyanegishi commented 5 years ago
if user && user.authenticate(params[:session][:password])

内容

リファレンス確認 ※Ruby on Rails APIにてauthenticateで検索

authenticate(unencrypted_password) 
 =>Returns self if the password is correct, otherwise false.

実行確認 ※rails consoleにて確認

user = User.find_by(email: "example@railstutorial.org")
=> #<User id: 1, name: "Example User", email: "example@railstutorial.org", created_at: "2018-11-22 01:14:16", updated_at: "2018-11-22 01:14:16", password_digest: "$2a$10$V2dr6oBhIMNNoQHjpj82EO9TBCMoiZz8a9oDKrLTB7o...", remember_digest: nil, admin: true, activation_digest: "$2a$10$gyfsdTN05JAwVft13tnJ2eFuKyODY5k.vydgyiTHb2J...", activated: true, activated_at: "2018-11-22 01:14:16", reset_digest: nil, reset_sent_at: nil>

user.authenticate("foobaz")
=> false

user.authenticate("foobar")
=> #<User id: 1, name: "Example User", email: "example@railstutorial.org", created_at: "2018-11-22 01:14:16", updated_at: "2018-11-22 01:14:16", password_digest: "$2a$10$V2dr6oBhIMNNoQHjpj82EO9TBCMoiZz8a9oDKrLTB7o...", remember_digest: nil, admin: true, activation_digest: "$2a$10$gyfsdTN05JAwVft13tnJ2eFuKyODY5k.vydgyiTHb2J...", activated: true, activated_at: "2018-11-22 01:14:16", reset_digest: nil, reset_sent_at: nil>
yuyanegishi commented 5 years ago
if user.activated?

内容

実行結果 ※rails consoleにて確認

user = User.find_by(email: "example@railstutorial.org") 
=> #<User id: 1, name: "Example User", email: "example@railstutorial.org", created_at: "2018-11-22 01:14:16", updated_at: "2018-11-22 01:14:16", password_digest: "$2a$10$V2dr6oBhIMNNoQHjpj82EO9TBCMoiZz8a9oDKrLTB7o...", remember_digest: nil, admin: true, activation_digest: "$2a$10$gyfsdTN05JAwVft13tnJ2eFuKyODY5k.vydgyiTHb2J...", activated: true, activated_at: "2018-11-22 01:14:16", reset_digest: nil, reset_sent_at: nil>

user.activated?
=> true

user.name?
=> true

user.reset_digest?
=> false

user.reset_sent_at? 
=> false
yuyanegishi commented 5 years ago
log_in user
#app/helpers/sessions_helper.rbより抜粋

def log_in(user)
      session[:user_id] = user.id
end

内容

yuyanegishi commented 5 years ago
params[:session][:remember_me] == '1' ? remember(user) : forget(user)

内容①

リファレンス確認 ※ プロを目指す人のためのRuby入門より抜粋

#app/helpers/sessions_helper.rbより抜粋
def remember(user)
      user.remember
      cookies.permanent.signed[:user_id] = user.id
      cookies.permanent[:remember_token] = user.remember_token
end
#app/models/user.rbより抜粋
def User.new_token
    SecureRandom.urlsafe_base64
end

def remember
    self.remember_token = User.new_token
    update_attribute(:remember_digest, User.digest(remember_token))
end

内容② ※remember(user)での挙動

リファレンス確認 ※Ruby on Rails APIにてupdate_attribute、permanentで検索

Updates a single attribute and saves the record. This is especially useful for boolean flags on existing records. Also note that

・Validation is skipped. ・Callbacks are invoked. ・updated_at/updated_on column is updated if that column is available. ・Updates all the attributes that are dirty in this object.

This method raises an ActiveRecord::ActiveRecordError if the attribute is marked as readonly. Also see update_column.

permanent()

Returns a jar that'll automatically set the assigned cookies to have an expiration date 20 years from now.

This jar is only meant for writing. You'll read permanent cookies through the regular accessor. This jar allows chaining with the signed jar as well, so you can set permanent, signed cookies.


### 内容③ ※forget(user)での挙動
*  変数userに代入されたレコードのremember_digestカラムをnilにする。また、user_idとremember_tokenのcookiesを削除する。
```ruby
def forget(user)
        user.forget
        cookies.delete(:user_id)
        cookies.delete(:remember_token)
end
#app/models/user.rbより抜粋
def forget
    update_attribute(:remember_digest, nil)
end
yuyanegishi commented 5 years ago
redirect_back_or user

内容

リファレンス確認 ※Ruby on Rails APIにてredirect_backで検索

The referrer information is pulled from the HTTP Referer (sic) header on the request. This is an optional header and its presence on the request is subject to browser security settings and user preferences. If the request is missing this header, the fallback_location will be used.

yuyanegishi commented 5 years ago
message  = "Account not activated. "
message += "Check your email for the activation link."
flash[:warning] = message
redirect_to root_url

内容

リファレンス確認 ※Ruby on Rails APIにてflashで検索

実行結果 ※flashの部分を変更して表示内容を確認

flash[:warning] = message
flash[:notice] = message
flash[:danger] = message
flash[:success] = message
2018-11-28 14 41 13