20 lines
550 B
Ruby
20 lines
550 B
Ruby
class Auth::LoginController < ApplicationController
|
|
def post
|
|
@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
|
|
|
|
private
|
|
|
|
def login_params
|
|
params.permit(:email, :password)
|
|
end
|
|
end
|