Skip to content

Commit 928c6ba

Browse files
authored
Initial commit
0 parents  commit 928c6ba

Some content is hidden

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

49 files changed

+3714
-0
lines changed

.env.local.example

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
AUTH_SECRET= # `npx auth secret` or `openssl rand -hex 32`
2+
3+
AUTH_AUTH0_ID=
4+
AUTH_AUTH0_SECRET=
5+
AUTH_AUTH0_ISSUER=
6+
7+
AUTH_FACEBOOK_ID=
8+
AUTH_FACEBOOK_SECRET=
9+
10+
AUTH_GITHUB_ID=
11+
AUTH_GITHUB_SECRET=
12+
13+
AUTH_GOOGLE_ID=
14+
AUTH_GOOGLE_SECRET=
15+
16+
AUTH_TWITTER_ID=
17+
AUTH_TWITTER_SECRET=
18+
19+
# THIRD_PARTY_API_EXAMPLE_BACKEND= # Read more at https://authjs.dev/guides/integrating-third-party-backends
20+
21+
# AUTH_TRUST_HOST=1 # Read more at https://authjs.dev/getting-started/deployment#auth_trust_host

.github/FUNDING.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# https://docs.github.com/en/github/administering-a-repository/displaying-a-sponsor-button-in-your-repository
2+
3+
open_collective: nextauth
4+
github: [balazsorban44, ThangHuuVu]

.gitignore

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
.DS_Store
2+
3+
node_modules/
4+
logs
5+
*.log
6+
npm-debug.log*
7+
yarn-debug.log*
8+
yarn-error.log*
9+
lerna-debug.log*
10+
.yarn-integrity
11+
.npm
12+
13+
.eslintcache
14+
15+
*.tsbuildinfo
16+
next-env.d.ts
17+
18+
.next
19+
.vercel
20+
.env*.local

Dockerfile

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
FROM node:20-alpine AS base
2+
3+
# Install dependencies only when needed
4+
FROM base AS deps
5+
# Check https://github.com/nodejs/docker-node/tree/b4117f9333da4138b03a546ec926ef50a31506c3#nodealpine to understand why libc6-compat might be needed.
6+
RUN apk add --no-cache libc6-compat
7+
WORKDIR /app
8+
9+
# Install dependencies
10+
COPY package.json ./
11+
RUN npm i
12+
13+
14+
# Rebuild the source code only when needed
15+
FROM base AS builder
16+
WORKDIR /app
17+
COPY --from=deps /app/node_modules ./node_modules
18+
COPY . .
19+
20+
# Next.js collects completely anonymous telemetry data about general usage.
21+
# Learn more here: https://nextjs.org/telemetry
22+
# Uncomment the following line in case you want to disable telemetry during the build.
23+
# ENV NEXT_TELEMETRY_DISABLED 1
24+
25+
RUN npm run build
26+
27+
# Production image, copy all the files and run next
28+
FROM base AS runner
29+
WORKDIR /app
30+
31+
ENV NODE_ENV production
32+
# Uncomment the following line in case you want to disable telemetry during runtime.
33+
# ENV NEXT_TELEMETRY_DISABLED 1
34+
35+
RUN addgroup --system --gid 1001 nodejs
36+
RUN adduser --system --uid 1001 nextjs
37+
38+
COPY --from=builder /app/public ./public
39+
40+
# Set the correct permission for prerender cache
41+
RUN mkdir .next
42+
RUN chown nextjs:nodejs .next
43+
44+
# Automatically leverage output traces to reduce image size
45+
# https://nextjs.org/docs/advanced-features/output-file-tracing
46+
COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./
47+
COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static
48+
49+
USER nextjs
50+
51+
EXPOSE 3000
52+
53+
ENV PORT 3000
54+
55+
# server.js is created by next build from the standalone output
56+
# https://nextjs.org/docs/pages/api-reference/next-config-js/output
57+
CMD HOSTNAME="0.0.0.0" node server.js

LICENSE

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
ISC License
2+
3+
Copyright (c) 2022-2024, Balázs Orbán
4+
5+
Permission to use, copy, modify, and/or distribute this software for any
6+
purpose with or without fee is hereby granted, provided that the above
7+
copyright notice and this permission notice appear in all copies.
8+
9+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10+
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11+
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12+
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13+
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14+
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15+
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

README.md

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
> The example repository is maintained from a [monorepo](https://github.com/nextauthjs/next-auth/tree/main/apps/examples/nextjs). Pull Requests should be opened against [`nextauthjs/next-auth`](https://github.com/nextauthjs/next-auth).
2+
3+
<p align="center">
4+
<br/>
5+
<a href="https://authjs.dev" target="_blank"><img width="150px" src="https://authjs.dev/img/logo-sm.png" /></a>
6+
<h3 align="center">NextAuth.js Example App</h3>
7+
<p align="center">
8+
Open Source. Full Stack. Own Your Data.
9+
</p>
10+
<p align="center" style="align: center;">
11+
<a href="https://npm.im/next-auth">
12+
<img alt="npm" src="https://img.shields.io/npm/v/next-auth?color=green&label=next-auth">
13+
</a>
14+
<a href="https://bundlephobia.com/result?p=next-auth-example">
15+
<img src="https://img.shields.io/bundlephobia/minzip/next-auth?label=next-auth" alt="Bundle Size"/>
16+
</a>
17+
<a href="https://www.npmtrends.com/next-auth">
18+
<img src="https://img.shields.io/npm/dm/next-auth?label=next-auth%20downloads" alt="Downloads" />
19+
</a>
20+
<a href="https://npm.im/next-auth">
21+
<img src="https://img.shields.io/badge/npm-TypeScript-blue" alt="TypeScript" />
22+
</a>
23+
</p>
24+
</p>
25+
26+
## Overview
27+
28+
NextAuth.js is a complete open source authentication solution.
29+
30+
This is an example application that shows how `next-auth` is applied to a basic Next.js app.
31+
32+
The deployed version can be found at [`next-auth-example.vercel.app`](https://next-auth-example.vercel.app)
33+
34+
### About NextAuth.js
35+
36+
NextAuth.js is an easy to implement, full-stack (client/server) open source authentication library originally designed for [Next.js](https://nextjs.org) and [Serverless](https://vercel.com). Our goal is to [support even more frameworks](https://github.com/nextauthjs/next-auth/issues/2294) in the future.
37+
38+
Go to [next-auth.js.org](https://authjs.dev) for more information and documentation.
39+
40+
> _NextAuth.js is not officially associated with Vercel or Next.js._
41+
42+
## Getting Started
43+
44+
### 1. Clone the repository and install dependencies
45+
46+
```
47+
git clone https://github.com/nextauthjs/next-auth-example.git
48+
cd next-auth-example
49+
npm install
50+
```
51+
52+
### 2. Configure your local environment
53+
54+
Copy the .env.local.example file in this directory to .env.local (which will be ignored by Git):
55+
56+
```
57+
cp .env.local.example .env.local
58+
```
59+
60+
Add details for one or more providers (e.g. Google, Twitter, GitHub, Email, etc).
61+
62+
#### Database
63+
64+
A database is needed to persist user accounts and to support email sign in. However, you can still use NextAuth.js for authentication without a database by using OAuth for authentication. If you do not specify a database, [JSON Web Tokens](https://jwt.io/introduction) will be enabled by default.
65+
66+
You **can** skip configuring a database and come back to it later if you want.
67+
68+
For more information about setting up a database, please check out the following links:
69+
70+
- Docs: [authjs.dev/reference/core/adapters](https://authjs.dev/reference/core/adapters)
71+
72+
### 3. Configure Authentication Providers
73+
74+
1. Review and update options in `auth.ts` as needed.
75+
76+
2. When setting up OAuth, in the developer admin page for each of your OAuth services, you should configure the callback URL to use a callback path of `{server}/api/auth/callback/{provider}`.
77+
78+
e.g. For Google OAuth you would use: `http://localhost:3000/api/auth/callback/google`
79+
80+
A list of configured providers and their callback URLs is available from the endpoint `api/auth/providers`. You can find more information at https://authjs.dev/getting-started/providers/oauth-tutorial
81+
82+
1. You can also choose to specify an SMTP server for passwordless sign in via email.
83+
84+
### 4. Start the application
85+
86+
To run your site locally, use:
87+
88+
```
89+
npm run dev
90+
```
91+
92+
To run it in production mode, use:
93+
94+
```
95+
npm run build
96+
npm run start
97+
```
98+
99+
### 5. Preparing for Production
100+
101+
Follow the [Deployment documentation](https://authjs.dev/getting-started/deployment)
102+
103+
## Acknowledgements
104+
105+
<a href="https://vercel.com?utm_source=nextauthjs&utm_campaign=oss">
106+
<img width="170px" src="https://raw.githubusercontent.com/nextauthjs/next-auth/main/docs/static/img/powered-by-vercel.svg" alt="Powered By Vercel" />
107+
</a>
108+
<p align="left">Thanks to Vercel sponsoring this project by allowing it to be deployed for free for the entire NextAuth.js Team</p>
109+
110+
## License
111+
112+
ISC

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+
29+
return stripContentEncoding(result)
30+
}
31+
32+
export const dynamic = "force-dynamic"
33+
34+
export { handler as GET, handler as POST }

app/api-example/page.tsx

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
"use client"
2+
import CustomLink from "@/components/custom-link"
3+
import { useEffect, useState } from "react"
4+
5+
export default function Page() {
6+
const [data, setData] = useState()
7+
useEffect(() => {
8+
;(async () => {
9+
const res = await fetch("/api/protected")
10+
const json = await res.json()
11+
setData(json)
12+
})()
13+
}, [])
14+
return (
15+
<div className="flex flex-col gap-6">
16+
<h1 className="text-3xl font-bold">Route Handler Usage</h1>
17+
<p>
18+
This page fetches data from an API{" "}
19+
<CustomLink href="https://nextjs.org/docs/app/building-your-application/routing/route-handlers">
20+
Route Handler
21+
</CustomLink>
22+
. The API is protected using the universal{" "}
23+
<CustomLink href="https://nextjs.authjs.dev#auth">
24+
<code>auth()</code>
25+
</CustomLink>{" "}
26+
method.
27+
</p>
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>
36+
</div>
37+
)
38+
}

app/api/auth/[...nextauth]/route.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
import { handlers } from "auth"
2+
export const { GET, POST } = handlers

app/api/protected/route.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { auth } from "auth"
2+
3+
export const GET = auth((req) => {
4+
if (req.auth) {
5+
return Response.json({ data: "Protected data" })
6+
}
7+
8+
return Response.json({ message: "Not authenticated" }, { status: 401 })
9+
}) as any // TODO: Fix `auth()` return type

0 commit comments

Comments
 (0)