Skip to content

Commit 57dbfcd

Browse files
committed
🔄 Synced local '.' with remote 'apps/examples/nextjs'
1 parent 04913fb commit 57dbfcd

File tree

4 files changed

+96
-1
lines changed

4 files changed

+96
-1
lines changed

.env.local.example

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,6 @@ AUTH_GOOGLE_SECRET=
1616
AUTH_TWITTER_ID=
1717
AUTH_TWITTER_SECRET=
1818

19+
# THIRD_PARTY_API_EXAMPLE_BACKEND= # Read more at https://authjs.dev/guides/integrating-third-party-backends
20+
1921
# AUTH_TRUST_HOST=1 # Read more at https://authjs.dev/getting-started/deployment#auth_trust_host

app/[...proxy]/route.tsx

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { auth } from "@/auth"
2+
import { NextRequest } from "next/server"
3+
4+
// Review if we need this, and why
5+
function stripContentEncoding(result: Response) {
6+
const responseHeaders = new Headers(result.headers)
7+
responseHeaders.delete("content-encoding")
8+
9+
return new Response(result.body, {
10+
status: result.status,
11+
statusText: result.statusText,
12+
headers: responseHeaders,
13+
})
14+
}
15+
16+
export async function handler(request: NextRequest) {
17+
const session = await auth()
18+
19+
const headers = new Headers(request.headers)
20+
headers.set("Authorization", `Bearer ${session?.accessToken}`)
21+
22+
let backendUrl =
23+
process.env.THIRD_PARTY_API_EXAMPLE_BACKEND ??
24+
"https://authjs-third-party-backend.authjs.dev/"
25+
26+
let url = request.nextUrl.href.replace(request.nextUrl.origin, backendUrl)
27+
let result = await fetch(url, { headers, body: request.body })
28+
console.log("fetched", result)
29+
return stripContentEncoding(result)
30+
}
31+
32+
export const dynamic = "force-dynamic"
33+
34+
export { handler as GET, handler as POST }

auth.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import NextAuth from "next-auth"
2+
import "next-auth/jwt"
23

34
import Apple from "next-auth/providers/apple"
45
import Auth0 from "next-auth/providers/auth0"
@@ -76,11 +77,30 @@ export const config = {
7677
if (pathname === "/middleware-example") return !!auth
7778
return true
7879
},
79-
jwt({ token, trigger, session }) {
80+
jwt({ token, trigger, session, account }) {
8081
if (trigger === "update") token.name = session.user.name
82+
if (account?.provider === "keycloak") {
83+
return { ...token, accessToken: account.access_token }
84+
}
8185
return token
8286
},
87+
async session({ session, token }) {
88+
session.accessToken = token.accessToken
89+
return session
90+
},
8391
},
8492
} satisfies NextAuthConfig
8593

8694
export const { handlers, auth, signIn, signOut } = NextAuth(config)
95+
96+
declare module "next-auth" {
97+
interface Session {
98+
accessToken?: string
99+
}
100+
}
101+
102+
declare module "next-auth/jwt" {
103+
interface JWT {
104+
accessToken?: string
105+
}
106+
}

components/client-example.tsx

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,17 @@ const UpdateForm = () => {
4343

4444
export default function ClientExample() {
4545
const { data: session, status } = useSession()
46+
const [apiResponse, setApiResponse] = useState("")
47+
48+
const makeRequestWithToken = async () => {
49+
try {
50+
const response = await fetch("/api/authenticated/greeting")
51+
const data = await response.json()
52+
setApiResponse(JSON.stringify(data, null, 2))
53+
} catch (error) {
54+
setApiResponse("Failed to fetch data: " + error)
55+
}
56+
}
4657

4758
return (
4859
<div className="flex flex-col gap-4">
@@ -71,6 +82,34 @@ export default function ClientExample() {
7182
to provide the session data.
7283
</p>
7384

85+
<div>
86+
<h2 className="text-xl font-bold">Third-party backend integration</h2>
87+
<p>
88+
Press the button below to send a request to our{" "}
89+
<CustomLink href="https://github.com/nextauthjs/authjs-third-party-backend">
90+
<code>example backend</code>
91+
</CustomLink>
92+
.
93+
</p>
94+
<div className="flex flex-col ">
95+
<p>Note: This example only works when using the Keycloak provider.</p>
96+
<Button
97+
disabled={!session?.accessToken}
98+
className="mt-4 mb-4"
99+
onClick={makeRequestWithToken}
100+
>
101+
Make API Request
102+
</Button>
103+
</div>
104+
<p>
105+
Read more{" "}
106+
<CustomLink href="https://authjs.dev/guides/integrating-third-party-backends">
107+
<code>here</code>
108+
</CustomLink>
109+
</p>
110+
<pre>{apiResponse}</pre>
111+
</div>
112+
74113
{status === "loading" ? (
75114
<div>Loading...</div>
76115
) : (

0 commit comments

Comments
 (0)