Class: SessionsController

Inherits:
ApplicationController
  • Object
show all
Defined in:
app/controllers/sessions_controller.rb

Instance Method Summary collapse

Instance Method Details

#createObject



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'app/controllers/sessions_controller.rb', line 4

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