@windrun-huaiin/backend-core 14.2.0 → 14.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/app/api/stripe/checkout/route.js +1 -1
- package/dist/app/api/stripe/checkout/route.mjs +1 -1
- package/dist/app/api/stripe/customer-portal/route.js +1 -1
- package/dist/app/api/stripe/customer-portal/route.mjs +1 -1
- package/dist/app/api/user/anonymous/init/route.d.ts.map +1 -1
- package/dist/app/api/user/anonymous/init/route.js +2 -1
- package/dist/app/api/user/anonymous/init/route.mjs +3 -2
- package/dist/auth/auth-middleware.d.ts +13 -0
- package/dist/auth/auth-middleware.d.ts.map +1 -0
- package/dist/auth/auth-middleware.js +86 -0
- package/dist/auth/auth-middleware.mjs +82 -0
- package/dist/auth/auth-shared.d.ts +14 -0
- package/dist/auth/auth-shared.d.ts.map +1 -0
- package/dist/auth/auth-shared.js +13 -0
- package/dist/auth/auth-shared.mjs +10 -0
- package/dist/auth/auth-utils.d.ts +48 -0
- package/dist/auth/auth-utils.d.ts.map +1 -0
- package/dist/auth/auth-utils.js +110 -0
- package/dist/auth/auth-utils.mjs +105 -0
- package/dist/index.js +2 -6
- package/dist/index.mjs +2 -2
- package/dist/lib/index.d.ts +0 -1
- package/dist/lib/index.d.ts.map +1 -1
- package/dist/lib/index.js +0 -5
- package/dist/lib/index.mjs +0 -1
- package/dist/lib/money-price-helper.js +2 -1
- package/dist/lib/money-price-helper.mjs +3 -2
- package/dist/services/context/index.d.ts +1 -0
- package/dist/services/context/index.d.ts.map +1 -1
- package/dist/services/context/index.js +2 -1
- package/dist/services/context/index.mjs +2 -1
- package/dist/services/context/user-context-finalizer.d.ts +13 -0
- package/dist/services/context/user-context-finalizer.d.ts.map +1 -0
- package/dist/services/context/user-context-finalizer.js +74 -0
- package/dist/services/context/user-context-finalizer.mjs +72 -0
- package/dist/services/context/user-context-service.d.ts +0 -6
- package/dist/services/context/user-context-service.d.ts.map +1 -1
- package/dist/services/context/user-context-service.js +0 -64
- package/dist/services/context/user-context-service.mjs +1 -64
- package/package.json +22 -7
- package/src/app/api/stripe/checkout/route.ts +1 -1
- package/src/app/api/stripe/customer-portal/route.ts +1 -1
- package/src/app/api/user/anonymous/init/route.ts +3 -2
- package/src/auth/auth-middleware.ts +109 -0
- package/src/auth/auth-shared.ts +16 -0
- package/src/{lib → auth}/auth-utils.ts +14 -10
- package/src/lib/index.ts +0 -1
- package/src/lib/money-price-helper.ts +3 -3
- package/src/services/context/index.ts +2 -1
- package/src/services/context/user-context-finalizer.ts +84 -0
- package/src/services/context/user-context-service.ts +0 -77
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
export const AUTH_HEADERS = {
|
|
2
|
+
provider: 'x-auth-provider',
|
|
3
|
+
providerUserId: 'x-auth-provider-user-id',
|
|
4
|
+
} as const;
|
|
5
|
+
|
|
6
|
+
export const AUTH_ERRORS = {
|
|
7
|
+
unauthorized: 'UNAUTHORIZED',
|
|
8
|
+
userNotFound: 'USER_NOT_FOUND',
|
|
9
|
+
} as const;
|
|
10
|
+
|
|
11
|
+
export type AuthProvider = 'clerk';
|
|
12
|
+
|
|
13
|
+
export interface ProviderIdentity {
|
|
14
|
+
provider: AuthProvider;
|
|
15
|
+
providerUserId: string;
|
|
16
|
+
}
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { NextRequest } from 'next/server';
|
|
2
2
|
import { userService } from '../services/database/index';
|
|
3
3
|
import { User } from '../services/database/prisma-model-type';
|
|
4
|
+
import { AUTH_ERRORS, AUTH_HEADERS, type AuthProvider } from './auth-shared';
|
|
4
5
|
|
|
5
6
|
/**
|
|
6
7
|
* 认证结果类型
|
|
@@ -8,7 +9,8 @@ import { User } from '../services/database/prisma-model-type';
|
|
|
8
9
|
export interface AuthResult {
|
|
9
10
|
userId: string;
|
|
10
11
|
user: User;
|
|
11
|
-
|
|
12
|
+
provider: AuthProvider;
|
|
13
|
+
providerUserId: string;
|
|
12
14
|
}
|
|
13
15
|
|
|
14
16
|
/**
|
|
@@ -16,20 +18,22 @@ export interface AuthResult {
|
|
|
16
18
|
*/
|
|
17
19
|
export async function getAuthenticatedUser(req: NextRequest): Promise<AuthResult> {
|
|
18
20
|
try {
|
|
19
|
-
const
|
|
20
|
-
|
|
21
|
-
|
|
21
|
+
const provider = req.headers.get(AUTH_HEADERS.provider);
|
|
22
|
+
const providerUserId = req.headers.get(AUTH_HEADERS.providerUserId);
|
|
23
|
+
if (provider !== 'clerk' || !providerUserId) {
|
|
24
|
+
throw new Error(AUTH_ERRORS.unauthorized);
|
|
22
25
|
}
|
|
23
|
-
|
|
24
|
-
const user = await userService.findByClerkUserId(
|
|
26
|
+
|
|
27
|
+
const user = await userService.findByClerkUserId(providerUserId);
|
|
25
28
|
if (!user) {
|
|
26
|
-
throw new Error(
|
|
29
|
+
throw new Error(AUTH_ERRORS.userNotFound);
|
|
27
30
|
}
|
|
28
|
-
|
|
31
|
+
|
|
29
32
|
return {
|
|
30
33
|
userId: user.userId,
|
|
31
34
|
user,
|
|
32
|
-
|
|
35
|
+
provider: 'clerk',
|
|
36
|
+
providerUserId,
|
|
33
37
|
};
|
|
34
38
|
} catch (error) {
|
|
35
39
|
console.error('Error getting authenticated user:', error);
|
|
@@ -98,4 +102,4 @@ export class ApiAuthUtils {
|
|
|
98
102
|
return null;
|
|
99
103
|
}
|
|
100
104
|
}
|
|
101
|
-
}
|
|
105
|
+
}
|
package/src/lib/index.ts
CHANGED
|
@@ -5,9 +5,9 @@ import {
|
|
|
5
5
|
} from '@windrun-huaiin/third-ui/fingerprint/server';
|
|
6
6
|
import type { InitUserContext } from '@windrun-huaiin/third-ui/main/server';
|
|
7
7
|
import {
|
|
8
|
-
applyUserMockContext,
|
|
9
8
|
buildInitUserContextFromEntities,
|
|
10
9
|
fetchUserContextByClerkUserId,
|
|
10
|
+
finalizeUserContext,
|
|
11
11
|
} from '../services/context';
|
|
12
12
|
|
|
13
13
|
async function readFingerprintIdFromRequest(): Promise<string | null> {
|
|
@@ -42,7 +42,7 @@ export async function getMoneyPriceInitUserContext(): Promise<InitUserContext> {
|
|
|
42
42
|
isClerkAuthenticated: true,
|
|
43
43
|
});
|
|
44
44
|
|
|
45
|
-
return
|
|
45
|
+
return finalizeUserContext(initUserContext);
|
|
46
46
|
}
|
|
47
47
|
|
|
48
48
|
const fingerprintId = await readFingerprintIdFromRequest();
|
|
@@ -58,4 +58,4 @@ export async function getMoneyPriceInitUserContext(): Promise<InitUserContext> {
|
|
|
58
58
|
xSubscription: null,
|
|
59
59
|
isClerkAuthenticated: false,
|
|
60
60
|
};
|
|
61
|
-
}
|
|
61
|
+
}
|
|
@@ -1 +1,2 @@
|
|
|
1
|
-
export * from './user-context-service';
|
|
1
|
+
export * from './user-context-service';
|
|
2
|
+
export * from './user-context-finalizer';
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import { viewLocalTime } from '@windrun-huaiin/lib/utils';
|
|
2
|
+
import type { XSubscription, XUser } from '@windrun-huaiin/third-ui/fingerprint';
|
|
3
|
+
|
|
4
|
+
type FinalizableUserContext = {
|
|
5
|
+
xUser: XUser | null;
|
|
6
|
+
xSubscription: XSubscription | null;
|
|
7
|
+
};
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Output finalizer for user-context payloads.
|
|
11
|
+
* Real data assembly should stay in user-context-service; any optional test-only
|
|
12
|
+
* shaping is isolated here so production services do not depend on mock code.
|
|
13
|
+
*/
|
|
14
|
+
export function finalizeUserContext<T extends FinalizableUserContext>(context: T): T {
|
|
15
|
+
const mockEnabled = process.env.MONEY_PRICE_MOCK_USER_ENABLED === 'true';
|
|
16
|
+
const mockType = Number(process.env.MONEY_PRICE_MOCK_USER_TYPE ?? NaN);
|
|
17
|
+
|
|
18
|
+
if (
|
|
19
|
+
!context.xUser ||
|
|
20
|
+
!mockEnabled ||
|
|
21
|
+
!Number.isInteger(mockType) ||
|
|
22
|
+
mockType < 0 ||
|
|
23
|
+
mockType > 4
|
|
24
|
+
) {
|
|
25
|
+
return context;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
const ensureSubscription = () => {
|
|
29
|
+
if (!context.xSubscription) {
|
|
30
|
+
const now = new Date();
|
|
31
|
+
context.xSubscription = {
|
|
32
|
+
id: BigInt(99999),
|
|
33
|
+
userId: context.xUser!.userId,
|
|
34
|
+
paySubscriptionId: 'MOCK-PAY-SUB-ID',
|
|
35
|
+
orderId: '',
|
|
36
|
+
priceId: '',
|
|
37
|
+
priceName: 'MOCK-TEST',
|
|
38
|
+
status: 'active',
|
|
39
|
+
creditsAllocated: 0,
|
|
40
|
+
subPeriodStart: viewLocalTime(now),
|
|
41
|
+
subPeriodEnd: viewLocalTime(now),
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
return context.xSubscription!;
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
switch (mockType) {
|
|
49
|
+
case 0: {
|
|
50
|
+
const subscription = ensureSubscription();
|
|
51
|
+
subscription.status = '';
|
|
52
|
+
subscription.priceId = '';
|
|
53
|
+
break;
|
|
54
|
+
}
|
|
55
|
+
case 1: {
|
|
56
|
+
const subscription = ensureSubscription();
|
|
57
|
+
subscription.priceId =
|
|
58
|
+
process.env.STRIPE_PRO_MONTHLY_PRICE_ID || subscription.priceId;
|
|
59
|
+
break;
|
|
60
|
+
}
|
|
61
|
+
case 2: {
|
|
62
|
+
const subscription = ensureSubscription();
|
|
63
|
+
subscription.priceId =
|
|
64
|
+
process.env.STRIPE_ULTRA_MONTHLY_PRICE_ID || subscription.priceId;
|
|
65
|
+
break;
|
|
66
|
+
}
|
|
67
|
+
case 3: {
|
|
68
|
+
const subscription = ensureSubscription();
|
|
69
|
+
subscription.priceId =
|
|
70
|
+
process.env.STRIPE_PRO_YEARLY_PRICE_ID || subscription.priceId;
|
|
71
|
+
break;
|
|
72
|
+
}
|
|
73
|
+
case 4: {
|
|
74
|
+
const subscription = ensureSubscription();
|
|
75
|
+
subscription.priceId =
|
|
76
|
+
process.env.STRIPE_ULTRA_YEARLY_PRICE_ID || subscription.priceId;
|
|
77
|
+
break;
|
|
78
|
+
}
|
|
79
|
+
default:
|
|
80
|
+
break;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
return context;
|
|
84
|
+
}
|
|
@@ -121,80 +121,3 @@ export async function fetchLatestUserContextByFingerprintId(
|
|
|
121
121
|
hasAnonymousUser: true,
|
|
122
122
|
};
|
|
123
123
|
}
|
|
124
|
-
|
|
125
|
-
type MockableContext = {
|
|
126
|
-
xUser: XUser | null;
|
|
127
|
-
xSubscription: XSubscription | null;
|
|
128
|
-
};
|
|
129
|
-
|
|
130
|
-
export function applyUserMockContext<T extends MockableContext>(context: T): T {
|
|
131
|
-
const mockEnabled = process.env.MONEY_PRICE_MOCK_USER_ENABLED === 'true';
|
|
132
|
-
const mockType = Number(process.env.MONEY_PRICE_MOCK_USER_TYPE ?? NaN);
|
|
133
|
-
|
|
134
|
-
if (
|
|
135
|
-
!context.xUser ||
|
|
136
|
-
!mockEnabled ||
|
|
137
|
-
!Number.isInteger(mockType) ||
|
|
138
|
-
mockType < 0 ||
|
|
139
|
-
mockType > 4
|
|
140
|
-
) {
|
|
141
|
-
return context;
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
const ensureSubscription = () => {
|
|
145
|
-
if (!context.xSubscription) {
|
|
146
|
-
const now = new Date();
|
|
147
|
-
context.xSubscription = {
|
|
148
|
-
id: BigInt(99999),
|
|
149
|
-
userId: context.xUser!.userId,
|
|
150
|
-
paySubscriptionId: 'MOCK-PAY-SUB-ID',
|
|
151
|
-
orderId: '',
|
|
152
|
-
priceId: '',
|
|
153
|
-
priceName: 'MOCK-TEST',
|
|
154
|
-
status: 'active',
|
|
155
|
-
creditsAllocated: 0,
|
|
156
|
-
subPeriodStart: viewLocalTime(now),
|
|
157
|
-
subPeriodEnd: viewLocalTime(now),
|
|
158
|
-
};
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
return context.xSubscription!;
|
|
162
|
-
};
|
|
163
|
-
|
|
164
|
-
switch (mockType) {
|
|
165
|
-
case 0: {
|
|
166
|
-
const subscription = ensureSubscription();
|
|
167
|
-
subscription.status = '';
|
|
168
|
-
subscription.priceId = '';
|
|
169
|
-
break;
|
|
170
|
-
}
|
|
171
|
-
case 1: {
|
|
172
|
-
const subscription = ensureSubscription();
|
|
173
|
-
subscription.priceId =
|
|
174
|
-
process.env.STRIPE_PRO_MONTHLY_PRICE_ID || subscription.priceId;
|
|
175
|
-
break;
|
|
176
|
-
}
|
|
177
|
-
case 2: {
|
|
178
|
-
const subscription = ensureSubscription();
|
|
179
|
-
subscription.priceId =
|
|
180
|
-
process.env.STRIPE_ULTRA_MONTHLY_PRICE_ID || subscription.priceId;
|
|
181
|
-
break;
|
|
182
|
-
}
|
|
183
|
-
case 3: {
|
|
184
|
-
const subscription = ensureSubscription();
|
|
185
|
-
subscription.priceId =
|
|
186
|
-
process.env.STRIPE_PRO_YEARLY_PRICE_ID || subscription.priceId;
|
|
187
|
-
break;
|
|
188
|
-
}
|
|
189
|
-
case 4: {
|
|
190
|
-
const subscription = ensureSubscription();
|
|
191
|
-
subscription.priceId =
|
|
192
|
-
process.env.STRIPE_ULTRA_YEARLY_PRICE_ID || subscription.priceId;
|
|
193
|
-
break;
|
|
194
|
-
}
|
|
195
|
-
default:
|
|
196
|
-
break;
|
|
197
|
-
}
|
|
198
|
-
|
|
199
|
-
return context;
|
|
200
|
-
}
|