Rewritten using quasar framework
This commit is contained in:
4
backend/app/channels/application_cable/channel.rb
Normal file
4
backend/app/channels/application_cable/channel.rb
Normal file
@@ -0,0 +1,4 @@
|
||||
module ApplicationCable
|
||||
class Channel < ActionCable::Channel::Base
|
||||
end
|
||||
end
|
||||
4
backend/app/channels/application_cable/connection.rb
Normal file
4
backend/app/channels/application_cable/connection.rb
Normal file
@@ -0,0 +1,4 @@
|
||||
module ApplicationCable
|
||||
class Connection < ActionCable::Connection::Base
|
||||
end
|
||||
end
|
||||
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
7
backend/app/jobs/application_job.rb
Normal file
7
backend/app/jobs/application_job.rb
Normal file
@@ -0,0 +1,7 @@
|
||||
class ApplicationJob < ActiveJob::Base
|
||||
# Automatically retry jobs that encountered a deadlock
|
||||
# retry_on ActiveRecord::Deadlocked
|
||||
|
||||
# Most jobs are safe to ignore if the underlying records are no longer available
|
||||
# discard_on ActiveJob::DeserializationError
|
||||
end
|
||||
13
backend/app/lib/json_web_token.rb
Normal file
13
backend/app/lib/json_web_token.rb
Normal file
@@ -0,0 +1,13 @@
|
||||
class JsonWebToken
|
||||
SECRET_KEY = Rails.application.secrets.secret_key_base.to_s
|
||||
|
||||
def self.encode(payload, exp = 24.hours.from_now)
|
||||
payload[:exp] = exp.to_i
|
||||
JWT.encode(payload, SECRET_KEY)
|
||||
end
|
||||
|
||||
def self.decode(token)
|
||||
decoded = JWT.decode(token, SECRET_KEY)[0]
|
||||
HashWithIndifferentAccess.new decoded
|
||||
end
|
||||
end
|
||||
4
backend/app/mailers/application_mailer.rb
Normal file
4
backend/app/mailers/application_mailer.rb
Normal file
@@ -0,0 +1,4 @@
|
||||
class ApplicationMailer < ActionMailer::Base
|
||||
default from: "from@example.com"
|
||||
layout "mailer"
|
||||
end
|
||||
3
backend/app/models/application_record.rb
Normal file
3
backend/app/models/application_record.rb
Normal file
@@ -0,0 +1,3 @@
|
||||
class ApplicationRecord < ActiveRecord::Base
|
||||
primary_abstract_class
|
||||
end
|
||||
0
backend/app/models/concerns/.keep
Normal file
0
backend/app/models/concerns/.keep
Normal file
7
backend/app/models/user.rb
Normal file
7
backend/app/models/user.rb
Normal file
@@ -0,0 +1,7 @@
|
||||
class User < ApplicationRecord
|
||||
has_secure_password
|
||||
validates :email, presence: true, uniqueness: true
|
||||
validates :email, format: { with: URI::MailTo::EMAIL_REGEXP }
|
||||
validates :username, presence: true
|
||||
validates :password, presence: true
|
||||
end
|
||||
13
backend/app/views/layouts/mailer.html.erb
Normal file
13
backend/app/views/layouts/mailer.html.erb
Normal file
@@ -0,0 +1,13 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
||||
<style>
|
||||
/* Email styles need to be inline */
|
||||
</style>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<%= yield %>
|
||||
</body>
|
||||
</html>
|
||||
1
backend/app/views/layouts/mailer.text.erb
Normal file
1
backend/app/views/layouts/mailer.text.erb
Normal file
@@ -0,0 +1 @@
|
||||
<%= yield %>
|
||||
Reference in New Issue
Block a user