diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 0d95db2..ce2bb32 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -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 diff --git a/app/controllers/sessions_controller.rb b/app/controllers/sessions_controller.rb index ba061be..b1b7356 100644 --- a/app/controllers/sessions_controller.rb +++ b/app/controllers/sessions_controller.rb @@ -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 diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 2adc274..e4a385d 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -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! diff --git a/app/views/layouts/_footer.html.erb b/app/views/layouts/_footer.html.erb index 7f2b96d..115ac12 100644 --- a/app/views/layouts/_footer.html.erb +++ b/app/views/layouts/_footer.html.erb @@ -14,6 +14,7 @@ -
- <%= link_to 'Login', login_path, class: 'btn btn-outline-primary mr-2' %> - <%= link_to 'Become Member', pets_path, class: 'btn btn-outline-success' %> + + <%= hidden_field_tag :_method, :delete %> + <% if current_user %> + + Hello, <%= current_user.name %>! + + <%= 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 %>
diff --git a/app/views/sessions/login.html.erb b/app/views/sessions/login.html.erb new file mode 100644 index 0000000..e519dde --- /dev/null +++ b/app/views/sessions/login.html.erb @@ -0,0 +1,18 @@ +
+

Log In

+ <%= form_tag '/login', class: 'needs-validation' do %> +
+ <%= label_tag :email, "Username", class: 'form-label' %> + <%= text_field_tag :email, nil, class: 'form-control' %> +
+ +
+ <%= label_tag :password, "Password", class: 'form-label' %> + <%= password_field_tag :password, nil, class: 'form-control' %> +
+ +
+ <%= submit_tag "Log In", class: 'btn btn-primary' %> +
+ <% end %> +
\ No newline at end of file diff --git a/app/views/sessions/new.html.erb b/app/views/sessions/new.html.erb deleted file mode 100644 index 678ed04..0000000 --- a/app/views/sessions/new.html.erb +++ /dev/null @@ -1,17 +0,0 @@ -

Log In

- -<%= form_with url: sessions_path, local: true do |form| %> -
- <%= form.label :email %> - <%= form.email_field :email %> -
- -
- <%= form.label :password %> - <%= form.password_field :password %> -
- -
- <%= form.submit "Log In" %> -
-<% end %> diff --git a/app/views/users/new.html.erb b/app/views/users/new.html.erb index dc74c44..dd21fd0 100644 --- a/app/views/users/new.html.erb +++ b/app/views/users/new.html.erb @@ -1,9 +1,9 @@ -

Sign Up

+

Sign Up

-<%= form_with model: @user, local: true do |form| %> +<%= form_with model: @user, local: true, class: 'needs-validation' do |form| %> <% if @user.errors.any? %> -
-

<%= pluralize(@user.errors.count, "error") %> prohibited this user from being saved:

+ <% end %> -
- <%= form.label :name %> - <%= form.text_field :name %> +
+ <%= form.label :name, class: 'form-label' %> + <%= form.text_field :name, class: 'form-control' %>
-
- <%= form.label :email %> - <%= form.email_field :email %> +
+ <%= form.label :email, class: 'form-label' %> + <%= form.email_field :email, class: 'form-control' %>
-
- <%= form.label :password %> - <%= form.password_field :password %> +
+ <%= form.label :password, class: 'form-label' %> + <%= form.password_field :password, class: 'form-control' %>
-
- <%= form.label :password_confirmation %> - <%= form.password_field :password_confirmation %> +
+ <%= form.label :password_confirmation, class: 'form-label' %> + <%= form.password_field :password_confirmation, class: 'form-control' %>
-
- <%= form.submit "Sign Up" %> +
+ <%= form.submit "Sign Up", class: 'btn btn-primary' %>
<% end %> -

Log In

+

Log In

-<%= form_with url: login_path, local: true do |form| %> -
- <%= form.label :email %> - <%= form.email_field :email %> +<%= form_with url: login_path, local: true, class: 'needs-validation' do |form| %> +
+ <%= form.label :email, class: 'form-label' %> + <%= form.email_field :email, class: 'form-control' %>
-
- <%= form.label :password %> - <%= form.password_field :password %> +
+ <%= form.label :password, class: 'form-label' %> + <%= form.password_field :password, class: 'form-control' %>
-
- <%= form.submit "Log In" %> +
+ <%= form.submit "Log In", class: 'btn btn-primary' %>
<% end %> diff --git a/app/views/users/show.html.erb b/app/views/users/show.html.erb index f9b42e2..88bfd37 100644 --- a/app/views/users/show.html.erb +++ b/app/views/users/show.html.erb @@ -1,9 +1,19 @@ -

<%= @user.name %>

-

Email: <%= @user.email %>

+

<%= @user.name %>

+

Registered e-mail: <%= @user.email %>

+
+ <%= link_to 'Edit User Info', edit_user_path(@user), class: 'btn btn-warning' %> +
-

Pets

-
    +

    My Pets

    +
    <% @user.pets.each do |pet| %> -
  • <%= link_to pet.name, pet_path(pet) %>
  • +
    +
    +
    +
    <%= link_to pet.name, pet_path(pet), class: 'text-decoration-none' %>
    + <%= link_to 'Show Pet', pet_path(pet), class: 'btn btn-primary' %> +
    +
    +
    <% end %> -
+
diff --git a/config/routes.rb b/config/routes.rb index 9bd10bc..d1cc5b3 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -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