Advertisement
MartinYanchev-99

SessionsController

Dec 15th, 2021
3,082
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rails 0.80 KB | None | 0 0
  1. class SessionsController < ApplicationController
  2.     def new
  3.     end
  4.  
  5.     def create
  6.         user = User.find_by(email: params[:email])
  7.         # printing to see if user.present is true and the password is the one I have set
  8.         puts "==================================="
  9.         puts user.present?
  10.         puts params[:password]
  11.         puts "==================================="
  12.  
  13.         if user.present? && user.authenticate([params[:password]])
  14.             session[:user_id] = user.id
  15.             redirect_to root_path , notice: "Logged in successfully"
  16.         else
  17.              flash[:alert] = "Invalid email or password"
  18.              render :new
  19.         end
  20.     end
  21.     def destroy
  22.         session[:user_id] = nil
  23.         redirect_to root_path, notice: "Logged out"
  24.     end
  25.  
  26.  
  27. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement