|
1 | 1 | import { getServerSession } from 'next-auth/next'
|
2 |
| -import { getAuthOptions } from './[...nextauth]' |
3 |
| -import { serialize } from 'cookie' |
4 |
| -import { datePivot } from '@/lib/time' |
| 2 | +import { getAuthOptions, generateRandomString } from './[...nextauth]' |
| 3 | +import prisma from '@/api/models' |
5 | 4 |
|
6 |
| -// TODO: dirty of previous iterations, refactor |
7 |
| -// UNSAFE UNSAFE UNSAFE tokens are visible in the URL |
8 | 5 | export default async function handler (req, res) {
|
9 |
| - console.log(req.query) |
10 |
| - if (req.query.token) { |
11 |
| - const session = JSON.parse(decodeURIComponent(req.query.token)) |
12 |
| - return saveCookie(req, res, session) |
13 |
| - } else { |
14 |
| - const { redirectUrl } = req.query |
15 |
| - const session = await getServerSession(req, res, getAuthOptions(req)) |
16 |
| - // TODO: use session to create a verification token |
17 |
| - if (session) { |
18 |
| - console.log('session', session) |
19 |
| - console.log('req.cookies', req.cookies) |
20 |
| - |
21 |
| - const userId = session.user.id |
22 |
| - const multiAuthCookieName = `multi_auth.${userId}` |
23 |
| - const multiAuthToken = req.cookies[multiAuthCookieName] |
24 |
| - |
25 |
| - if (!multiAuthToken) { |
26 |
| - console.error('No multi_auth token found for user', userId) |
27 |
| - return res.status(400).json({ error: 'No multi_auth token found' }) |
28 |
| - } |
29 |
| - |
30 |
| - const transferData = { |
31 |
| - session, |
32 |
| - multiAuthToken, |
33 |
| - userId |
34 |
| - } |
35 |
| - |
36 |
| - // redirect back to the custom domain with the token data |
37 |
| - const callbackUrl = new URL('/api/auth/sync', redirectUrl) |
38 |
| - callbackUrl.searchParams.set('token', encodeURIComponent(JSON.stringify(transferData))) |
39 |
| - callbackUrl.searchParams.set('redirectUrl', req.query.redirectUrl || '/') |
40 |
| - |
41 |
| - return res.redirect(callbackUrl.toString()) |
42 |
| - } |
43 |
| - return res.redirect(redirectUrl) |
| 6 | + const { redirectUrl } = req.query |
| 7 | + if (!redirectUrl) { |
| 8 | + return res.status(400).json({ error: 'Missing redirectUrl parameter' }) |
44 | 9 | }
|
45 |
| -} |
46 | 10 |
|
47 |
| -export async function saveCookie (req, res, tokenData) { |
48 |
| - if (!tokenData) { |
49 |
| - return res.status(400).json({ error: 'Missing token' }) |
| 11 | + const session = await getServerSession(req, res, getAuthOptions(req, res)) |
| 12 | + |
| 13 | + if (!session) { |
| 14 | + // TODO: redirect to login page, this goes to login overlapping other paths |
| 15 | + return res.redirect(redirectUrl + '/login?callbackUrl=' + encodeURIComponent(redirectUrl)) |
50 | 16 | }
|
51 | 17 |
|
52 | 18 | try {
|
53 |
| - const secure = process.env.NODE_ENV === 'development' |
54 |
| - const expiresAt = datePivot(new Date(), { months: 1 }) |
55 |
| - const cookieOptions = { |
56 |
| - path: '/', |
57 |
| - httpOnly: true, |
58 |
| - secure, |
59 |
| - sameSite: 'lax', |
60 |
| - expires: expiresAt |
61 |
| - } |
62 |
| - // extract the data from the token |
63 |
| - const { multiAuthToken, userId } = tokenData |
64 |
| - console.log('Received session and multi_auth token for user', userId) |
65 |
| - |
66 |
| - // set the session cookie |
67 |
| - const sessionCookieName = secure ? '__Secure-next-auth.session-token' : 'next-auth.session-token' |
68 |
| - // create cookies |
69 |
| - const sessionCookie = serialize(sessionCookieName, multiAuthToken, cookieOptions) |
70 |
| - // also set the multi_auth cookie on the custom domain |
71 |
| - const multiAuthCookie = serialize(`multi_auth.${userId}`, multiAuthToken, cookieOptions) |
72 |
| - // set the cookie pointer |
73 |
| - const pointerCookie = serialize('multi_auth.user-id', userId, cookieOptions) |
| 19 | + const token = generateRandomString() |
| 20 | + // create a sync token |
| 21 | + await prisma.verificationToken.create({ |
| 22 | + data: { |
| 23 | + identifier: `sync:${session.user.id}`, |
| 24 | + token, |
| 25 | + expires: new Date(Date.now() + 1 * 60 * 1000) // 1 minute |
| 26 | + } |
| 27 | + }) |
74 | 28 |
|
75 |
| - // set the cookies in the response |
76 |
| - res.setHeader('Set-Cookie', [sessionCookie, multiAuthCookie, pointerCookie]) |
| 29 | + const customDomainCallback = new URL('/?type=sync', redirectUrl) |
| 30 | + customDomainCallback.searchParams.set('token', token) |
| 31 | + customDomainCallback.searchParams.set('callbackUrl', redirectUrl) |
77 | 32 |
|
78 |
| - // redirect to the home page or a specified return URL |
79 |
| - const returnTo = req.query.redirectUrl || '/' |
80 |
| - return res.redirect(returnTo) |
| 33 | + return res.redirect(customDomainCallback.toString()) |
81 | 34 | } catch (error) {
|
82 |
| - console.error('Error processing auth callback:', error) |
83 |
| - return res.status(500).json({ error: 'Failed to process authentication' }) |
| 35 | + console.error('Error generating token:', error) |
| 36 | + return res.status(500).json({ error: 'Failed to generate token' }) |
84 | 37 | }
|
85 | 38 | }
|
0 commit comments