Skip to content

Commit cd1aab5

Browse files
committed
Add connectTokenExternal route
1 parent eb7bdc6 commit cd1aab5

2 files changed

Lines changed: 52 additions & 0 deletions

File tree

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { NextRequest } from 'next/server';
2+
import { getCorbadoConnectTokenExternal, verifyAmplifyToken } from '@/lib/utils';
3+
4+
type Payload = {
5+
idToken: string;
6+
connectTokenType: string;
7+
};
8+
9+
export async function POST(req: NextRequest) {
10+
const body = (await req.json()) as Payload;
11+
12+
const { idToken, connectTokenType } = body;
13+
14+
const { displayName, identifier } = await verifyAmplifyToken(idToken);
15+
16+
const connectToken = await getCorbadoConnectTokenExternal(connectTokenType, {
17+
displayName: displayName,
18+
identifier: identifier,
19+
});
20+
21+
return new Response(JSON.stringify({ token: connectToken }), {
22+
status: 201,
23+
headers: { 'Content-Type': 'application/json' },
24+
});
25+
}

playground/connect-next/lib/utils.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,3 +68,30 @@ export const getCorbadoConnectToken = async (connectTokenType: string, connectTo
6868

6969
return out.secret;
7070
};
71+
72+
export const getCorbadoConnectTokenExternal = async (
73+
connectTokenType: string,
74+
connectTokenData: any,
75+
): Promise<string> => {
76+
const payload = {
77+
type: connectTokenType,
78+
data: connectTokenData,
79+
};
80+
81+
const body = JSON.stringify(payload);
82+
83+
const url = `${process.env.CORBADO_BACKEND_API_URL_EXTERNAL}/v2/connectTokens`;
84+
const response = await fetch(url, {
85+
method: 'POST',
86+
headers: {
87+
Authorization: `Basic ${process.env.CORBADO_BACKEND_API_BASIC_AUTH_EXTERNAL}`,
88+
'Content-Type': 'application/json',
89+
},
90+
cache: 'no-cache',
91+
body: body,
92+
});
93+
94+
const out = await response.json();
95+
96+
return out.secret;
97+
};

0 commit comments

Comments
 (0)