File tree Expand file tree Collapse file tree 14 files changed +128
-102
lines changed Expand file tree Collapse file tree 14 files changed +128
-102
lines changed Original file line number Diff line number Diff line change @@ -5,14 +5,14 @@ import { useEffect, useState } from "react"
5
5
export default function Page ( ) {
6
6
const [ data , setData ] = useState ( )
7
7
useEffect ( ( ) => {
8
- ; ( async ( ) => {
8
+ ; ( async ( ) => {
9
9
const res = await fetch ( "/api/protected" )
10
10
const json = await res . json ( )
11
11
setData ( json )
12
12
} ) ( )
13
13
} , [ ] )
14
14
return (
15
- < div className = "space-y-2 " >
15
+ < div className = "flex flex-col gap-6 " >
16
16
< h1 className = "text-3xl font-bold" > Route Handler Usage</ h1 >
17
17
< p >
18
18
This page fetches data from an API{ " " }
@@ -25,10 +25,14 @@ export default function Page() {
25
25
</ CustomLink > { " " }
26
26
method.
27
27
</ p >
28
- < h2 className = "text-xl font-bold" > Data from API Route:</ h2 >
29
- < pre >
30
- < code > { JSON . stringify ( data , null , 2 ) } </ code >
31
- </ pre >
28
+ < div className = "flex flex-col rounded-md bg-neutral-100" >
29
+ < div className = "p-4 font-bold rounded-t-md bg-neutral-200" >
30
+ Data from API Route
31
+ </ div >
32
+ < pre className = "py-6 px-4 whitespace-pre-wrap break-all" >
33
+ { JSON . stringify ( data , null , 2 ) }
34
+ </ pre >
35
+ </ div >
32
36
</ div >
33
37
)
34
38
}
Original file line number Diff line number Diff line change @@ -5,7 +5,7 @@ import { SessionProvider } from "next-auth/react"
5
5
export default async function ClientPage ( ) {
6
6
const session = await auth ( )
7
7
if ( session ?. user ) {
8
- // @ts -expect-error TODO: Look into https://react.dev/reference/react/experimental_taintObjectReference
8
+ // TODO: Look into https://react.dev/reference/react/experimental_taintObjectReference
9
9
// filter out sensitive data before passing to client.
10
10
session . user = {
11
11
name : session . user . name ,
@@ -15,7 +15,7 @@ export default async function ClientPage() {
15
15
}
16
16
17
17
return (
18
- < SessionProvider session = { session } >
18
+ < SessionProvider basePath = { "/auth" } session = { session } >
19
19
< ClientExample />
20
20
</ SessionProvider >
21
21
)
Original file line number Diff line number Diff line change 34
34
35
35
--radius : 0.5rem ;
36
36
}
37
-
38
- .dark {
39
- --background : 222.2 84% 4.9% ;
40
- --foreground : 210 40% 98% ;
41
-
42
- --card : 222.2 84% 4.9% ;
43
- --card-foreground : 210 40% 98% ;
44
-
45
- --popover : 222.2 84% 4.9% ;
46
- --popover-foreground : 210 40% 98% ;
47
-
48
- --primary : 210 40% 98% ;
49
- --primary-foreground : 222.2 47.4% 11.2% ;
50
-
51
- --secondary : 217.2 32.6% 17.5% ;
52
- --secondary-foreground : 210 40% 98% ;
53
-
54
- --muted : 217.2 32.6% 17.5% ;
55
- --muted-foreground : 215 20.2% 65.1% ;
56
-
57
- --accent : 217.2 32.6% 17.5% ;
58
- --accent-foreground : 210 40% 98% ;
59
-
60
- --destructive : 0 62.8% 30.6% ;
61
- --destructive-foreground : 210 40% 98% ;
62
-
63
- --border : 217.2 32.6% 17.5% ;
64
- --input : 217.2 32.6% 17.5% ;
65
- --ring : 212.7 26.8% 83.9% ;
66
- }
67
37
}
68
38
69
39
@layer base {
70
40
* {
71
41
@apply border-border;
72
42
}
43
+
73
44
body {
74
45
@apply bg-background text-foreground;
75
46
}
Original file line number Diff line number Diff line change 1
1
import CustomLink from "@/components/custom-link"
2
- import packageJSON from "../package.json"
2
+ import { auth } from "auth"
3
+
4
+ export default async function Index ( ) {
5
+ const session = await auth ( )
3
6
4
- export default function Index ( ) {
5
7
return (
6
- < div className = "space-y-2 " >
8
+ < div className = "flex flex-col gap-6 " >
7
9
< h1 className = "text-3xl font-bold" > NextAuth.js Example</ h1 >
8
- < p >
10
+ < div >
9
11
This is an example site to demonstrate how to use{ " " }
10
12
< CustomLink href = "https://nextjs.authjs.dev" > NextAuth.js</ CustomLink > { " " }
11
13
for authentication. Check out the{ " " }
@@ -17,12 +19,15 @@ export default function Index() {
17
19
Client
18
20
</ CustomLink > { " " }
19
21
examples to see how to secure pages and get session data.
20
- </ p >
21
- < p >
22
- Current{ " " }
23
- < CustomLink href = "https://nextjs.authjs.dev" > NextAuth.js</ CustomLink > { " " }
24
- version: < em > next-auth@{ packageJSON . dependencies [ "next-auth" ] } </ em >
25
- </ p >
22
+ </ div >
23
+ < div className = "flex flex-col rounded-md bg-neutral-100" >
24
+ < div className = "p-4 font-bold rounded-t-md bg-neutral-200" >
25
+ Current Session
26
+ </ div >
27
+ < pre className = "py-6 px-4 whitespace-pre-wrap break-all" >
28
+ { JSON . stringify ( session , null , 2 ) }
29
+ </ pre >
30
+ </ div >
26
31
</ div >
27
32
)
28
33
}
Original file line number Diff line number Diff line change @@ -140,6 +140,10 @@ export const config = {
140
140
if ( pathname === "/middleware-example" ) return ! ! auth
141
141
return true
142
142
} ,
143
+ jwt ( { token, trigger, session } ) {
144
+ if ( trigger === "update" ) token . name = session . user . name
145
+ return token
146
+ } ,
143
147
} ,
144
148
} satisfies NextAuthConfig
145
149
Original file line number Diff line number Diff line change @@ -9,7 +9,7 @@ import CustomLink from "./custom-link"
9
9
10
10
const UpdateForm = ( ) => {
11
11
const { data : session , update } = useSession ( )
12
- const [ name , setName ] = useState ( session ?. user ?. name ?? "" )
12
+ const [ name , setName ] = useState ( `New ${ session ?. user ?. name } ` ?? "" )
13
13
14
14
if ( ! session ?. user ) return null
15
15
return (
@@ -25,11 +25,11 @@ const UpdateForm = () => {
25
25
console . log ( { newSession } )
26
26
}
27
27
} }
28
- className = "flex items-center w-full max-w-sm space-x-2 "
28
+ className = "flex items-center space-x-2 w-full max-w-sm"
29
29
>
30
30
< Input
31
31
type = "text"
32
- placeholder = { session . user . name ?? "" }
32
+ placeholder = "New name"
33
33
value = { name }
34
34
onChange = { ( e ) => {
35
35
setName ( e . target . value )
@@ -43,9 +43,10 @@ const UpdateForm = () => {
43
43
44
44
export default function ClientExample ( ) {
45
45
const { data : session , status } = useSession ( )
46
+
46
47
return (
47
- < div className = "space-y-2 " >
48
- < h1 className = "text-3xl font-bold" > Client Side Rendering Usage </ h1 >
48
+ < div className = "flex flex-col gap-4 " >
49
+ < h1 className = "text-3xl font-bold" > Client Side Rendering</ h1 >
49
50
< p >
50
51
This page fetches session data client side using the{ " " }
51
52
< CustomLink href = "https://nextjs.authjs.dev/react#usesession" >
Original file line number Diff line number Diff line change @@ -28,11 +28,14 @@ const CustomLink = ({
28
28
href = { href }
29
29
target = "_blank"
30
30
rel = "noopener noreferrer"
31
- className = { cn ( "items-center underline" , className ) }
31
+ className = { cn (
32
+ "inline-flex align-baseline gap-1 items-center underline underline-offset-4" ,
33
+ className
34
+ ) }
32
35
{ ...rest }
33
36
>
34
- { children }
35
- < ExternalLink className = " ml-0.5 h-4 w-4 inline-block " />
37
+ < span > { children } </ span >
38
+ < ExternalLink className = "inline-block ml-0.5 w-4 h-4 " />
36
39
</ Link >
37
40
)
38
41
}
Original file line number Diff line number Diff line change 1
1
import CustomLink from "./custom-link"
2
+ import packageJSON from "next-auth/package.json"
2
3
3
4
export default function Footer ( ) {
4
5
return (
5
- < footer className = "flex flex-col w-full px-4 mx-0 my-4 space-y-1 text-sm md:max-w-3xl md:my-12 md:mx-auto sm:px-6 md:h-5 md:items-center md:space-y-0 md:space-x-4 md:flex-row" >
6
- < CustomLink href = "https://nextjs.authjs.dev" > Documentation</ CustomLink >
7
- < CustomLink href = "https://www.npmjs.com/package/next-auth" >
8
- NPM
9
- </ CustomLink >
10
- < CustomLink href = "https://github.com/nextauthjs/next-auth/tree/main/apps/examples/nextjs" >
11
- Source on GitHub
12
- </ CustomLink >
13
- < CustomLink href = "/policy" > Policy</ CustomLink >
6
+ < footer className = "flex flex-col gap-4 px-4 my-4 mx-0 w-full text-sm sm:flex-row sm:justify-between sm:items-center sm:px-6 sm:my-12 sm:mx-auto sm:max-w-3xl sm:h-5" >
7
+ < div className = "flex flex-col gap-4 sm:flex-row" >
8
+ < CustomLink href = "https://nextjs.authjs.dev" > Documentation</ CustomLink >
9
+ < CustomLink href = "https://www.npmjs.com/package/next-auth" >
10
+ NPM
11
+ </ CustomLink >
12
+ < CustomLink href = "https://github.com/nextauthjs/next-auth/tree/main/apps/examples/nextjs" >
13
+ Source on GitHub
14
+ </ CustomLink >
15
+ < CustomLink href = "/policy" > Policy</ CustomLink >
16
+ </ div >
17
+ < div className = "flex gap-2 justify-start items-center" >
18
+ < img
19
+ className = "size-5"
20
+ src = "https://authjs.dev/img/logo/logo-sm.webp"
21
+ alt = "Auth.js Logo"
22
+ />
23
+ < CustomLink href = "https://npmjs.org/package/next-auth" >
24
+ { packageJSON . version }
25
+ </ CustomLink >
26
+ </ div >
14
27
</ footer >
15
28
)
16
29
}
Original file line number Diff line number Diff line change @@ -18,16 +18,24 @@ import { Button } from "./ui/button"
18
18
19
19
export function MainNav ( ) {
20
20
return (
21
- < div className = "flex items-center space-x-2 lg:space-x-6 " >
21
+ < div className = "flex gap-4 items-center " >
22
22
< CustomLink href = "/" >
23
23
< Button variant = "ghost" className = "p-0" >
24
- < Image src = "/logo.png" alt = "Home" width = "32" height = "32" />
24
+ < Image
25
+ src = "/logo.png"
26
+ alt = "Home"
27
+ width = "32"
28
+ height = "32"
29
+ className = "min-w-8"
30
+ />
25
31
</ Button >
26
32
</ CustomLink >
27
33
< NavigationMenu >
28
34
< NavigationMenuList >
29
35
< NavigationMenuItem >
30
- < NavigationMenuTrigger > Server Side</ NavigationMenuTrigger >
36
+ < NavigationMenuTrigger className = "px-2" >
37
+ Server Side
38
+ </ NavigationMenuTrigger >
31
39
< NavigationMenuContent >
32
40
< ul className = "grid gap-3 p-6 md:w-[400px] lg:w-[500px] lg:grid-cols-[.75fr_1fr]" >
33
41
< ListItem href = "/server-example" title = "RSC Example" >
Original file line number Diff line number Diff line change @@ -3,7 +3,7 @@ import type { Session } from "next-auth"
3
3
export default function SessionData ( { session } : { session : Session | null } ) {
4
4
if ( session ?. user ) {
5
5
return (
6
- < div className = "w-full space-y-2 overflow-auto " >
6
+ < div className = "flex flex-col gap-4 w-full " >
7
7
< h2 className = "text-xl font-bold" > Current Session Data</ h2 >
8
8
{ Object . keys ( session . user ) . length > 3 ? (
9
9
< p >
@@ -18,7 +18,14 @@ export default function SessionData({ session }: { session: Session | null }) {
18
18
the page to avoid exposing sensitive information.
19
19
</ p >
20
20
) }
21
- < pre > { JSON . stringify ( session , null , 2 ) } </ pre >
21
+ < div className = "flex flex-col rounded-md bg-neutral-100" >
22
+ < div className = "p-4 font-bold rounded-t-md bg-neutral-200" >
23
+ Session
24
+ </ div >
25
+ < pre className = "py-6 px-4 whitespace-pre-wrap break-all" >
26
+ { JSON . stringify ( session , null , 2 ) }
27
+ </ pre >
28
+ </ div >
22
29
</ div >
23
30
)
24
31
}
You can’t perform that action at this time.
0 commit comments