-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathroute.ts
More file actions
58 lines (49 loc) · 1.86 KB
/
route.ts
File metadata and controls
58 lines (49 loc) · 1.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import { NextRequest } from 'next/server';
import { getCorbadoConnectTokenExternal, verifyAmplifyTokenExternal } from '@/lib/utils';
type Payload = {
idToken: string;
connectTokenType: string;
};
export async function POST(req: NextRequest) {
const body = (await req.json()) as Payload;
const { idToken, connectTokenType } = body;
console.log('creating connectTokenType', connectTokenType);
try {
const { displayName, identifier } = await verifyAmplifyTokenExternal(idToken);
const connectToken = await getCorbadoConnectTokenExternal(connectTokenType, {
displayName: displayName,
identifier: identifier,
});
const simulateError = process.env.SIMULATE_ERROR;
if (simulateError && displayName.endsWith('@corbado.com')) {
console.warn('Simulating error for testing purposes');
switch (simulateError) {
case 'error_response':
return new Response(JSON.stringify({ error: 'Simulated error' }), {
status: 500,
headers: { 'Content-Type': 'application/json' },
});
case 'invalid_token':
return new Response(JSON.stringify({ token: 'invalid_token' }), {
status: 201,
headers: { 'Content-Type': 'application/json' },
});
case 'empty_token':
return new Response(JSON.stringify({ token: '' }), {
status: 201,
headers: { 'Content-Type': 'application/json' },
});
}
}
return new Response(JSON.stringify({ token: connectToken }), {
status: 201,
headers: { 'Content-Type': 'application/json' },
});
} catch (e) {
console.error('Error verifying token or getting connect token', e);
return new Response(JSON.stringify({ error: 'Failed to verify token or get connect token' }), {
status: 500,
headers: { 'Content-Type': 'application/json' },
});
}
}