Skip to content

Commit 8567ff7

Browse files
committed
Merge branch 'master' of github.com:profullstack/generate-pdf-api
2 parents e7b94aa + eafa3f3 commit 8567ff7

File tree

4 files changed

+41
-20
lines changed

4 files changed

+41
-20
lines changed

public/js/route-helpers.js

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,24 @@
44
* This module provides helper functions to simplify route creation and registration
55
* for the SPA router.
66
*/
7+
import { apiKeyService } from '../../src/services/api-key-service.js';
78
import { loadPage } from './router.js';
89

10+
/**
11+
* Check if a user has access based on subscription status or admin privileges
12+
* @param {Object} user - User object
13+
* @returns {boolean} - Whether the user has access
14+
*/
15+
function hasAccess(user) {
16+
if (!user) return false;
17+
18+
// Admin users always have access
19+
if (user.is_admin === true) return true;
20+
21+
// Otherwise, check for active subscription
22+
return user.subscription?.status === 'active';
23+
}
24+
925
/**
1026
* Create a route configuration object with common patterns
1127
*
@@ -46,11 +62,10 @@ export function createRoute(viewPath, options = {}) {
4662
const userJson = localStorage.getItem('user');
4763
if (userJson) {
4864
const user = JSON.parse(userJson);
49-
const hasActiveSubscription = user?.subscription?.status === 'active';
50-
const isAdmin = user?.isAdmin === true;
65+
66+
const hasAccess = await apiKeyService.hasAccess(user.email);
5167

52-
// Allow access for users with either an active subscription or admin privileges
53-
if (!hasActiveSubscription && !isAdmin) {
68+
if (!hasAccess) {
5469
console.log('Subscription required, redirecting to subscription page');
5570
alert('You need an active subscription to access this page.');
5671
return next('/subscription');

public/js/views/dashboard.js

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,19 @@ function initDashboard() {
3333
}
3434
}
3535

36-
// Verify subscription status or admin privileges
37-
const hasActiveSubscription = user &&
38-
user.subscription &&
39-
user.subscription.status === 'active';
40-
const isAdmin = user && user.isAdmin === true;
36+
/**
37+
* Check if user has access (admin or active subscription)
38+
* @param {Object} user - User object
39+
* @returns {boolean} - Whether the user has access
40+
*/
41+
function hasAccess(user) {
42+
if (!user) return false;
43+
if (user.is_admin === true) return true;
44+
return user.subscription?.status === 'active';
45+
}
4146

42-
// Allow access for users with either an active subscription or admin privileges
43-
if (!hasActiveSubscription && !isAdmin) {
47+
// Check if user has access
48+
if (!hasAccess(user)) {
4449
// Redirect to subscription page
4550
alert('You need an active subscription to access the dashboard.');
4651
window.router.navigate('/subscription');

src/middleware/subscription-check.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { paymentService } from '../services/payment-service.js';
2+
import { apiKeyService } from '../services/api-key-service.js';
23
import { errorUtils } from '../utils/error-utils.js';
34

45
/**
@@ -33,10 +34,10 @@ export async function subscriptionCheck(c, next) {
3334
// For simplicity, we're using the API key as the email address
3435
const email = apiKey;
3536

36-
// Check if user has an active subscription
37-
const hasActiveSubscription = await paymentService.hasActiveSubscription(email);
37+
// Check if user has an active subscription or is an admin
38+
const hasAccess = await apiKeyService.hasAccess(email);
3839

39-
if (!hasActiveSubscription) {
40+
if (!hasAccess) {
4041
return c.json({
4142
error: 'Active subscription required',
4243
subscription_required: true,

src/services/api-key-service.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,11 @@ export const apiKeyService = {
4040
/**
4141
* Create user if not exists
4242
* @param {string} email - User email
43-
* @param {boolean} isAdmin - Whether the user is an admin
43+
* @param {boolean} is_admin - Whether the user is an admin
4444
* @returns {Promise<Object|null>} - User object or null if couldn't be created
4545
* @private
4646
*/
47-
async _createUserIfNotExists(email, isAdmin = false) {
47+
async _createUserIfNotExists(email, is_admin = false) {
4848
try {
4949
// Check if user exists
5050
const user = await this._getUserByEmail(email);
@@ -62,7 +62,7 @@ export const apiKeyService = {
6262
.from('users')
6363
.insert([{
6464
email,
65-
is_admin: isAdmin
65+
is_admin: is_admin
6666
}])
6767
.select()
6868
.single();
@@ -72,7 +72,7 @@ export const apiKeyService = {
7272
console.warn(`API Key Service: Permission denied to create user ${email}, proceeding anyway`);
7373
// Return a temporary user object with just the email
7474
// This allows operations to proceed without failing entirely
75-
return { email, id: null, is_admin: isAdmin, temp_user: true };
75+
return { email, id: null, is_admin: is_admin, temp_user: true };
7676
} else {
7777
throw error;
7878
}
@@ -86,15 +86,15 @@ export const apiKeyService = {
8686
if (insertError.code === '42501') { // Permission denied error
8787
console.warn(`API Key Service: Permission denied to create user ${email}, proceeding anyway`);
8888
// Return a temporary user object with just the email
89-
return { email, id: null, is_admin: isAdmin, temp_user: true };
89+
return { email, id: null, is_admin: is_admin, temp_user: true };
9090
} else {
9191
throw insertError;
9292
}
9393
}
9494
} catch (error) {
9595
console.error(`API Key Service: Error in _createUserIfNotExists for ${email}:`, error);
9696
// Return a temporary user object to allow operations to continue
97-
return { email, id: null, is_admin: isAdmin, temp_user: true };
97+
return { email, id: null, is_admin: is_admin, temp_user: true };
9898
}
9999
},
100100

0 commit comments

Comments
 (0)