mirror of
https://github.com/Freika/dawarich.git
synced 2026-01-11 09:41:40 -05:00
Accept subscription updates from Dawarich Manager
This commit is contained in:
parent
e368df4e10
commit
3ebf492b14
3 changed files with 32 additions and 3 deletions
|
|
@ -4,4 +4,31 @@ class Settings::SubscriptionsController < ApplicationController
|
||||||
before_action :authenticate_user!
|
before_action :authenticate_user!
|
||||||
|
|
||||||
def index; end
|
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
|
end
|
||||||
|
|
|
||||||
|
|
@ -99,8 +99,6 @@ class User < ApplicationRecord
|
||||||
end
|
end
|
||||||
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
|
def generate_subscription_token
|
||||||
payload = {
|
payload = {
|
||||||
user_id: id,
|
user_id: id,
|
||||||
|
|
|
||||||
|
|
@ -22,7 +22,11 @@ Rails.application.routes.draw do
|
||||||
resources :users, only: %i[index create destroy edit update]
|
resources :users, only: %i[index create destroy edit update]
|
||||||
resources :maps, only: %i[index]
|
resources :maps, only: %i[index]
|
||||||
patch 'maps', to: 'maps#update'
|
patch 'maps', to: 'maps#update'
|
||||||
resources :subscriptions, only: %i[index]
|
resources :subscriptions, only: %i[index] do
|
||||||
|
collection do
|
||||||
|
get :subscription_callback
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
patch 'settings', to: 'settings#update'
|
patch 'settings', to: 'settings#update'
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue