Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add apps/dev/solid-start. Not ready for merging due to cookie esm issue; help? #7203

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions apps/dev/solid-start/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
GITHUB_ID=
GITHUB_SECRET=
AUTH_SECRET=
27 changes: 27 additions & 0 deletions apps/dev/solid-start/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
dist
.solid
.output
.vercel
.netlify
netlify

# dependencies
/node_modules

# IDEs and editors
/.idea
.project
.classpath
*.launch
.settings/

# Temp
gitignore

# System Files
.DS_Store
Thumbs.db

.env

.vercel
85 changes: 85 additions & 0 deletions apps/dev/solid-start/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
> The example repository is maintained from a [monorepo](https://github.com/nextauthjs/next-auth/tree/main/apps/examples/solid-start). Pull Requests should be opened against [`nextauthjs/next-auth`](https://github.com/nextauthjs/next-auth).

<p align="center">
<br/>
<a href="https://authjs.dev" target="_blank">
<img height="64" src="https://authjs.dev/img/logo/logo-sm.png" />
</a>
<a href="https://start.solidjs.com" target="_blank">
<img height="64" src="https://www.solidjs.com/assets/logo-123b04bc.svg" />
</a>
<h3 align="center"><b>SolidStart Auth</b> - Example App</h3>
<p align="center">
Open Source. Full Stack. Own Your Data.
</p>
<p align="center" style="align: center;">
<a href="https://npm.im/@auth/solid-start">
<img alt="npm" src="https://img.shields.io/npm/v/@auth/solid-start?color=green&label=@auth/solid-start&style=flat-square">
</a>
<a href="https://bundlephobia.com/result?p=@auth/solid-start">
<img src="https://img.shields.io/bundlephobia/minzip/@auth/solid-start?label=size&style=flat-square" alt="Bundle Size"/>
</a>
<a href="https://www.npmtrends.com/@auth/solid-start">
<img src="https://img.shields.io/npm/dm/@auth/solid-start?label=downloads&style=flat-square" alt="Downloads" />
</a>
<a href="https://npm.im/@auth/solid-start">
<img src="https://img.shields.io/badge/TypeScript-blue?style=flat-square" alt="TypeScript" />
</a>
</p>
</p>

## Overview

This is the official SolidStart Auth example for [Auth.js](https://authjs.dev).


## Getting started

You can follow the guide below, or click the following button to deploy this example to [Vercel](https://vercel.com?utm_source=github&utm_medium=readme&utm_campaign=solid-start-auth-example).

[![Deploy with Vercel](https://vercel.com/button)](https://vercel.com/new/git/external?repository-url=https://github.com/nextauthjs/solid-start-auth-example&project-name=solid-start-auth-example&repository-name=solid-start-auth-example)

### Installing

```sh
pnpm add -D solid-start-vercel
```
```sh
npm i -D solid-start-vercel
```
```sh
yarn add -D solid-start-vercel
```

### Adding to Vite config

```ts
import solid from "solid-start/vite";
import dotenv from "dotenv";
import { defineConfig } from "vite";
// @ts-expect-error no typing
import vercel from "solid-start-vercel";

export default defineConfig(() => {
dotenv.config();
return {
plugins: [solid({ ssr: true, adapter: vercel({ edge: false }) })],
};
});

```

### Environment Variables

- `ENABLE_VC_BUILD`=`1` .

### Finishing up

Create a GitHub repo and push the code to it, then deploy it to Vercel.

## Acknowledgements

<a href="https://vercel.com?utm_source=nextauthjs&utm_campaign=oss">
<img width="170px" src="https://raw.githubusercontent.com/nextauthjs/next-auth/main/docs/static/img/powered-by-vercel.svg" alt="Powered By Vercel" />
</a>
<p align="left">Thanks to Vercel sponsoring this project by allowing it to be deployed for free for the entire Auth.js Team</p>
32 changes: 32 additions & 0 deletions apps/dev/solid-start/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"name": "my-app",
"scripts": {
"dev": "solid-start dev",
"build": "solid-start build",
"start": "solid-start start",
"lint": "eslint --fix \"**/*.{ts,tsx,js,jsx}\""
},
"type": "module",
"devDependencies": {
"autoprefixer": "^10.4.13",
"postcss": "^8.4.19",
"solid-start-node": "^0.2.26",
"solid-start-vercel": "^0.2.26",
"tailwindcss": "^3.2.4",
"typescript": "^4.8.3",
"vite": "^4.2.1"
},
"dependencies": {
"@auth/core": "workspace:*",
"@auth/solid-start": "workspace:*",
"@solidjs/meta": "^0.28.4",
"@solidjs/router": "^0.8.2",
"solid-js": "^1.7.3",
"solid-start": "^0.2.26",
"undici": "5.11.0",
"zod": "^3.19.1"
},
"engines": {
"node": ">=16"
}
}
6 changes: 6 additions & 0 deletions apps/dev/solid-start/postcss.config.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = {
plugins: {
tailwindcss: {},
autoprefixer: {},
},
};
Binary file added apps/dev/solid-start/public/favicon.ico
Binary file not shown.
72 changes: 72 additions & 0 deletions apps/dev/solid-start/src/components/NavBar/NavBar.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import { Match, Show, Switch, type Component } from "solid-js";
import { createServerData$ } from "solid-start/server";
import { authOpts } from "~/routes/api/auth/[...solidauth]";
import { signIn, signOut } from "@auth/solid-start/client";
import { getSession } from "@auth/solid-start";
import { A } from "solid-start";

interface INavBarProps {}

const NavBar: Component<INavBarProps> = () => {
const session = useSession();
return (
<header class="flex flex-col w-full gap-2 fixed left-2/4 right-2/4 -translate-x-2/4 items-center">
<nav class="w-[70vw] sm:w-2/4 lg:w-[40%] p-5 bg-[#0000000d] flex items-center justify-between rounded-lg">
<Show
when={session()?.user}
keyed
fallback={
<>
<p class="text-lg font-semibold">You are not signed in</p>
<button
class="p-2.5 rounded-lg bg-[#346df1] text-white text-lg font-bold flex items-center justify-center"
onClick={() => signIn("github")}
>
Sign in
</button>
</>
}
>
{(us) => (
<>
<div class="flex gap-2 items-center">
<Show when={us.image} keyed>
{(im) => <img src={im} class="w-12 h-12 rounded-full" />}
</Show>
<div class="flex flex-col">
<h3 class="font-bold text-lg">Signed in as</h3>
<p class="text-lg font-semibold">{us.name}</p>
</div>
</div>
<button
onClick={() => signOut()}
class="text-[#555] font-semibold underline"
>
Sign out
</button>
</>
)}
</Show>
</nav>
<div class="flex gap-2 items-center">
<A class="text-blue-500 font-bold underline" href="/">
Home
</A>
<A class="text-blue-500 font-bold underline" href="/protected">
Protected
</A>
</div>
</header>
);
};

export default NavBar;

export const useSession = () => {
return createServerData$(
async (_, { request }) => {
return await getSession(request, authOpts);
},
{ key: () => ["auth_user"] }
);
};
1 change: 1 addition & 0 deletions apps/dev/solid-start/src/components/NavBar/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default } from "./NavBar";
37 changes: 37 additions & 0 deletions apps/dev/solid-start/src/components/Protected/Protected.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { type Session } from "@auth/core";
import { getSession } from "@auth/solid-start";
import { Component, Show } from "solid-js";
import { useRouteData } from "solid-start";
import { createServerData$, redirect } from "solid-start/server";
import { authOpts } from "~/routes/api/auth/[...solidauth]";

const Protected = (Comp: IProtectedComponent) => {
const routeData = () => {
return createServerData$(
async (_, event) => {
const session = await getSession(event.request, authOpts);
if (!session || !session.user) {
throw redirect("/");
}
return session;
},
{ key: () => ["auth_user"] }
);
};

return {
routeData,
Page: () => {
const session = useRouteData<typeof routeData>();
return (
<Show when={session()} keyed>
{(sess) => <Comp {...sess} />}
</Show>
);
},
};
};

type IProtectedComponent = Component<Session>;

export default Protected;
1 change: 1 addition & 0 deletions apps/dev/solid-start/src/components/Protected/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default } from "./Protected";
2 changes: 2 additions & 0 deletions apps/dev/solid-start/src/components/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { default as NavBar } from "./NavBar";
export { default as Protected } from "./Protected";
3 changes: 3 additions & 0 deletions apps/dev/solid-start/src/entry-client.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { mount, StartClient } from "solid-start/entry-client";

mount(() => <StartClient />, document);
9 changes: 9 additions & 0 deletions apps/dev/solid-start/src/entry-server.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import {
StartServer,
createHandler,
renderAsync,
} from "solid-start/entry-server";

export default createHandler(
renderAsync((event) => <StartServer event={event} />)
);
24 changes: 24 additions & 0 deletions apps/dev/solid-start/src/env/client.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import type { ZodFormattedError } from "zod";
import { clientScheme } from "./schema";

export const formatErrors = (
errors: ZodFormattedError<Map<string, string>, string>
) =>
Object.entries(errors)
.map(([name, value]) => {
if (value && "_errors" in value)
return `${name}: ${value._errors.join(", ")}\n`;
})
.filter(Boolean);

const env = clientScheme.safeParse(import.meta.env);

if (env.success === false) {
console.error(
"❌ Invalid environment variables:\n",
...formatErrors(env.error.format())
);
throw new Error("Invalid environment variables");
}

export const clientEnv = env.data;
15 changes: 15 additions & 0 deletions apps/dev/solid-start/src/env/schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { z } from "zod";

export const serverScheme = z.object({
NODE_ENV: z
.enum(["development", "production", "test"])
.default("development"),
GITHUB_ID: z.string(),
GITHUB_SECRET: z.string(),
AUTH_SECRET: z.string(),
NEXTAUTH_URL: z.string().optional(),
});

export const clientScheme = z.object({
MODE: z.enum(["development", "production", "test"]).default("development"),
});
24 changes: 24 additions & 0 deletions apps/dev/solid-start/src/env/server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { serverScheme } from "./schema";
import type { ZodFormattedError } from "zod";

export const formatErrors = (
errors: ZodFormattedError<Map<string, string>, string>
) =>
Object.entries(errors)
.map(([name, value]) => {
if (value && "_errors" in value)
return `${name}: ${value._errors.join(", ")}\n`;
})
.filter(Boolean);

const env = serverScheme.safeParse(process.env);

if (env.success === false) {
console.error(
"❌ Invalid environment variables:\n",
...formatErrors(env.error.format())
);
throw new Error("Invalid environment variables");
}

export const serverEnv = env.data;
3 changes: 3 additions & 0 deletions apps/dev/solid-start/src/root.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
@tailwind base;
@tailwind components;
@tailwind utilities;
Loading