Skip to content

Commit 8e1e8d5

Browse files
committed
Switch to Amplify done
1 parent 470ff8b commit 8e1e8d5

44 files changed

Lines changed: 1017 additions & 824 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

playground/connect-next/app/home/client.tsx renamed to playground/connect-next/app/(auth-required)/home/client.tsx

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use client';
22

33
import { useRouter } from 'next/navigation';
4+
import { signOut } from 'aws-amplify/auth';
45

56
type Props = {
67
maybeSecretCode?: string;
@@ -9,6 +10,11 @@ type Props = {
910
export default function Home({ maybeSecretCode }: Props) {
1011
const router = useRouter();
1112

13+
const logout = async () => {
14+
await signOut();
15+
router.push('/login');
16+
};
17+
1218
return (
1319
<>
1420
<div className='w-full flex justify-center'>
@@ -27,9 +33,7 @@ export default function Home({ maybeSecretCode }: Props) {
2733
</button>
2834
<button
2935
className='bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 my-4 rounded w-full'
30-
onClick={async () => {
31-
window.location.replace(`/login`);
32-
}}
36+
onClick={logout}
3337
>
3438
Logout
3539
</button>

playground/connect-next/app/home/page.tsx renamed to playground/connect-next/app/(auth-required)/home/page.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { cookies } from 'next/headers';
2-
import Home from '@/app/home/client';
2+
import Home from '@/app/(auth-required)/home/client';
33

44
export default async function Page() {
55
const cookieStore = await cookies();
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
'use client';
2+
3+
import { ProtectedRoute } from '@/components/ProtectedRoute';
4+
5+
export default function AuthenticatedLayout({ children }: { children: React.ReactNode }) {
6+
return <ProtectedRoute>{children}</ProtectedRoute>;
7+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
'use server';
2+
3+
import { cookies } from 'next/headers';
4+
import { ConnectTokenType } from '@corbado/types';
5+
import { getCorbadoConnectToken, verifyAmplifyToken } from '@/lib/utils';
6+
7+
export const getCorbadoToken = async (tokenType: ConnectTokenType) => {
8+
const cookieStore = await cookies();
9+
const idToken = cookieStore.get('idToken');
10+
if (!idToken) {
11+
throw new Error('idToken is required');
12+
}
13+
14+
const { displayName, identifier } = await verifyAmplifyToken(idToken.value);
15+
16+
return getCorbadoConnectToken(tokenType, {
17+
displayName: displayName,
18+
identifier: identifier,
19+
});
20+
};

playground/connect-next/app/passkey-list/page.tsx renamed to playground/connect-next/app/(auth-required)/passkey-list-wv/page.tsx

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,13 @@
11
'use client';
2-
export const runtime = 'edge';
3-
42
import { CorbadoConnectPasskeyList } from '@corbado/connect-react';
5-
import { useRouter } from 'next/navigation';
63
import { getCorbadoToken } from './actions';
7-
import { getAppendToken } from '../actions';
84

95
export default function PasskeyListPage() {
10-
const router = useRouter();
11-
126
return (
137
<div className='w-full flex justify-center'>
148
<div className='w-full my-4 mx-4'>
159
<div className='mb-2 flex justify-between w-full'>
16-
<CorbadoConnectPasskeyList
17-
connectTokenProvider={async tokenType =>
18-
tokenType === 'passkey-append' ? await getAppendToken() : await getCorbadoToken(tokenType)
19-
}
20-
/>
10+
<CorbadoConnectPasskeyList connectTokenProvider={async tokenType => getCorbadoToken(tokenType)} />
2111
</div>
2212
</div>
2313
</div>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
'use server';
2+
3+
import { ConnectTokenType } from '@corbado/types';
4+
import { getCorbadoConnectToken, verifyAmplifyToken } from '@/lib/utils';
5+
6+
export const getCorbadoToken = async (tokenType: ConnectTokenType, idToken?: string) => {
7+
if (!idToken) {
8+
throw new Error('idToken is required');
9+
}
10+
11+
const { displayName, identifier } = await verifyAmplifyToken(idToken);
12+
13+
return getCorbadoConnectToken(tokenType, {
14+
displayName: displayName,
15+
identifier: identifier,
16+
});
17+
};

playground/connect-next/app/passkey-list-wv/page.tsx renamed to playground/connect-next/app/(auth-required)/passkey-list/page.tsx

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,21 @@
11
'use client';
2-
// export const runtime = 'edge';
2+
import { fetchAuthSession } from 'aws-amplify/auth';
3+
34
import { CorbadoConnectPasskeyList } from '@corbado/connect-react';
45
import { getCorbadoToken } from './actions';
5-
import { getAppendToken } from '../actions';
66

77
export default function PasskeyListPage() {
88
return (
99
<div className='w-full flex justify-center'>
1010
<div className='w-full my-4 mx-4'>
1111
<div className='mb-2 flex justify-between w-full'>
1212
<CorbadoConnectPasskeyList
13-
connectTokenProvider={async tokenType =>
14-
tokenType === 'passkey-append' ? await getAppendToken() : await getCorbadoToken(tokenType)
15-
}
13+
connectTokenProvider={async tokenType => {
14+
const session = await fetchAuthSession();
15+
const idToken = session.tokens?.idToken?.toString();
16+
17+
return getCorbadoToken(tokenType, idToken);
18+
}}
1619
/>
1720
</div>
1821
</div>
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
'use server';
2+
3+
import { AppendStatus, ConnectTokenType } from '@corbado/types';
4+
import { cookies } from 'next/headers';
5+
import { getCorbadoConnectToken, verifyAmplifyToken } from '@/lib/utils';
6+
7+
export const getCorbadoToken = async () => {
8+
const cookieStore = await cookies();
9+
const idToken = cookieStore.get('idToken');
10+
if (!idToken) {
11+
throw new Error('idToken is required');
12+
}
13+
14+
const { displayName, identifier } = await verifyAmplifyToken(idToken.value);
15+
16+
return getCorbadoConnectToken('passkey-append' as ConnectTokenType, {
17+
displayName: displayName,
18+
identifier: identifier,
19+
});
20+
};
21+
22+
export async function postPasskeyAppend(appendStatus: AppendStatus, clientState: string) {
23+
// update client side state
24+
console.log(appendStatus);
25+
if (appendStatus === 'complete' || appendStatus === 'complete-noop') {
26+
const cookieStore = await cookies();
27+
cookieStore.set({
28+
name: 'cbo_client_state',
29+
value: clientState,
30+
httpOnly: true,
31+
expires: new Date(Date.now() + 1000 * 60 * 60 * 24 * 365),
32+
});
33+
}
34+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
'use client';
2+
import { CorbadoConnectAppend } from '@corbado/connect-react';
3+
import { getCorbadoToken, postPasskeyAppend } from '@/app/(auth-required)/post-login-wv/actions';
4+
import { AppendStatus } from '@corbado/types';
5+
6+
export default function PostLoginPage() {
7+
return (
8+
<div className='w-full flex justify-center'>
9+
<div className='w-96 my-4 mx-4'>
10+
<div className='mb-2 flex justify-between'>
11+
<CorbadoConnectAppend
12+
onSkip={async status => {
13+
window.location.href = `auth://callback?status=${status}`;
14+
}}
15+
appendTokenProvider={async () => {
16+
return await getCorbadoToken();
17+
}}
18+
onComplete={async (status: AppendStatus, clientSideState: string) => {
19+
await postPasskeyAppend(status, clientSideState);
20+
window.location.href = `auth://callback?status=${status}`;
21+
}}
22+
/>
23+
</div>
24+
</div>
25+
</div>
26+
);
27+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
'use server';
2+
3+
import { AppendStatus, ConnectTokenType } from '@corbado/types';
4+
import { cookies } from 'next/headers';
5+
import { getCorbadoConnectToken, verifyAmplifyToken } from '@/lib/utils';
6+
7+
export const getCorbadoToken = async (idToken?: string) => {
8+
if (!idToken) {
9+
throw new Error('idToken is required');
10+
}
11+
12+
const { displayName, identifier } = await verifyAmplifyToken(idToken);
13+
14+
return getCorbadoConnectToken('passkey-append' as ConnectTokenType, {
15+
displayName: displayName,
16+
identifier: identifier,
17+
});
18+
};
19+
20+
export async function postPasskeyAppend(appendStatus: AppendStatus, clientState: string) {
21+
// update client side state
22+
console.log(appendStatus);
23+
if (appendStatus === 'complete' || appendStatus === 'complete-noop') {
24+
const cookieStore = await cookies();
25+
cookieStore.set({
26+
name: 'cbo_client_state',
27+
value: clientState,
28+
httpOnly: true,
29+
expires: new Date(Date.now() + 1000 * 60 * 60 * 24 * 365),
30+
});
31+
}
32+
}

0 commit comments

Comments
 (0)