Skip to content

Commit

Permalink
fix: login/logout features
Browse files Browse the repository at this point in the history
  • Loading branch information
sarahcssiqueira committed Sep 6, 2024
1 parent 7e67964 commit 7b3a6f3
Show file tree
Hide file tree
Showing 10 changed files with 116 additions and 89 deletions.
6 changes: 6 additions & 0 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
class ApplicationController < ActionController::Base
# Only allow modern browsers supporting webp images, web push, badges, import maps, CSS nesting, and CSS :has.
allow_browser versions: :modern

helper_method :current_user

def current_user
@current_user ||= User.find(session[:user_id]) if session[:user_id]
end
end
18 changes: 11 additions & 7 deletions app/controllers/sessions_controller.rb
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
class SessionsController < ApplicationController
def new
end

def create
user = User.find_by(email: params[:email])

respond_to do |format|
if user && user.authenticate(params[:password])

session[:user_id] = user.id
format.html { redirect_back(fallback_location: root_path, notice: "Logged in successfully.") }
format.json { render :show, status: :created, location: user }
redirect_to user_path(user), notice: "Logged in successfully."
else
format.html { render :new, status: :unprocessable_entity }
message = "Something went wrong! Make sure your username and password are correct."
redirect_to login_path, notice: message
end
end

def login
if session[:user_id]
redirect_to user_path(session[:user_id]), notice: "You already logged in."
else
render :login
end
end

Expand Down
8 changes: 4 additions & 4 deletions app/helpers/application_helper.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
module ApplicationHelper
def current_user
@current_user ||= User.find(session[:user_id]) if session[:user_id]
def logged_in?
!!session[:user_id]
end

def logged_in?
!!current_user
def current_user
@current_user ||= User.find_by_id(session[:user_id]) if !!session[:user_id]
end

def authenticate_user!
Expand Down
1 change: 1 addition & 0 deletions app/views/layouts/_footer.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
<ul class="list-unstyled">
<li><%= link_to 'Home', root_path, class: 'text-white' %></li>
<li><%= link_to 'Pets', pets_path, class: 'text-white' %></li>
<li><%= link_to 'Contribute', contribute_guide_path, target: '_blank', class: 'text-white' %></li>
<%
=begin%>
<li><%= link_to 'Privacy Policy', privacy_policy_path, class: 'text-white' %></li>
Expand Down
24 changes: 18 additions & 6 deletions app/views/layouts/_navbar.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,27 @@
<%= link_to 'Home', root_path, class: 'nav-link active' %>
</li>
<li class="nav-item">
<%= link_to 'Pets', pets_path, class: 'nav-link active' %>
<%= link_to 'Pet Members', pets_path, class: 'nav-link active' %>
</li>
<li class="nav-item">
<%= link_to 'Open Source', github_repo_path, class: 'nav-link active' %>
<li class="nav-item">
<%= link_to 'Open Source', github_repo_path, class: 'nav-link active', target: '_blank' %>
</li>
<li class="nav-item">
<%= link_to "Register new pet for Free", new_pet_path, class: "btn btn-success mt-3" %>
</li>
</ul>
<form class="">
<%= link_to 'Login', login_path, class: 'btn btn-outline-primary mr-2' %>
<%= link_to 'Become Member', pets_path, class: 'btn btn-outline-success' %>
<form class="form-inline" method="post" action="<%= logout_path %>">
<%= hidden_field_tag :_method, :delete %>
<% if current_user %>
<span class="navbar-text mr-3">
Hello, <%= current_user.name %>!
</span>
<%= link_to 'Dashboard', login_path, class: 'btn btn-outline-primary mr-2' %>
<%= submit_tag 'Logout', class: 'btn btn-outline-success' %>
<% else %>
<%= link_to 'Login', login_path, class: 'btn btn-outline-primary mr-2' %>
<%= link_to 'Become Member', new_user_path, class: 'btn btn-outline-success' %>
<% end %>
</form>

</div>
Expand Down
18 changes: 18 additions & 0 deletions app/views/sessions/login.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<div class="mb-4">
<h2 class="mb-3">Log In</h2>
<%= form_tag '/login', class: 'needs-validation' do %>
<div class="mb-3">
<%= label_tag :email, "Username", class: 'form-label' %>
<%= text_field_tag :email, nil, class: 'form-control' %>
</div>

<div class="mb-3">
<%= label_tag :password, "Password", class: 'form-label' %>
<%= password_field_tag :password, nil, class: 'form-control' %>
</div>

<div class="mb-3">
<%= submit_tag "Log In", class: 'btn btn-primary' %>
</div>
<% end %>
</div>
17 changes: 0 additions & 17 deletions app/views/sessions/new.html.erb

This file was deleted.

56 changes: 28 additions & 28 deletions app/views/users/new.html.erb
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<h1>Sign Up</h1>
<h1 class="mb-4">Sign Up</h1>

<%= form_with model: @user, local: true do |form| %>
<%= form_with model: @user, local: true, class: 'needs-validation' do |form| %>
<% if @user.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@user.errors.count, "error") %> prohibited this user from being saved:</h2>
<div class="alert alert-danger" role="alert">
<h4 class="alert-heading"><%= pluralize(@user.errors.count, "error") %> prohibited this user from being saved:</h4>
<ul>
<% @user.errors.full_messages.each do |message| %>
<li><%= message %></li>
Expand All @@ -12,45 +12,45 @@
</div>
<% end %>

<div class="field">
<%= form.label :name %>
<%= form.text_field :name %>
<div class="mb-3">
<%= form.label :name, class: 'form-label' %>
<%= form.text_field :name, class: 'form-control' %>
</div>

<div class="field">
<%= form.label :email %>
<%= form.email_field :email %>
<div class="mb-3">
<%= form.label :email, class: 'form-label' %>
<%= form.email_field :email, class: 'form-control' %>
</div>

<div class="field">
<%= form.label :password %>
<%= form.password_field :password %>
<div class="mb-3">
<%= form.label :password, class: 'form-label' %>
<%= form.password_field :password, class: 'form-control' %>
</div>

<div class="field">
<%= form.label :password_confirmation %>
<%= form.password_field :password_confirmation %>
<div class="mb-3">
<%= form.label :password_confirmation, class: 'form-label' %>
<%= form.password_field :password_confirmation, class: 'form-control' %>
</div>

<div class="actions">
<%= form.submit "Sign Up" %>
<div class="mb-3">
<%= form.submit "Sign Up", class: 'btn btn-primary' %>
</div>
<% end %>

<h1>Log In</h1>
<h1 class="mb-4">Log In</h1>

<%= form_with url: login_path, local: true do |form| %>
<div class="field">
<%= form.label :email %>
<%= form.email_field :email %>
<%= form_with url: login_path, local: true, class: 'needs-validation' do |form| %>
<div class="mb-3">
<%= form.label :email, class: 'form-label' %>
<%= form.email_field :email, class: 'form-control' %>
</div>

<div class="field">
<%= form.label :password %>
<%= form.password_field :password %>
<div class="mb-3">
<%= form.label :password, class: 'form-label' %>
<%= form.password_field :password, class: 'form-control' %>
</div>

<div class="actions">
<%= form.submit "Log In" %>
<div class="mb-3">
<%= form.submit "Log In", class: 'btn btn-primary' %>
</div>
<% end %>
22 changes: 16 additions & 6 deletions app/views/users/show.html.erb
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
<h1><%= @user.name %></h1>
<p>Email: <%= @user.email %></p>
<h1 class="mb-4"><%= @user.name %></h1>
<p class="mb-4">Registered e-mail: <%= @user.email %></p>
<div class="mb-4">
<%= link_to 'Edit User Info', edit_user_path(@user), class: 'btn btn-warning' %>
</div>

<h2>Pets</h2>
<ul>
<h2 class="mb-4">My Pets</h2>
<div class="row">
<% @user.pets.each do |pet| %>
<li><%= link_to pet.name, pet_path(pet) %></li>
<div class="col-md-4 mb-4">
<div class="card">
<div class="card-body">
<h5 class="card-title"><%= link_to pet.name, pet_path(pet), class: 'text-decoration-none' %></h5>
<%= link_to 'Show Pet', pet_path(pet), class: 'btn btn-primary' %>
</div>
</div>
</div>
<% end %>
</ul>
</div>
35 changes: 14 additions & 21 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,30 +1,23 @@
Rails.application.routes.draw do
get "up" => "rails/health#show", as: :rails_health_check
# Render dynamic PWA files from app/views/pwa/*
get "service-worker" => "rails/pwa#service_worker", as: :pwa_service_worker
get "manifest" => "rails/pwa#manifest", as: :pwa_manifest
# Users routes
resources :users, only: [ :index, :new, :create, :edit, :update, :show, :destroy ]

resources :users, only: [ :new, :create, :show, :delete ]
resources :pets, only: [ :index, :show, :new, :create, :edit, :update, :destroy ]
resources :sessions, only: [ :new, :create, :destroy ]
resources :privacy_policy

get "home/index"
root "home#index"

# Defines a route for "/pets"
get "/pets", to: "pets#index"
get "/users", to: "users#index"

# Routes for sessions (login and logout)
get "/login", to: "sessions#new", as: "login"
# Sessions routes
get "/login", to: "sessions#login"
post "/login", to: "sessions#create"
delete "/logout", to: "sessions#destroy", as: "logout"
delete "/logout", to: "sessions#destroy"

# Pets Routes
resources :pets, only: [ :index, :show, :new, :create, :edit, :update, :destroy ]

# Privacy Policy
get "/privacy-policy", to: "privacy_policy#index"
resources :privacy_policy, only: [ :index ]

# Homepage
get "home/index"
root "home#index"

# Define a route for the external URL
# External Routes
get "github_repo", to: proc { |env| [ 302, { "Location" => "https://github.com/sarahcssiqueira/pet-tag-generator" }, [] ] }
get "contribute_guide", to: proc { |env| [ 302, { "Location" => "https://github.com/sarahcssiqueira/pet-tag-generator/blob/main/CONTRIBUTING.md" }, [] ] }
end

0 comments on commit 7b3a6f3

Please sign in to comment.