authentication

main
Sebskyo 2025-03-01 22:26:34 +01:00
parent 11d208ef56
commit 156632c5d7
10 changed files with 73 additions and 9 deletions

View File

@ -31,7 +31,7 @@ Metrics/BlockLength:
- config/environments/development.rb
Layout/IndentationConsistency:
EnforcedStyle: indented_internal_methods
EnforcedStyle: normal
Layout/MultilineMethodCallIndentation:
EnforcedStyle: indented

View File

@ -2,4 +2,16 @@
class ApplicationController < ActionController::Base
skip_before_action :verify_authenticity_token
before_action :ensure_authentication
protected
def ensure_authentication
redirect_to login_path if current_user.blank?
end
def current_user
@current_user ||= User.find_by(id: session[:current_user_id])
end
end

View File

@ -1,7 +0,0 @@
# frozen_string_literal: true
class AuthenticateController < ApplicationController
def create
# Enable authentication here
end
end

View File

@ -0,0 +1,5 @@
# frozen_string_literal: true
class ConversationsController < ApplicationController
def index; end
end

View File

@ -0,0 +1,22 @@
# frozen_string_literal: true
class SessionsController < ApplicationController
skip_before_action :ensure_authentication
def new; end
def create
user = User.find_or_initialize_by(email: params[:email])
if user.authenticate(params[:password])
session[:current_user_id] = user.id
redirect_to root_path
else
render :new, status: :unauthorized
end
end
def destroy
session[:current_user_id] = nil
redirect_to login_path
end
end

View File

View File

View File

@ -0,0 +1,19 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>chatbot</title>
</head>
<body>
<nav>
<% if @current_user %>
<%= form_with url: logout_path do |form| %>
<%= form.submit "logout" %>
<% end %>
<% end %>
</nav>
<main>
<%= yield %>
</main>
</body>
</html>

View File

@ -0,0 +1,7 @@
<%= form_with do |form| %>
<%= form.label :email, "email" %>
<%= form.text_field :email %>
<%= form.label :password, "password" %>
<%= form.password_field :password %>
<%= form.submit "login" %>
<% end %>

View File

@ -1,5 +1,11 @@
# frozen_string_literal: true
Rails.application.routes.draw do
post :authenticate, to: 'authenticate#create'
root to: redirect('/conversations')
get :login, to: 'sessions#new'
post :login, to: 'sessions#create'
post :logout, to: 'sessions#destroy'
resources :conversations
end