aboutsummaryrefslogtreecommitdiff
path: root/app/controllers/sessions_controller.rb
diff options
context:
space:
mode:
Diffstat (limited to 'app/controllers/sessions_controller.rb')
-rw-r--r--app/controllers/sessions_controller.rb27
1 files changed, 27 insertions, 0 deletions
diff --git a/app/controllers/sessions_controller.rb b/app/controllers/sessions_controller.rb
new file mode 100644
index 0000000..b79e3dd
--- /dev/null
+++ b/app/controllers/sessions_controller.rb
@@ -0,0 +1,27 @@
+# frozen_string_literal: true
+
+# SessionsController holds the logic for logging in and out users.
+class SessionsController < ApplicationController
+ skip_before_action :user_authenticated!, only: %i[new create]
+ skip_before_action :set_searches, only: %i[new create]
+
+ # We only need to render its template.
+ def new; end
+
+ # Create a session for the current user.
+ def create
+ user = User.find_by(username: params[:username])
+ if user&.authenticate(params[:password])
+ session[:user_id] = user.id
+ redirect_to root_url
+ else
+ redirect_to root_url, alert: t('sessions.wrong-credentials')
+ end
+ end
+
+ # Destroy the current session in order to log out.
+ def destroy
+ session[:user_id] = nil
+ redirect_to root_url
+ end
+end