sare26 commited on
Commit
c97a8e9
·
verified ·
1 Parent(s): aa3fd22

Update artifacts/api-server/src/routes/me/index.ts

Browse files
artifacts/api-server/src/routes/me/index.ts CHANGED
@@ -1,7 +1,7 @@
1
  import { Router, type Request, type Response } from "express";
2
  import { db, subscriptionsTable, usersTable } from "@workspace/db";
3
  import { eq } from "drizzle-orm";
4
- import { requireAuth, type AuthedRequest } from "../../lib/auth";
5
 
6
  const router = Router();
7
 
@@ -18,20 +18,47 @@ function getClientIp(req: Request): string | null {
18
  return req.socket?.remoteAddress ?? null;
19
  }
20
 
21
- router.get("/", requireAuth, async (req: Request, res: Response) => {
22
- const { userId, userEmail } = req as AuthedRequest;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
23
  const clientIp = getClientIp(req);
24
  const now = new Date();
25
 
26
  // 👑 استثناء حساب المالك الماستر
27
  if (userEmail && userEmail.toLowerCase() === "saresare2555@hotmail.com") {
28
- // جلب اشتراك المالك من قاعدة البيانات (إن وُجد)
29
  const [ownerSub] = await db
30
  .select()
31
  .from(subscriptionsTable)
32
  .where(eq(subscriptionsTable.userId, userId));
33
 
34
- // إذا كان المالك قد أنهى تجربته يدوياً (trial_expired)، احترم ذلك
35
  if (ownerSub && ownerSub.status === "trial_expired") {
36
  res.set("Cache-Control", "private, max-age=60");
37
  return res.json({
@@ -42,12 +69,11 @@ router.get("/", requireAuth, async (req: Request, res: Response) => {
42
  trialEnd: ownerSub.trialEnd?.toISOString() ?? null,
43
  currentPeriodEnd: null,
44
  paypalSubscriptionId: null,
45
- hasAccess: false, // 🔥 المالك يرى واجهة منتهية التجربة للاختبار
46
  },
47
  });
48
  }
49
 
50
- // الوضع الطبيعي: المالك لديه صلاحية كاملة
51
  res.set("Cache-Control", "private, max-age=60");
52
  return res.json({
53
  userId,
@@ -114,11 +140,9 @@ router.get("/", requireAuth, async (req: Request, res: Response) => {
114
  } else if (sub.trialEnd && sub.trialEnd <= now) {
115
  status = "trial_expired";
116
  }
117
- }
118
- else if (status === "trial_expired" || status === "expired") {
119
  hasAccess = false;
120
- }
121
- else if (status === "active") {
122
  if (!sub.currentPeriodEnd || sub.currentPeriodEnd > now) {
123
  hasAccess = true;
124
  }
 
1
  import { Router, type Request, type Response } from "express";
2
  import { db, subscriptionsTable, usersTable } from "@workspace/db";
3
  import { eq } from "drizzle-orm";
4
+ import { stackServerApp } from "@stackframe/stack";
5
 
6
  const router = Router();
7
 
 
18
  return req.socket?.remoteAddress ?? null;
19
  }
20
 
21
+ // ✅ بديل requireAuth من Clerk - يستخدم Stack Auth
22
+ async function requireAuth(req: Request, res: Response): Promise<{ userId: string; userEmail: string | null } | null> {
23
+ const authHeader = req.headers.authorization;
24
+ if (!authHeader?.startsWith("Bearer ")) {
25
+ res.status(401).json({ error: "Unauthorized" });
26
+ return null;
27
+ }
28
+
29
+ const token = authHeader.split(" ")[1];
30
+
31
+ try {
32
+ const user = await stackServerApp.getUser(token);
33
+ if (!user) {
34
+ res.status(401).json({ error: "Invalid token" });
35
+ return null;
36
+ }
37
+ return {
38
+ userId: user.id,
39
+ userEmail: user.primaryEmail ?? null,
40
+ };
41
+ } catch {
42
+ res.status(401).json({ error: "Invalid token" });
43
+ return null;
44
+ }
45
+ }
46
+
47
+ router.get("/", async (req: Request, res: Response) => {
48
+ const auth = await requireAuth(req, res);
49
+ if (!auth) return;
50
+
51
+ const { userId, userEmail } = auth;
52
  const clientIp = getClientIp(req);
53
  const now = new Date();
54
 
55
  // 👑 استثناء حساب المالك الماستر
56
  if (userEmail && userEmail.toLowerCase() === "saresare2555@hotmail.com") {
 
57
  const [ownerSub] = await db
58
  .select()
59
  .from(subscriptionsTable)
60
  .where(eq(subscriptionsTable.userId, userId));
61
 
 
62
  if (ownerSub && ownerSub.status === "trial_expired") {
63
  res.set("Cache-Control", "private, max-age=60");
64
  return res.json({
 
69
  trialEnd: ownerSub.trialEnd?.toISOString() ?? null,
70
  currentPeriodEnd: null,
71
  paypalSubscriptionId: null,
72
+ hasAccess: false,
73
  },
74
  });
75
  }
76
 
 
77
  res.set("Cache-Control", "private, max-age=60");
78
  return res.json({
79
  userId,
 
140
  } else if (sub.trialEnd && sub.trialEnd <= now) {
141
  status = "trial_expired";
142
  }
143
+ } else if (status === "trial_expired" || status === "expired") {
 
144
  hasAccess = false;
145
+ } else if (status === "active") {
 
146
  if (!sub.currentPeriodEnd || sub.currentPeriodEnd > now) {
147
  hasAccess = true;
148
  }