Open yuyanegishi opened 5 years ago
user = User.find_by(email: params[:session][:email].downcase)
Finds the first record matching the specified conditions.
There is no implied ordering so if order matters, you should specify it yourself.
If no record is found, returns nil.
if user && user.authenticate(params[:session][:password])
authenticate(unencrypted_password)
=>Returns self if the password is correct, otherwise false.
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>
if user.activated?
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
log_in user
#app/helpers/sessions_helper.rbより抜粋
def log_in(user)
session[:user_id] = user.id
end
params[:session][:remember_me] == '1' ? remember(user) : forget(user)
#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
update_attribute(name, value)
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
redirect_back_or user
Redirects the browser to the page that issued the request (the referrer) if possible,
otherwise redirects to the provided default fallback location.
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.
message = "Account not activated. "
message += "Check your email for the activation link."
flash[:warning] = message
redirect_to root_url
Access the contents of the flash.
Use flash["notice"] to read a notice you put there or flash["notice"] = "hello" to put a new one.
flash[:warning] = message
flash[:notice] = message
flash[:danger] = message
flash[:success] = message