|
1 | 1 | import type { Handle } from '@sveltejs/kit'; |
2 | | -import { createCommercifyClient } from '$lib/server/api'; |
3 | 2 | import { sequence } from '@sveltejs/kit/hooks'; |
4 | | -import { env } from '$env/dynamic/private'; |
5 | 3 | import { redirect } from '@sveltejs/kit'; |
| 4 | +import { CachedCommercifyApiClient, createApiClient } from '$lib/server/commercify/api'; |
6 | 5 |
|
7 | 6 | const handleAuth: Handle = async ({ event, resolve }) => { |
8 | 7 | // Check if the route requires admin authentication |
9 | 8 | if (event.url.pathname.startsWith('/admin')) { |
10 | | - const accessToken = event.cookies.get('access_token'); |
| 9 | + const { commercify } = event.locals; |
11 | 10 |
|
12 | | - // If no access token, redirect to login immediately |
13 | | - if (!accessToken) { |
| 11 | + if (!commercify.client.authToken) { |
| 12 | + console.log('No auth token found, redirecting to login'); |
| 13 | + // Redirect to login if no auth token is present |
14 | 14 | throw redirect(303, '/login'); |
15 | 15 | } |
16 | 16 |
|
17 | | - // Create commercify client to validate the token |
18 | | - const cookieString = event.cookies |
19 | | - .getAll() |
20 | | - .map((cookie) => `${cookie.name}=${cookie.value}`) |
21 | | - .join('; '); |
22 | | - |
23 | | - const commercifyClient = createCommercifyClient(cookieString); |
24 | | - |
25 | | - // Validate the access token by calling getUser() |
26 | | - const userResponse = await commercifyClient.getUser(); |
27 | | - |
28 | | - if (!userResponse.success || !userResponse.data) { |
29 | | - console.log('Access token validation failed:', userResponse.error); |
30 | | - |
31 | | - // Clear all auth-related cookies since token is invalid |
32 | | - event.cookies.delete('access_token', { path: '/' }); |
| 17 | + try { |
| 18 | + const userResponse = await commercify.auth.getUser(); |
| 19 | + |
| 20 | + // Add validated user info to locals for use in admin pages |
| 21 | + event.locals.user = { |
| 22 | + email: userResponse.email, |
| 23 | + name: `${userResponse.firstName} ${userResponse.lastName}`, |
| 24 | + role: userResponse.role as 'admin' |
| 25 | + }; |
| 26 | + } catch (error) { |
| 27 | + event.cookies.delete('auth_token', { path: '/' }); |
33 | 28 | event.cookies.delete('user_role', { path: '/' }); |
34 | | - |
35 | | - throw redirect(303, '/login'); |
36 | 29 | } |
37 | 30 |
|
38 | 31 | // Verify user has admin role (from validated user data) |
39 | | - if (userResponse.data.role !== 'admin') { |
40 | | - console.log('User does not have admin role:', userResponse.data.role); |
| 32 | + if (event.locals.user?.role !== 'admin') { |
| 33 | + console.log('User does not have admin role:', event.locals.user!.role); |
41 | 34 | throw redirect(303, '/login'); |
42 | 35 | } |
43 | | - |
44 | | - // Add validated user info to locals for use in admin pages |
45 | | - event.locals.user = { |
46 | | - email: userResponse.data.email, |
47 | | - name: `${userResponse.data.firstName} ${userResponse.data.lastName}`, |
48 | | - role: userResponse.data.role as 'admin', |
49 | | - accessToken |
50 | | - }; |
51 | 36 | } |
52 | 37 |
|
53 | 38 | return resolve(event); |
54 | 39 | }; |
55 | 40 |
|
56 | 41 | const handleCommercify: Handle = async ({ event, resolve }) => { |
57 | | - // Create commercify client with forwarded cookies and store in locals |
58 | | - const cookieString = event.cookies |
59 | | - .getAll() |
60 | | - .map((cookie) => `${cookie.name}=${cookie.value}`) |
61 | | - .join('; '); |
62 | | - |
63 | | - event.locals.commercify = createCommercifyClient(cookieString); |
64 | | - |
65 | | - // For shop routes, ensure we have a checkout session and set the cookie |
66 | | - if (event.url.pathname.startsWith('/shop')) { |
67 | | - try { |
68 | | - const checkoutResponse = await event.locals.commercify.getOrCreateCheckout('DKK'); |
| 42 | + // Get the auth token from a secure, httpOnly cookie |
| 43 | + const authToken = event.cookies.get('auth_token'); |
69 | 44 |
|
70 | | - if (checkoutResponse.success && checkoutResponse.data?.id) { |
71 | | - // Set the checkout session cookie |
72 | | - event.cookies.set('checkout_session_id', checkoutResponse.data.id, { |
73 | | - path: '/', |
74 | | - // httpOnly: true, |
75 | | - secure: env.NODE_ENV === 'production', // Set to true in production |
76 | | - // sameSite: 'lax', |
77 | | - maxAge: 86400 * 7 // 7 days |
78 | | - }); |
79 | | - } |
80 | | - } catch (error) { |
81 | | - console.error('Error setting up checkout session in hooks:', error); |
82 | | - // Don't fail the request, just log the error |
83 | | - } |
84 | | - } |
| 45 | + // Create an API client instance and attach it to the event object |
| 46 | + event.locals.commercify = createApiClient(event, authToken) as CachedCommercifyApiClient; |
85 | 47 |
|
86 | 48 | return resolve(event); |
87 | 49 | }; |
88 | 50 |
|
89 | | -export const handle: Handle = sequence(handleAuth, handleCommercify); |
| 51 | +export const handle: Handle = sequence(handleCommercify, handleAuth); |
0 commit comments