Skip to content

Commit 3705880

Browse files
committed
Add connectTokenExternal route
1 parent eb7bdc6 commit 3705880

File tree

2 files changed

+66
-0
lines changed

2 files changed

+66
-0
lines changed
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, verifyAmplifyTokenExternal } 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 verifyAmplifyTokenExternal(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: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,20 @@ export const verifyAmplifyToken = async (idToken: string): Promise<TokenData> =>
2626
return { displayName, identifier };
2727
};
2828

29+
export const verifyAmplifyTokenExternal = async (idToken: string): Promise<TokenData> => {
30+
const verifier = CognitoJwtVerifier.create({
31+
userPoolId: process.env.AWS_COGNITO_USER_POOL_ID_EXTERNAL!,
32+
tokenUse: 'id',
33+
clientId: process.env.AWS_COGNITO_CLIENT_ID_EXTERNAL!,
34+
});
35+
36+
const verifiedToken = await verifier.verify(idToken);
37+
const displayName: string = verifiedToken.email as string;
38+
const identifier = verifiedToken['cognito:username'];
39+
40+
return { displayName, identifier };
41+
};
42+
2943
export type CognitoUserInfo = {
3044
username: string;
3145
email: string;
@@ -68,3 +82,30 @@ export const getCorbadoConnectToken = async (connectTokenType: string, connectTo
6882

6983
return out.secret;
7084
};
85+
86+
export const getCorbadoConnectTokenExternal = async (
87+
connectTokenType: string,
88+
connectTokenData: any,
89+
): Promise<string> => {
90+
const payload = {
91+
type: connectTokenType,
92+
data: connectTokenData,
93+
};
94+
95+
const body = JSON.stringify(payload);
96+
97+
const url = `${process.env.CORBADO_BACKEND_API_URL_EXTERNAL}/v2/connectTokens`;
98+
const response = await fetch(url, {
99+
method: 'POST',
100+
headers: {
101+
Authorization: `Basic ${process.env.CORBADO_BACKEND_API_BASIC_AUTH_EXTERNAL}`,
102+
'Content-Type': 'application/json',
103+
},
104+
cache: 'no-cache',
105+
body: body,
106+
});
107+
108+
const out = await response.json();
109+
110+
return out.secret;
111+
};

0 commit comments

Comments
 (0)