Skip to content

Conversation

nguyenthanhhaaa
Copy link

WHAT

  • Implement user sessions and login/logout functionality.

HOW

  1. Sessions

    • Set up a session to maintain state between requests using cookies.
    • Create a helper module to handle session logic (e.g., storing user_id in session).
  2. Create login form

    • Generate a new view (sessions/new.html.erb) with an email/password field.
    • Provide a route (e.g., get "/login") and link to this form in the navbar if user is not logged in.
  3. Authenticate

    • In the SessionsController, find user by email and authenticate password.
    • If valid, store the user’s ID in session[:user_id]; otherwise, render the form with an error message.
  4. Login

    • Once the user is authenticated, redirect them to their profile page or the root path.
    • Display different menu items (e.g., “Account”, “Logout”) based on whether logged_in? is true.
  5. Logout

    • Provide a logout route (DELETE /logout).
    • Clear session[:user_id] and redirect to root path.
    • Update navbar so user sees “Log in” instead of “Account”.

WHY

  • This approach allows users to maintain a logged-in state, securely storing their ID in the session.
  • It improves user experience by customizing the interface based on login status.

Evidence

  • LOG IN
    image
  • Successful Login Screenshot:
    image
    -Successful Logout:
    image

Notes

  • The flash.now method is useful for displaying error messages on rendered pages.
  • Rails session cookies are temporary and can be cleared when the browser is closed or upon logout.
  • Make sure to include method: :delete or data: { turbo_method: :delete } for the logout link (depending on your JS/Turbo setup).

@nguyenthanhhaaa
Copy link
Author

ready

@user = User.find_by id: params[:id]
unless @user
flash[:error] = "User not found."
redirect_to root_path and return

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mặc định line cuối cùng trong method là ruby tự return rồi em, ko cần khai báo

Suggested change
redirect_to root_path and return
redirect_to root_path

end
# Returns true if the user is logged in, false otherwise.
def logged_in?
!current_user.nil?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
!current_user.nil?
current_user.present?

def birthday_within_last_100years
return unless birthday.present?
if birthday < 100.years.ago.to_date
errors.add(:birthday, "must be within the last 100 years")

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i18n cho mấy cái message trong model này đi e nhé

private

def birthday_within_last_100years
return unless birthday.present?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nên đưa điều kiện đơn giản vào lúc khai báo validate ở trên nha
validate :birthday_within_last_100years, if: ->{birthday.present?}

Comment on lines +23 to +24


Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

đừng để line trống nhiều như này e, viết như này model về sau nó sẽ dài dòng không cần thiết

{ prompt: "Select Gender" }, class: "form-control" %>
</div>

<%= f.submit "Create my account", class: "btn btn-primary" %>

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i18n cho hết nhé

@@ -73,3 +78,4 @@ group :test do
gem "selenium-webdriver"
gem "webdrivers"
end
gem "rack-mini-profiler"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lỗi end line nè em, thêm line trống vào cuối file này

user = User.find_by(email: params.dig(:session, :email)&.downcase)
if user && user.authenticate(params.dig(:session, :password))
log_in user # Đây sẽ thiết lập session[:user_id] = user.id
redirect_to user

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thêm flash msg báo login thành công nữa đi e

end
def create
@user = User.new(user_params)
#byebug

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ko có để byebug lại nhé, xóa lun e

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants