Shurem commited on
Commit
90ca9a4
·
1 Parent(s): eb13f08

fix: Install dev deps for TypeScript build in Docker

Browse files
Files changed (2) hide show
  1. src/better-auth.ts +16 -2
  2. src/index.ts +63 -0
src/better-auth.ts CHANGED
@@ -21,8 +21,22 @@ export const auth = betterAuth({
21
  maxAge: 60 * 5, // 5 minutes
22
  },
23
  },
24
- trustedOrigins: [process.env.FRONTEND_URL || 'http://localhost:3000'],
25
- baseURL: process.env.BETTER_AUTH_URL || 'http://localhost:3001',
 
 
 
 
 
 
 
 
 
 
 
 
 
 
26
  secret: process.env.BETTER_AUTH_SECRET,
27
  });
28
 
 
21
  maxAge: 60 * 5, // 5 minutes
22
  },
23
  },
24
+ advanced: {
25
+ crossSubDomainCookies: {
26
+ enabled: true,
27
+ },
28
+ defaultCookieAttributes: {
29
+ sameSite: 'none',
30
+ secure: true,
31
+ },
32
+ },
33
+ trustedOrigins: [
34
+ 'http://localhost:3000',
35
+ 'http://127.0.0.1:3000',
36
+ 'https://shuremali02.github.io',
37
+ process.env.FRONTEND_URL || '',
38
+ ].filter(Boolean),
39
+ baseURL: process.env.BETTER_AUTH_URL || 'https://shurem-better-auth.hf.space',
40
  secret: process.env.BETTER_AUTH_SECRET,
41
  });
42
 
src/index.ts CHANGED
@@ -6,6 +6,69 @@ import profileRoutes from './routes/profile.js';
6
  import 'dotenv/config';
7
 
8
  const app = new Hono();
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
 
10
  // CORS configuration
11
  app.use('*', cors({
 
6
  import 'dotenv/config';
7
 
8
  const app = new Hono();
9
+ import { Hono } from 'hono';
10
+ import { cors } from 'hono/cors';
11
+ import { auth } from './better-auth.js';
12
+ import healthRoutes from './routes/health.js';
13
+ import profileRoutes from './routes/profile.js';
14
+ import 'dotenv/config';
15
+
16
+ const app = new Hono();
17
+
18
+ // CORS configuration - allow multiple origins
19
+ const allowedOrigins = [
20
+ 'http://localhost:3000',
21
+ 'http://127.0.0.1:3000',
22
+ process.env.FRONTEND_URL,
23
+ 'https://shuremali02.github.io',
24
+ ].filter(Boolean) as string[];
25
+
26
+ app.use('*', cors({
27
+ origin: (origin) => {
28
+ // Allow requests with no origin (mobile apps, curl, etc.)
29
+ if (!origin) return allowedOrigins[0];
30
+ // Check if origin is in allowed list
31
+ if (allowedOrigins.includes(origin)) return origin;
32
+ // Fallback
33
+ return allowedOrigins[0];
34
+ },
35
+ credentials: true,
36
+ allowMethods: ['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS'],
37
+ allowHeaders: ['Content-Type', 'Authorization', 'Cookie'],
38
+ exposeHeaders: ['Set-Cookie'],
39
+ }));
40
+
41
+ // Better-Auth routes - handles /api/auth/*
42
+ app.on(['POST', 'GET'], '/api/auth/*', (c) => auth.handler(c.req.raw));
43
+
44
+ // Application routes
45
+ app.route('/api', healthRoutes);
46
+ app.route('/api', profileRoutes);
47
+
48
+ // Root endpoint
49
+ app.get('/', (c) => {
50
+ return c.json({
51
+ name: 'Auth Service',
52
+ version: '1.0.0',
53
+ endpoints: {
54
+ health: '/api/health',
55
+ auth: '/api/auth/*',
56
+ profile: '/api/profile',
57
+ },
58
+ });
59
+ });
60
+
61
+ const port = parseInt(process.env.PORT || '3001', 10);
62
+
63
+ // Use Node.js adapter for Hono
64
+ import { serve } from '@hono/node-server';
65
+
66
+ serve({
67
+ fetch: app.fetch,
68
+ port,
69
+ }, (info) => {
70
+ console.log(`Auth service running on http://localhost:${info.port}`);
71
+ });
72
 
73
  // CORS configuration
74
  app.use('*', cors({