Rewritten using quasar framework
This commit is contained in:
14
backend/app/controllers/application_controller.rb
Normal file
14
backend/app/controllers/application_controller.rb
Normal file
@@ -0,0 +1,14 @@
|
||||
class ApplicationController < ActionController::API
|
||||
def authorize_request
|
||||
header = request.headers['Authorization']
|
||||
header = header.split(' ').last if header
|
||||
begin
|
||||
@decoded = JsonWebToken.decode(header)
|
||||
@current_user = User.find(@decoded[:user_id])
|
||||
rescue ActiveRecord::RecordNotFound => e
|
||||
render json: { errors: e.message }, status: :unauthorized
|
||||
rescue JWT::DecodeError => e
|
||||
render json: { errors: e.message }, status: :unauthorized
|
||||
end
|
||||
end
|
||||
end
|
||||
19
backend/app/controllers/auth/login_controller.rb
Normal file
19
backend/app/controllers/auth/login_controller.rb
Normal file
@@ -0,0 +1,19 @@
|
||||
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
|
||||
19
backend/app/controllers/auth/register_controller.rb
Normal file
19
backend/app/controllers/auth/register_controller.rb
Normal file
@@ -0,0 +1,19 @@
|
||||
class Auth::RegisterController < ApplicationController
|
||||
def post
|
||||
@user = User.new(user_params)
|
||||
if @user.save
|
||||
render status: :created
|
||||
else
|
||||
render json: { errors: @user.errors.full_messages },
|
||||
status: :unprocessable_entity
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def user_params
|
||||
params.permit(
|
||||
:username, :email, :password, :password_confirmation
|
||||
)
|
||||
end
|
||||
end
|
||||
0
backend/app/controllers/concerns/.keep
Normal file
0
backend/app/controllers/concerns/.keep
Normal file
Reference in New Issue
Block a user