/
Infernocasis
/
X.com
Обзор
Документация
Войти
/
Infernocasis
/
X.com
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
main
app/controllers/application_controller.rb
36 строк
1 KB
Infernocasis
Initial commit
08 июн 2025, 20:43
08 июн 2025, 20:43
68371ab
Код
Авторство
О чём код?
class ApplicationController < ActionController::API include ActionController::HttpAuthentication::Token::ControllerMethods before_action :authenticate_user before_action :set_cors_headers private def authenticate_user return if request.method == 'OPTIONS' token = request.headers['Authorization']&.split(' ')&.last if token begin decoded_token = JWT.decode(token, Rails.application.credentials.secret_key_base, true, algorithm: 'HS256') @current_user = User.find(decoded_token[0]['user_id']) rescue JWT::DecodeError, ActiveRecord::RecordNotFound render json: { error: 'Invalid token' }, status: :unauthorized end else render json: { error: 'Token missing' }, status: :unauthorized end end def current_user @current_user end def set_cors_headers headers['Access-Control-Allow-Origin'] = 'http://localhost:3000' headers['Access-Control-Allow-Methods'] = 'GET, POST, PUT, PATCH, DELETE, OPTIONS' headers['Access-Control-Allow-Headers'] = 'Origin, Content-Type, Accept, Authorization, Token, Auth-Token, Email' headers['Access-Control-Allow-Credentials'] = 'true' headers['Access-Control-Max-Age'] = '1728000' end end