Add session & user management in the backend

This commit is contained in:
hannah.dagemark 2025-05-09 14:56:30 +02:00 committed by Hannah Dagemark
commit 372fe53947
12 changed files with 88 additions and 11 deletions

View file

@ -0,0 +1,21 @@
class SessionsController < ApplicationController
def new
end
def create
user = User.find_by(email: params[:email])
if user && user.authenticate(params[:password])
session[:user_id] = user.id
redirect_to root_path, notice: "Logged in!"
else
flash.now[:alert] = "Invalid email or password"
render :new, status: :unprocessable_entity
end
end
def destroy
session[:user_id] = nil
redirect_to root_path
end
end