Accept subscription updates from Dawarich Manager

This commit is contained in:
Eugene Burmakin 2025-02-27 22:32:03 +01:00
parent e368df4e10
commit 3ebf492b14
3 changed files with 32 additions and 3 deletions

View file

@ -4,4 +4,31 @@ class Settings::SubscriptionsController < ApplicationController
before_action :authenticate_user!
def index; end
def subscription_callback
token = params[:token]
begin
decoded_token = JWT.decode(
token,
ENV['JWT_SECRET_KEY'],
true,
{ algorithm: 'HS256' }
).first.symbolize_keys
# Verify this is for the current user
unless decoded_token[:user_id] == current_user.id
redirect_to settings_subscriptions_path, alert: 'Invalid subscription update request.'
return
end
current_user.update!(status: decoded_token[:status])
redirect_to settings_subscriptions_path, notice: 'Your subscription has been updated successfully!'
rescue JWT::DecodeError
redirect_to settings_subscriptions_path, alert: 'Failed to verify subscription update.'
rescue ArgumentError
redirect_to settings_subscriptions_path, alert: 'Invalid subscription data received.'
end
end
end

View file

@ -99,8 +99,6 @@ class User < ApplicationRecord
end
end
# Generates a secure token for cross-application authentication with the subscription app
# @return [String] JWT token containing user identity information
def generate_subscription_token
payload = {
user_id: id,

View file

@ -22,7 +22,11 @@ Rails.application.routes.draw do
resources :users, only: %i[index create destroy edit update]
resources :maps, only: %i[index]
patch 'maps', to: 'maps#update'
resources :subscriptions, only: %i[index]
resources :subscriptions, only: %i[index] do
collection do
get :subscription_callback
end
end
end
patch 'settings', to: 'settings#update'