Skip to content

Commit 7dff1b5

Browse files
author
Paul Asjes
authored
Merge pull request #36 from workos/nicknisi/v1-compat
Update example app to use v1.0.x and get it working
2 parents b48ddf8 + 1c5ca00 commit 7dff1b5

File tree

8 files changed

+597
-143
lines changed

8 files changed

+597
-143
lines changed

package-lock.json

Lines changed: 545 additions & 119 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
"dependencies": {
1515
"@radix-ui/react-icons": "^1.3.0",
1616
"@radix-ui/themes": "^3.0.1",
17-
"@workos-inc/authkit-nextjs": "^0.13.2",
17+
"@workos-inc/authkit-nextjs": "^1.0.2",
1818
"next": "^15.0.1",
1919
"react": "^18",
2020
"react-dom": "^18"

src/app/account/page.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@ export default async function AccountPage() {
55
const { user, role, permissions } = await withAuth({ ensureSignedIn: true });
66

77
const userFields = [
8-
["First name", user.firstName],
9-
["Last name", user.lastName],
10-
["Email", user.email],
8+
["First name", user?.firstName],
9+
["Last name", user?.lastName],
10+
["Email", user?.email],
1111
role ? ["Role", role] : [],
1212
permissions ? ["Permissions", permissions] : [],
13-
["Id", user.id],
13+
["Id", user?.id],
1414
].filter((arr) => arr.length > 0);
1515

1616
return (

src/app/actions/getSignInUrl.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
"use server";
2+
3+
import { getSignInUrl } from "@workos-inc/authkit-nextjs";
4+
5+
export const getSignInUrlAction = async () => {
6+
return await getSignInUrl();
7+
};

src/app/actions/signOut.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
"use server";
2+
3+
import { signOut } from "@workos-inc/authkit-nextjs";
4+
5+
export const handleSignOutAction = async () => {
6+
await signOut();
7+
};

src/app/api/get-name/route.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import { getSession } from "@workos-inc/authkit-nextjs";
2-
import { NextResponse } from "next/server";
1+
import { authkit } from "@workos-inc/authkit-nextjs";
2+
import { NextRequest, NextResponse } from "next/server";
33

4-
export const GET = async () => {
5-
// Use 'getSession' for edge functions that don't have access to headers
6-
const session = await getSession();
4+
export const GET = async (request: NextRequest) => {
5+
// Use 'authkit' for edge functions that don't have access to headers
6+
const { session } = await authkit(request);
77

88
if (!session || !session.user) {
99
return NextResponse.json({ error: "User not found" }, { status: 404 });
Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,31 @@
1-
import { getSignInUrl, withAuth, signOut } from "@workos-inc/authkit-nextjs";
1+
"use client";
2+
3+
/**
4+
* Example of a client component using the useAuth hook to get the current user session.
5+
*/
6+
27
import { Button, Flex } from "@radix-ui/themes";
8+
import { useAuth } from "@workos-inc/authkit-nextjs/components";
9+
import { useEffect, useState } from "react";
10+
import { getSignInUrlAction } from "../actions/getSignInUrl";
11+
import { handleSignOutAction } from "../actions/signOut";
12+
13+
export function SignInButton({ large }: { large?: boolean }) {
14+
const { user, loading } = useAuth();
15+
const [signInUrl, setSignInUrl] = useState<string | undefined>(undefined);
316

4-
export async function SignInButton({ large }: { large?: boolean }) {
5-
const { user } = await withAuth();
6-
const authorizationUrl = await getSignInUrl();
17+
useEffect(() => {
18+
getSignInUrlAction().then(setSignInUrl);
19+
}, []);
20+
21+
if (loading) {
22+
return <div>Loading...</div>;
23+
}
724

825
if (user) {
926
return (
1027
<Flex gap="3">
11-
<form
12-
action={async () => {
13-
"use server";
14-
await signOut();
15-
}}
16-
>
28+
<form action={handleSignOutAction}>
1729
<Button type="submit" size={large ? "3" : "2"}>
1830
Sign Out
1931
</Button>
@@ -24,7 +36,7 @@ export async function SignInButton({ large }: { large?: boolean }) {
2436

2537
return (
2638
<Button asChild size={large ? "3" : "2"}>
27-
<a href={authorizationUrl}>Sign In {large && "with AuthKit"}</a>
39+
<a href={signInUrl}>Sign In {large && "with AuthKit"}</a>
2840
</Button>
2941
);
3042
}

src/app/layout.tsx

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@ import NextLink from "next/link";
66
import { Theme, Card, Container, Flex, Button, Box } from "@radix-ui/themes";
77
import { Footer } from "./components/footer";
88
import { SignInButton } from "./components/sign-in-button";
9-
import { AuthKitProvider, Impersonation } from "@workos-inc/authkit-nextjs";
9+
import {
10+
AuthKitProvider,
11+
Impersonation,
12+
} from "@workos-inc/authkit-nextjs/components";
1013

1114
export const metadata: Metadata = {
1215
title: "Example AuthKit Authenticated App",
@@ -26,9 +29,8 @@ export default function RootLayout({
2629
panelBackground="solid"
2730
style={{ backgroundColor: "var(--gray-1)" }}
2831
>
29-
<Impersonation />
30-
3132
<AuthKitProvider>
33+
<Impersonation />
3234
<Container style={{ backgroundColor: "var(--gray-1)" }}>
3335
<Flex direction="column" gap="5" p="5" height="100vh">
3436
<Box asChild flexGrow="1">

0 commit comments

Comments
 (0)