Skip to content

Commit 3f0974d

Browse files
feat: new auth & payment system (#1617)
This implements a new auth & payment system depending on better-auth and polar.sh for auth and payments respectively. Co-authored-by: Matthew Lipski <50169049+matthewlipski@users.noreply.github.com> Co-authored-by: Matthew Lipski <matthewlipski@gmail.com>
1 parent e9e2198 commit 3f0974d

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

+3489
-677
lines changed

docs/.env.local.example

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,30 @@
11
AUTH_SECRET= # Linux: `openssl rand -hex 32` or go to https://generate-secret.vercel.app/32
22

3-
AUTH_GITHUB_ID=
4-
AUTH_GITHUB_SECRET=
3+
# Better Auth Deployed URL
4+
BETTER_AUTH_URL=http://localhost:3000
55

6-
# The SENTRY_AUTH_TOKEN variable is picked up by the Sentry Build Plugin.
7-
# It's used for authentication when uploading source maps.
8-
SENTRY_AUTH_TOKEN=
6+
# ======= OPTIONAL =======
7+
8+
# # Polar Sandbox is used in dev mode: https://sandbox.polar.sh/
9+
# # You may need to delete your user in their dashboard if you get a "cannot attach new external ID error"
10+
# POLAR_ACCESS_TOKEN=
11+
# POLAR_WEBHOOK_SECRET=
12+
13+
# # In production, we use postgres
14+
# POSTGRES_URL=
15+
16+
# # Email
17+
# SMTP_HOST=
18+
# SMTP_USER=
19+
# SMTP_PASS=
20+
# SMTP_PORT=
21+
# # Insecure if false, secure if any other value
22+
# SMTP_SECURE=false
23+
24+
# # For GitHub Signin method
25+
# AUTH_GITHUB_ID=
26+
# AUTH_GITHUB_SECRET=
27+
28+
# # The SENTRY_AUTH_TOKEN variable is picked up by the Sentry Build Plugin.
29+
# # It's used for authentication when uploading source maps.
30+
# SENTRY_AUTH_TOKEN=

docs/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,4 @@ next-env.d.ts
3737

3838
# Sentry Config File
3939
.env.sentry-build-plugin
40+
*.db

docs/DEVELOPMENT.md

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
# Website Development
2+
3+
To get started with development of the website, you can follow these steps:
4+
5+
1. Initialize the DB
6+
7+
If you haven't already, you can initialize the database with the following command:
8+
9+
```bash
10+
cd docs && pnpm run init-db
11+
```
12+
13+
This will initialize an SQLite database at `./docs/sqlite.db`.
14+
15+
2. Setup environment variables
16+
17+
Copy the `.env.example` file to `.env.local` and set the environment variables.
18+
19+
```bash
20+
cp .env.example .env.local
21+
```
22+
23+
If you want to test logging in, or payments see more information below [in the environment variables section](#environment-variables).
24+
25+
3. Start the development server
26+
27+
```bash
28+
cd docs && pnpm run dev
29+
```
30+
31+
This will start the development server on port 3000.
32+
33+
## Environment Variables
34+
35+
### Logging in
36+
37+
To test logging in, you can set the following environment variables:
38+
39+
```bash
40+
AUTH_SECRET=test
41+
# Github OAuth optionally
42+
AUTH_GITHUB_ID=test
43+
AUTH_GITHUB_SECRET=test
44+
```
45+
46+
Note: the GITHUB_ID and GITHUB_SECRET are optional, but if you want to test logging in with Github you'll need to set them. For local development, you'll need to set the callback URL to `http://localhost:3000/api/auth/callback/github`
47+
48+
### Payments
49+
50+
To test payments, you can set the following environment variables:
51+
52+
```bash
53+
POLAR_ACCESS_TOKEN=test
54+
POLAR_WEBHOOK_SECRET=test
55+
```
56+
57+
For testing payments, you'll need access to the polar sandbox which needs to be configured to point a webhook to your local server. This can be configured at: <https://sandbox.polar.sh/dashboard/blocknote/settings/webhooks>
58+
59+
You'll need something like [ngrok](https://ngrok.com/) to expose your local server to the internet.
60+
61+
```bash
62+
ngrok http http://localhost:3000
63+
```
64+
65+
You'll need the webhook to point to ngrok like so:
66+
67+
```
68+
https://0000-00-00-000-00.ngrok-free.app/api/auth/polar/webhooks
69+
```
70+
71+
With this webhook pointing to your local server, you should be able to test payments.
72+
73+
### Email sending
74+
75+
Note, this is not required, if email sending is not configured, the app will log the email it would send to the console. Often this is more convenient for development.
76+
77+
To test email sending, you can set the following environment variables:
78+
79+
```bash
80+
SMTP_HOST=
81+
SMTP_USER=
82+
SMTP_PASS=
83+
SMTP_PORT=
84+
SMTP_SECURE=false
85+
```
86+
87+
When configured, you'll be able to send emails to the email address you've configured.
88+
89+
To setup with protonmail, you'll need to go to <https://account.proton.me/u/0/mail/imap-smtp> and create a new SMTP submission token.
90+
91+
You'll need to set the following environment variables:
92+
93+
```bash
94+
SMTP_HOST=smtp.protonmail.com
95+
SMTP_USER=my.email@protonmail.com
96+
SMTP_PASS=my-smtp-token
97+
SMTP_PORT=587
98+
SMTP_SECURE=false
99+
```

docs/app/api/auth/[...all]/route.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import { auth } from "../../../../auth";
2+
import { toNextJsHandler } from "better-auth/next-js";
3+
4+
export const { POST, GET } = toNextJsHandler(auth);

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

Lines changed: 0 additions & 2 deletions
This file was deleted.

docs/app/portal/page.tsx

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
"use client";
2+
3+
import { useSession } from "@/util/auth-client";
4+
5+
// Just shows session info
6+
export default function Me() {
7+
const { data: session } = useSession();
8+
9+
if (!session) {
10+
return <div>Not authenticated</div>;
11+
}
12+
13+
return (
14+
<div>
15+
<h1>Welcome {session.user.name}</h1>
16+
<pre>{JSON.stringify(session, null, 2)}</pre>
17+
</div>
18+
);
19+
};

docs/app/signin/page.tsx

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { Metadata } from "next";
2+
import dynamic from "next/dynamic";
3+
import { Suspense } from "react";
4+
5+
export const metadata: Metadata = {
6+
title: "Login",
7+
};
8+
9+
// dynamic import because we use search params in the client component
10+
const AuthenticationPage = dynamic(
11+
() => import("../../components/AuthenticationPage"),
12+
);
13+
14+
export default function Register() {
15+
return (
16+
<Suspense>
17+
<AuthenticationPage variant="email" />
18+
</Suspense>
19+
);
20+
}

docs/app/signin/password/page.tsx

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { Metadata } from "next";
2+
import { Suspense } from "react";
3+
import dynamic from "next/dynamic";
4+
5+
export const metadata: Metadata = {
6+
title: "Password Login",
7+
};
8+
9+
// dynamic import because we use search params in the client component
10+
const AuthenticationPage = dynamic(
11+
() => import("../../../components/AuthenticationPage"),
12+
);
13+
14+
export default function Register() {
15+
return (
16+
<Suspense>
17+
<AuthenticationPage variant="password" />
18+
</Suspense>
19+
);
20+
}

docs/app/signup/page.tsx

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { Metadata } from "next";
2+
import { Suspense } from "react";
3+
import dynamic from "next/dynamic";
4+
5+
// dynamic import because we use search params in the client component
6+
const AuthenticationPage = dynamic(
7+
() => import("../../components/AuthenticationPage"),
8+
);
9+
10+
export const metadata: Metadata = {
11+
title: "Sign-up",
12+
};
13+
14+
export default function Register() {
15+
return (
16+
<Suspense>
17+
<AuthenticationPage variant="register" />
18+
</Suspense>
19+
);
20+
}

0 commit comments

Comments
 (0)