Skip to content

Commit 8aa10f5

Browse files
authored
chore: Updated with-auth example (#1963)
1 parent d59409a commit 8aa10f5

File tree

25 files changed

+445
-254
lines changed

25 files changed

+445
-254
lines changed

examples/with-auth/.env.example

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1-
SESSION_SECRET = myverylonguniquesecretthatkeepsthingssafe
1+
# Generate using `openssl rand -hex 32`
2+
SESSION_SECRET = myverylongsessionsecretkeythatishouldchange
3+
4+
# Get credentials by creating an application at https://discord.com/developers/applications
25
DISCORD_ID =
36
DISCORD_SECRET =

examples/with-auth/README.md

Lines changed: 23 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,41 @@
1-
# SolidStart
1+
# SolidStart Template
22

3-
Everything you need to build a Solid project, powered by [`solid-start`](https://start.solidjs.com);
3+
The **with-auth** example demonstrates native, context-based authentication featuring OAuth and email-password login.
44

5-
## Creating a project
5+
## Installation
66

7-
```bash
8-
# create a new project in the current directory
9-
npm init solid@latest
7+
Generate the **with-auth** template using your preferred package manager
108

11-
# create a new project in my-app
12-
npm init solid@latest my-app
9+
```bash
10+
# using npm
11+
npm create solid@latest -- -s -t with-auth
1312
```
1413

15-
## Developing
16-
17-
Once you've created a project and installed dependencies with `npm install` (or `pnpm install` or `yarn`), start a development server:
18-
1914
```bash
20-
npm run dev
21-
22-
# or start the server and open the app in a new browser tab
23-
npm run dev -- --open
15+
# using pnpm
16+
pnpm create solid@latest -s -t with-auth
2417
```
2518

26-
## Env Vars
27-
28-
Rename the example file and add your Discord OAuth credentials:
29-
3019
```bash
31-
# rename example environment file
32-
cp .env.example .env
20+
# using bun
21+
bun create solid@latest --s --t with-auth
3322
```
3423

35-
Edit `.env` with your values:
24+
## Configuration
3625

37-
```dotenv
38-
DISCORD_ID=your-discord-client-id
39-
DISCORD_SECRET=your-discord-client-secret
40-
```
26+
1. Rename `.env.example` to `.env`
4127

42-
1. Create an application at [https://discord.com/developers/applications](https://discord.com/developers/applications) to obtain your client ID and secret.
43-
2. In the app's **OAuth2 → Redirects** settings, add:
28+
2. For Discord OAuth2 to work, update `.env` with your credentials:
4429

45-
```text
46-
http://localhost:3000/api/oauth/discord
30+
```dotenv
31+
DISCORD_ID=your-discord-client-id
32+
DISCORD_SECRET=your-discord-client-secret
4733
```
4834

49-
For more details on the [start-oauth](https://github.com/thomasbuilds/start-oauth) integration, see the repository.
50-
51-
## Building
52-
53-
Solid apps are built with _presets_, which optimise your project for deployment to different environments.
35+
- Create a Discord application at [discord.com/developers/applications](https://discord.com/developers/applications) to get your Client ID and Secret.
36+
- In the app's **OAuth2 → URL Generator** or **Redirects** section, add the following redirect URI:
37+
```
38+
http://localhost:3000/api/oauth/discord
39+
```
5440
55-
By default, `npm run build` will generate a Node app that you can run with `npm start`. To use a different preset, add it to the `devDependencies` in `package.json` and specify in your `app.config.js`.
41+
3. To configure additional providers, refer to the [start-oauth](https://github.com/thomasbuilds/start-oauth#README) documentation

examples/with-auth/app.config.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,7 @@ import { defineConfig } from "@solidjs/start/config";
22
import tailwindcss from "@tailwindcss/vite";
33

44
export default defineConfig({
5+
ssr: true, // false for client-side rendering only
6+
server: { preset: "" }, // your deployment
57
vite: { plugins: [tailwindcss()] }
68
});

examples/with-auth/package.json

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,17 @@
77
"start": "vinxi start"
88
},
99
"dependencies": {
10+
"@solidjs/meta": "^0.29.4",
1011
"@solidjs/router": "^0.15.3",
1112
"@solidjs/start": "^1.1.7",
12-
"solid-js": "^1.9.7",
13-
"start-oauth": "^1.2.4",
14-
"unstorage": "1.16.1",
13+
"solid-js": "^1.9.9",
14+
"start-oauth": "^1.3.0",
15+
"unstorage": "1.17.1",
1516
"vinxi": "^0.5.8"
1617
},
1718
"devDependencies": {
18-
"@tailwindcss/vite": "^4.1.11",
19-
"tailwindcss": "^4.1.11"
19+
"@tailwindcss/vite": "^4.1.13",
20+
"tailwindcss": "^4.1.13"
2021
},
2122
"engines": {
2223
"node": ">=22"
-664 Bytes
Binary file not shown.
Lines changed: 92 additions & 0 deletions
Loading

examples/with-auth/src/app.css

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,17 @@
11
@import "tailwindcss";
2+
3+
#app {
4+
user-select: none;
5+
}
6+
7+
main {
8+
@apply flex flex-col items-center justify-center min-h-screen bg-gray-50 gap-8 px-4;
9+
}
10+
11+
h1 {
12+
@apply uppercase text-6xl text-sky-700 font-thin;
13+
}
14+
15+
button {
16+
cursor: pointer;
17+
}

examples/with-auth/src/app.tsx

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
// @refresh reload
21
import { type RouteDefinition, Router } from "@solidjs/router";
32
import { FileRoutes } from "@solidjs/start/router";
3+
import { MetaProvider } from "@solidjs/meta";
44
import { Suspense } from "solid-js";
5-
import { querySession } from "./lib";
6-
import Session from "./lib/Context";
5+
import { querySession } from "./auth";
6+
import Auth from "./components/Context";
77
import Nav from "./components/Nav";
8+
import ErrorNotification from "./components/Error";
89
import "./app.css";
910

1011
export const route: RouteDefinition = {
@@ -15,12 +16,15 @@ export default function App() {
1516
return (
1617
<Router
1718
root={props => (
18-
<Session>
19-
<Suspense>
20-
<Nav />
21-
{props.children}
22-
</Suspense>
23-
</Session>
19+
<MetaProvider>
20+
<Auth>
21+
<Suspense>
22+
<Nav />
23+
{props.children}
24+
<ErrorNotification />
25+
</Suspense>
26+
</Auth>
27+
</MetaProvider>
2428
)}
2529
>
2630
<FileRoutes />
Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import { action, query, redirect } from "@solidjs/router";
2-
import { getSession, passwordSignIn } from "./server";
2+
import { getSession, passwordLogin } from "./server";
33

4-
// Define which routes require authentication
4+
// Define routes that require being logged in
55
const PROTECTED_ROUTES = ["/"];
66

7-
const isProtectedRoute = (path: string) =>
7+
const isProtected = (path: string) =>
88
PROTECTED_ROUTES.some(route =>
99
route.endsWith("/*")
1010
? path.startsWith(route.slice(0, -2))
@@ -16,17 +16,17 @@ export const querySession = query(async (path: string) => {
1616
const { data } = await getSession();
1717
if (path === "/login" && data.id) return redirect("/");
1818
if (data.id) return data;
19-
if (isProtectedRoute(path)) throw redirect(`/login?redirect=${path}`);
19+
if (isProtected(path)) throw redirect(`/login?redirect=${path}`);
2020
return null;
2121
}, "session");
2222

23-
export const passwdSignIn = action(async (formData: FormData) => {
23+
export const formLogin = action(async (formData: FormData) => {
2424
"use server";
2525
const email = formData.get("email");
2626
const password = formData.get("password");
2727
if (typeof email !== "string" || typeof password !== "string")
2828
return new Error("Email and password are required");
29-
return await passwordSignIn(email.trim().toLowerCase(), password);
29+
return await passwordLogin(email.trim().toLowerCase(), password);
3030
});
3131

3232
export const logout = action(async () => {

0 commit comments

Comments
 (0)