Class: AuthenticationController

Inherits:
ApplicationController show all
Defined in:
lib/generators/jwt_rails/templates/authentication_controller.rb

Instance Method Summary collapse

Methods inherited from ApplicationController

#authorize_request, #is_owner, #is_owner_object, #not_found

Instance Method Details

#loginObject

POST /auth/login



5
6
7
8
9
10
11
12
13
14
15
# File 'lib/generators/jwt_rails/templates/authentication_controller.rb', line 5

def 
  @user = User.find_by_email(params[:email])
  if @user&.authenticate(params[:password])
    token = JsonWebToken.encode(user_id: @user.id)
    time = Time.now + 24.hours.to_i
    render json: { token: token, exp: time.strftime("%m-%d-%Y %H:%M"),
                   username: @user.username }, status: :ok
  else
    render json: { error: 'unauthorized' }, status: :unauthorized
  end
end