aboutsummaryrefslogtreecommitdiff
path: root/app/controllers/sessions_controller.rb
diff options
context:
space:
mode:
authorMiquel Sabaté Solà <msabate@suse.com>2024-03-13 18:03:43 +0100
committerMiquel Sabaté Solà <msabate@suse.com>2024-03-13 18:03:43 +0100
commita7791635aa98f012d35f97c3c5288494bbb28e38 (patch)
treebad987e6b4d10ad2f7cd02e40658d464907595ee /app/controllers/sessions_controller.rb
downloadoperum-a7791635aa98f012d35f97c3c5288494bbb28e38.tar.gz
operum-a7791635aa98f012d35f97c3c5288494bbb28e38.zip
Initial commit
Signed-off-by: Miquel Sabaté Solà <msabate@suse.com>
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