Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
sample_app_ch9/app/helpers/sessions_helper.rb
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
49 lines (38 sloc)
1012 Bytes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module SessionsHelper | |
def sign_in(user) | |
remember_token = User.new_remember_token | |
cookies.permanent[:remember_token] = remember_token | |
user.update_attribute(:remember_token, User.encrypt(remember_token)) | |
self.current_user = user | |
end | |
def signed_in? | |
!current_user.nil? | |
end | |
def current_user | |
remember_token = User.encrypt(cookies[:remember_token]) | |
@current_user ||= User.find_by(remember_token: remember_token) | |
end | |
def current_user=(user) | |
@current_user = user | |
end | |
def current_user?(user) | |
user == current_user | |
end | |
def signed_in_user | |
unless signed_in? | |
store_location | |
redirect_to signin_url, notice: "Please sign in." | |
end | |
end | |
def sign_out | |
self.current_user = nil | |
cookies.delete(:remember_token) | |
end | |
def redirect_back_or(default) | |
redirect_to(session[:return_to] || default) | |
session.delete(:return_to) | |
end | |
def store_location | |
session[:return_to] = request.url if request.get? | |
end | |
end |