Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(next-example): add webauthn support w/ unstorage adapter #9963

Merged
merged 51 commits into from
May 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
51 commits
Select commit Hold shift + click to select a range
e13ffb0
fix: add webauthn and vercel/postgres via prisma to nextjs example app
ndom91 Feb 8, 2024
562aeaf
fix: put email helpers into own files to avoid nodemailer import
ndom91 Feb 8, 2024
874aafc
fix: use fallback avatar
ndom91 Feb 8, 2024
5500c20
fix: rm resend
ndom91 Feb 8, 2024
3f8eea5
Update apps/examples/nextjs/package.json
ndom91 Feb 8, 2024
248b1f1
fix: use webauthn builtin formField
ndom91 Feb 8, 2024
5558982
Merge branch 'ndom91/nextjs-example-app-webauthn-db' of ssh://github.…
ndom91 Feb 8, 2024
f2be93d
fix: move webauthnChallenge to webauthn-utils file
ndom91 Feb 8, 2024
9b58702
fix: advanced init nextauth to not save user data in db with provider…
ndom91 Feb 8, 2024
17bed64
Merge branch 'main' into ndom91/nextjs-example-app-webauthn-db
ndom91 Feb 8, 2024
135bf17
Merge branch 'main' into ndom91/nextjs-example-app-webauthn-db
ndom91 Feb 10, 2024
adef707
Merge branch 'main' into ndom91/nextjs-example-app-webauthn-db
ndom91 Feb 10, 2024
11c1508
Merge branch 'main' into ndom91/nextjs-example-app-webauthn-db
ndom91 Feb 11, 2024
5696a6f
Merge branch 'main' into ndom91/nextjs-example-app-webauthn-db
ndom91 Feb 13, 2024
9e6cb77
Merge branch 'main' into ndom91/nextjs-example-app-webauthn-db
ndom91 Feb 17, 2024
9956e80
Merge branch 'main' into ndom91/nextjs-example-app-webauthn-db
ndom91 Mar 14, 2024
cf61381
Merge branch 'main' into ndom91/nextjs-example-app-webauthn-db
ndom91 Apr 8, 2024
7dde2ea
Merge branch 'main' into ndom91/nextjs-example-app-webauthn-db
ndom91 Apr 14, 2024
258b349
fix: swap prisma for unstorage and simplify
ndom91 Apr 14, 2024
d135b10
Discard changes to apps/examples/nextjs/prisma/schema.prisma
ndom91 Apr 14, 2024
62135b6
fix: cleanup auth.ts
ndom91 Apr 14, 2024
2c17e5e
fix: bump @simpelwebauthn/server
ndom91 Apr 14, 2024
ec8d399
fix: update Passkey provider to show username field instead of email
ndom91 Apr 14, 2024
a774360
Merge branch 'main' into ndom91/nextjs-example-app-webauthn-db
ndom91 Apr 17, 2024
e1892f7
Merge branch 'main' into ndom91/nextjs-example-app-webauthn-db
ndom91 Apr 21, 2024
08f7d65
fix: update pnpm-lock
ndom91 May 4, 2024
06a9a78
fix: cleanup packagejson for example app
ndom91 May 4, 2024
8b61fd6
Merge branch 'main' into ndom91/nextjs-example-app-webauthn-db
ndom91 May 4, 2024
0a68ee3
chore: fix pnpm-locks in example and root
ndom91 May 4, 2024
8858047
Merge branch 'main' into ndom91/nextjs-example-app-webauthn-db
ndom91 May 4, 2024
0076f8c
Merge branch 'main' into ndom91/nextjs-example-app-webauthn-db
ndom91 May 6, 2024
766e2ca
Merge branch 'main' into ndom91/nextjs-example-app-webauthn-db
ndom91 May 10, 2024
9e86089
Merge branch 'main' into ndom91/nextjs-example-app-webauthn-db
ndom91 May 10, 2024
38f0d87
fix: cleanup example app for webauthn
ndom91 May 10, 2024
d0f4026
fix: cleanup example app for webauthn
ndom91 May 10, 2024
0261165
Merge branch 'ndom91/nextjs-example-app-webauthn-db' of ssh://github.…
ndom91 May 10, 2024
42ca2bb
fix: webauthn-utils test
ndom91 May 10, 2024
e1c36fd
Discard changes to packages/core/src/lib/actions/callback/oauth/check…
ndom91 May 10, 2024
1613b74
Discard changes to packages/core/src/lib/utils/webauthn-utils.ts
ndom91 May 10, 2024
1a2a471
Merge branch 'ndom91/nextjs-example-app-webauthn-db' of ssh://github.…
ndom91 May 10, 2024
ffeeee0
Revert "Merge branch 'ndom91/nextjs-example-app-webauthn-db' of ssh:/…
ndom91 May 10, 2024
3507f72
Discard changes to packages/core/test/webauthn-utils.test.ts
ndom91 May 10, 2024
b1d25b0
Discard changes to packages/core/src/lib/utils/webauthn-utils.ts
ndom91 May 10, 2024
d39629b
Discard changes to packages/core/src/lib/actions/callback/oauth/check…
ndom91 May 10, 2024
08d50e8
fix: revert root pnpm-lock changes
ndom91 May 10, 2024
45712e4
fix(gha): lint before running unit tests
ndom91 May 10, 2024
2a36c33
fix: prettier
ndom91 May 10, 2024
e6b6d27
Merge branch 'main' into ndom91/nextjs-example-app-webauthn-db
ndom91 May 10, 2024
8ac3c14
fix: add note about webauthn users resetting
ndom91 May 10, 2024
6c266b8
Merge branch 'main' into ndom91/nextjs-example-app-webauthn-db
ndom91 May 11, 2024
20f6aa9
Merge branch 'main' into ndom91/nextjs-example-app-webauthn-db
ndom91 May 11, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,11 @@ jobs:
if: ${{ github.repository == 'nextauthjs/next-auth' && github.event_name == 'push' && github.ref == 'refs/heads/main' }}
- name: Build
run: pnpm build
- name: Run unit tests
run: pnpm test
- name: Lint
run: pnpm lint
timeout-minutes: 15
- name: Run unit tests
run: pnpm test
- name: Get installed Playwright version
id: playwright-version
run: echo "PLAYWRIGHT_VERSION=$(pnpx playwright -V | awk '{ print $2 }')" >> $GITHUB_ENV
Expand Down
4 changes: 2 additions & 2 deletions apps/examples/nextjs/app/api-example/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ export default function Page() {
</CustomLink>{" "}
method.
</p>
<div className="flex flex-col rounded-md bg-neutral-100">
<div className="p-4 font-bold rounded-t-md bg-neutral-200">
<div className="flex flex-col bg-gray-100 rounded-md">
<div className="p-4 font-bold bg-gray-200 rounded-t-md">
Data from API Route
</div>
<pre className="py-6 px-4 whitespace-pre-wrap break-all">
Expand Down
9 changes: 7 additions & 2 deletions apps/examples/nextjs/app/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,13 @@ export default async function Index() {
</CustomLink>{" "}
examples to see how to secure pages and get session data.
</div>
<div className="flex flex-col rounded-md bg-neutral-100">
<div className="p-4 font-bold rounded-t-md bg-neutral-200">
<div>
WebAuthn users are reset on every deploy, don't expect your test user(s)
to still be available after a few days. It is designed to only
demonstrate registration, login, and logout briefly.
</div>
<div className="flex flex-col bg-gray-100 rounded-md">
<div className="p-4 font-bold bg-gray-200 rounded-t-md">
Current Session
</div>
<pre className="py-6 px-4 whitespace-pre-wrap break-all">
Expand Down
18 changes: 15 additions & 3 deletions apps/examples/nextjs/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import LinkedIn from "next-auth/providers/linkedin"
import Netlify from "next-auth/providers/netlify"
import Okta from "next-auth/providers/okta"
import Passage from "next-auth/providers/passage"
import Passkey from "next-auth/providers/passkey"
import Pinterest from "next-auth/providers/pinterest"
import Reddit from "next-auth/providers/reddit"
import Slack from "next-auth/providers/slack"
Expand All @@ -27,11 +28,15 @@ import Twitch from "next-auth/providers/twitch"
import Twitter from "next-auth/providers/twitter"
import WorkOS from "next-auth/providers/workos"
import Zoom from "next-auth/providers/zoom"

import { createStorage } from "unstorage"
import { UnstorageAdapter } from "@auth/unstorage-adapter"
import type { NextAuthConfig } from "next-auth"

export const config = {
const storage = createStorage()

const config = {
theme: { logo: "https://authjs.dev/img/logo-sm.png" },
adapter: UnstorageAdapter(storage),
providers: [
Apple,
Auth0,
Expand All @@ -58,6 +63,7 @@ export const config = {
LinkedIn,
Netlify,
Okta,
Passkey,
Passage,
Pinterest,
Reddit,
Expand Down Expand Up @@ -85,10 +91,16 @@ export const config = {
return token
},
async session({ session, token }) {
session.accessToken = token.accessToken
if (token?.accessToken) {
session.accessToken = token.accessToken
}
return session
},
},
experimental: {
enableWebAuthn: true,
},
debug: process.env.NODE_ENV !== "production" ? true : false,
} satisfies NextAuthConfig

export const { handlers, auth, signIn, signOut } = NextAuth(config)
Expand Down
43 changes: 16 additions & 27 deletions apps/examples/nextjs/components/client-example.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,8 @@ const UpdateForm = () => {
if (!session?.user) return null
return (
<>
<h2 className="text-xl font-bold">Updating the session</h2>
<form
onSubmit={async () => {
if (session) {
const newSession = await update({
...session,
user: { ...session.user, name },
})
console.log({ newSession })
}
}}
className="flex items-center space-x-2 w-full max-w-sm"
>
<h2 className="text-xl font-bold">Updating the session client-side</h2>
<div className="flex items-center space-x-2 w-full max-w-sm">
<Input
type="text"
placeholder="New name"
Expand All @@ -35,8 +24,10 @@ const UpdateForm = () => {
setName(e.target.value)
}}
/>
<Button type="submit">Update</Button>
</form>
<Button onClick={() => update({ user: { name } })} type="submit">
Update
</Button>
</div>
</>
)
}
Expand Down Expand Up @@ -82,32 +73,30 @@ export default function ClientExample() {
to provide the session data.
</p>

<div>
<div className="flex flex-col gap-4 p-4 bg-gray-100 rounded-md">
<h2 className="text-xl font-bold">Third-party backend integration</h2>
<p>
Press the button below to send a request to our{" "}
Press the button to send a request to our{" "}
<CustomLink href="https://github.com/nextauthjs/authjs-third-party-backend">
<code>example backend</code>
</CustomLink>
.
. Read more{" "}
<CustomLink href="https://authjs.dev/guides/integrating-third-party-backends">
<code>here</code>
</CustomLink>
</p>
<div className="flex flex-col ">
<p>Note: This example only works when using the Keycloak provider.</p>
<div className="flex flex-col">
<Button
disabled={!session?.accessToken}
className="mt-4 mb-4"
onClick={makeRequestWithToken}
>
Make API Request
</Button>
</div>
<p>
Read more{" "}
<CustomLink href="https://authjs.dev/guides/integrating-third-party-backends">
<code>here</code>
</CustomLink>
</p>
<pre>{apiResponse}</pre>
<p className="italic">
Note: This example only works when using the Keycloak provider.
</p>
</div>

{status === "loading" ? (
Expand Down
4 changes: 2 additions & 2 deletions apps/examples/nextjs/components/session-data.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type { Session } from "next-auth"
export default function SessionData({ session }: { session: Session | null }) {
if (session?.user) {
return (
<div className="flex flex-col gap-4 w-full">
<div className="flex flex-col gap-4 p-4 w-full bg-gray-100 rounded-md">
<h2 className="text-xl font-bold">Current Session Data</h2>
{Object.keys(session.user).length > 3 ? (
<p>
Expand Down Expand Up @@ -31,7 +31,7 @@ export default function SessionData({ session }: { session: Session | null }) {
}

return (
<p>
<p className="p-4 w-full bg-gray-100 rounded-md">
No session data, please <em>Sign In</em> first.
</p>
)
Expand Down
17 changes: 7 additions & 10 deletions apps/examples/nextjs/components/user-button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,13 @@ export default async function UserButton() {
<DropdownMenuTrigger asChild>
<Button variant="ghost" className="relative w-8 h-8 rounded-full">
<Avatar className="w-8 h-8">
{session.user.image && (
<AvatarImage
src={
session.user.image ??
"https://source.boringavatars.com/beam/120"
}
alt={session.user.name ?? ""}
/>
)}
<AvatarFallback>{session.user.email}</AvatarFallback>
<AvatarImage
src={
session.user.image ??
"https://source.boringavatars.com/marble/120"
}
alt={session.user.name ?? ""}
/>
</Avatar>
</Button>
</DropdownMenuTrigger>
Expand Down
10 changes: 8 additions & 2 deletions apps/examples/nextjs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@
"Thang Huu Vu <hi@thvu.dev>"
],
"dependencies": {
"@auth/unstorage-adapter": "^2.0.0",
"@radix-ui/react-avatar": "^1.0.3",
"@radix-ui/react-collapsible": "^1.0.3",
"@radix-ui/react-dropdown-menu": "^2.0.5",
"@radix-ui/react-navigation-menu": "^1.1.3",
"@radix-ui/react-slot": "^1.0.2",
"@simplewebauthn/server": "^9.0.3",
"class-variance-authority": "^0.7.0",
"clsx": "^2.0.0",
"lucide-react": "^0.274.0",
Expand All @@ -32,15 +34,19 @@
"react": "^18.2.0",
"react-dom": "^18.2.0",
"tailwind-merge": "^1.14.0",
"tailwindcss-animate": "^1.0.7"
"tailwindcss-animate": "^1.0.7",
"unstorage": "^1.10.1"
},
"devDependencies": {
"@types/node": "^18",
"@types/node": "^20.12.8",
"@types/react": "^18.2.23",
"@types/react-dom": "^18.2.8",
"autoprefixer": "^10.4.15",
"postcss": "^8.4.29",
"tailwindcss": "^3.3.3",
"typescript": "^5.2.2"
},
"engines": {
"node": ">=20.0.0"
}
}
Loading
Loading