require_dependency "jwt"

class SessionsController < ApplicationController
  def create
    user = User.find_by(email: session_params[:email])
    if user && user.authenticate(session_params[:password])
      jwt = JWT.encode(
        {
          user_id: user.id, # the data to encode
          exp: 24.hours.from_now.to_i # the expiration time
        },
        Rails.application.credentials.fetch(:secret_key_base), # the secret key
        "HS256" # the encryption algorithm
      )
      render json: { jwt: jwt, email: user.email, user_id: user.id, isPasswordReset: user.isPasswordReset }, status: :created
    else
      render json: {}, status: :unauthorized
    end
  end

  private

  def session_params
    params.permit(:email, :password)
  end
end
