Skip to content

Commit

Permalink
chore: Further minor fixes for the docs
Browse files Browse the repository at this point in the history
  • Loading branch information
codinsonn committed Jan 12, 2024
1 parent 68457a5 commit 01af2e0
Show file tree
Hide file tree
Showing 12 changed files with 173 additions and 20 deletions.
85 changes: 85 additions & 0 deletions .storybook/disabled/@aetherspace-clerk-auth/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<a href="https://github.com/Aetherspace/green-stack-starter-demo#readme" target="_blank">
<img src="/packages/@aetherspace/assets/AetherspaceLogo.svg" width="50" height="50" />
</a>

<p>
<a href="https://clerk.com/" target="_blank">
<img alt="Clerk Auth Logo" longdesc="Cross Platform Auth with Clerk" src="https://camo.githubusercontent.com/02b3799f98edbed5bafa868dbd884287f249d3153e53c34b3c05855adf2478a2/68747470733a2f2f696d672e736869656c64732e696f2f7374617469632f76313f7374796c653d666f722d7468652d6261646765266d6573736167653d436c65726b26636f6c6f723d364334374646266c6f676f3d436c65726b266c6f676f436f6c6f723d464646464646266c6162656c3d" />
</a>
</p>

# `@aetherspace/clerk-auth` - Aetherspace Plugin

Prerequisites:
- [Sponsor Aetherspace on Github](https://github.com/Aetherspace/green-stack-starter-demo#readme) or get access to it some other way
- Fork or generate a new repository from the official or free [aetherspace/green-stack-starter](https://github.com/Aetherspace/green-stack-starter#readme) github repo
- Choose the "✅ Include all branches" option during the fork / generation process
- [Set up an account on clerk.com](https://clerk.com/)
- Add your `CLERK_SECRET_KEY` and `NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY` to `apps/next/.env`

```shell
git merge with/clerk-auth
```

<p style={{ padding: "12px" }} />

## Cross platform authentication with Clerk and Aetherspace:

After a yarn install, you'll be able to use `@aetherspace/clerk-auth` to import a bunch of handy clerk auth helpers:

```shell
│── apps/
│ └── nextjs/
│ └── middleware.ts/ # ➡️ Clerk auth middleware already set up for you
│── packages/
│ └── @aetherspace-clerk-auth/
│ └── schemas/ # ➡️ Zod schemas for clerk datastructures to re-use for types, resolvers & docs
│ └── context/ # ➡️ Universal context for clerk on web & native
│ └── hooks/ # ➡️ Universal hooks for clerk on web & native
│ └── middleware/ # ➡️ SWR middleware to ensure clerk auth headers are included on mobile
│ └── utils/ # ➡️ Utils to create auth related request context in Next.js middleware
│ └── components/ # ➡️ Handy universal components like a SignOutButton you can make your own
```

We'll also set you up with a `features/user-managment` workspace for some example integration code:

```shell
│── features/
│ └── user-managment/ # ➡️ importable from 'user-management'
│ └── schemas/ # ➡️ Zod schemas for state management & auth request context
│ └── hooks/ # ➡️ Typed form states for the clerk based auth forms
│ └── screens/ # ➡️ Example SignIn, SignUp, UserInfo screens
│ └── routes/ # ➡️ Example routes for '/sign-in', '/sign-up', '/me'
│ └── resolvers/ # ➡️ Retrieve user data from the request context
```

<p style={{ padding: "12px" }} />

## Checking the authenticated user context on the server

After signing in through our example `/sign-in` or `/sign-up` routes, you can check the user context on the data resolver context in our example `/api/context` route.

We've also provided an example `UserInfoScreen.tsx` that integrates with this `getRequestContext()` resolver and prints it out in the browser or the mobile app on the `/me` route.

This auth context will be available on all resolvers when logged in, and will be `null` or `undefined` when not authenticated. Feel free to use this context to to integrate with user data in DB queries, check for permissions or roles in your resolvers, or do stuff with e.g. the users name / email / etc.

> Do note that this is just the recommended way to get the user context **on the server**, but Clerk provides better ways to **retrieve this data on the client**:
- [Clerk Docs - useAuth()](https://clerk.com/docs/references/react/use-auth) -- importable as `useAuth` from `@aetherspace/clerk-auth/hooks`
- [Clerk Docs - useUser()](https://clerk.com/docs/references/react/use-user) -- importable as `useUser` from `@aetherspace/clerk-auth/hooks`

In general, you should **use the Clerk hooks on the client**, and the **request context in your resolvers on the server**.

<p style={{ padding: "12px" }} />

### Next.js `middleware.ts` and `createRequestContext()`

Merging this plugin will edit the `middleware.ts` file in your `apps/nextjs` workspace to include a `createRequestContext()` function. This is what will actually create the request context for you on all requests under the hood. Feel free to edit this helper function, but know that due to limitations in Next.js middleware, you can only have it include JSON serializable data. (and not e.g. Classes or functions)

You can find it at `packages/@aetherspace-clerk-auth/utils/createRequestContext.ts`

> For further reading on integrating Clerk auth with next.js middleware:
- [Clerk Docs - authMiddleware()](https://clerk.com/docs/references/nextjs/auth-middleware) -- importable as `authMiddleware` from `@clerk/nextjs`
- [Next.js Docs - Middleware](https://nextjs.org/docs/app/building-your-application/routing/middleware) -- defined at `apps/nextjs/middleware.ts`
11 changes: 11 additions & 0 deletions .storybook/disabled/@aetherspace-clerk-auth/README.stories.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { Meta } from '@storybook/addon-docs'
import StorybookLinkTransformer from '../../../packages/@aetherspace/docs/helpers/StorybookLinkTransformer'
import StorybookFontTransformer from '../../../packages/@aetherspace/docs/helpers/StorybookFontTransformer'
import ReadMeMD from './README.md'

<StorybookLinkTransformer />
<StorybookFontTransformer />

<Meta title="packages / @aetherspace-clerk-auth / README.md" />

<ReadMeMD />
7 changes: 5 additions & 2 deletions .storybook/disabled/@aetherspace-mongoose/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<img src="/packages/@aetherspace/assets/AetherspaceLogo.svg" width="50" height="50" />
<a href="https://github.com/Aetherspace/green-stack-starter-demo#readme" target="_blank">
<img src="/packages/@aetherspace/assets/AetherspaceLogo.svg" width="50" height="50" />
</a>

<p>
<a href="https://mongoosejs.com/docs/" target="_blank">
Expand All @@ -9,9 +11,10 @@
# `@aetherspace/mongoose` - Aetherspace Plugin

Prerequisites:
- [Sponsor Aetherspace on Github]() or get access to it some other way
- [Sponsor Aetherspace on Github](https://github.com/Aetherspace/green-stack-starter-demo#readme) or get access to it some other way
- Fork or generate a new repository from the official or free [aetherspace/green-stack-starter](https://github.com/Aetherspace/green-stack-starter#readme) github repo
- Choose the "✅ Include all branches" option during the fork / generation process
- Add `MONGODB_URI` to `apps/next/.env`

```shell
git merge with/mongoose
Expand Down
4 changes: 3 additions & 1 deletion .storybook/disabled/@green-stack-icons/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<img src="/packages/@aetherspace/assets/AetherspaceLogo.svg" width="50" height="50" />
<a href="https://github.com/Aetherspace/green-stack-starter-demo#readme" target="_blank">
<img src="/packages/@aetherspace/assets/AetherspaceLogo.svg" width="50" height="50" />
</a>

<p>
<a href="/?path=/docs/aetherspace-icon-management--page">
Expand Down
43 changes: 40 additions & 3 deletions packages/@aetherspace-clerk-auth/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<img src="/packages/@aetherspace/assets/AetherspaceLogo.svg" width="50" height="50" />
<a href="https://github.com/Aetherspace/green-stack-starter-demo#readme" target="_blank">
<img src="/packages/@aetherspace/assets/AetherspaceLogo.svg" width="50" height="50" />
</a>

<p>
<a href="https://clerk.com/" target="_blank">
Expand All @@ -12,11 +14,17 @@ Prerequisites:
- [Sponsor Aetherspace on Github](https://github.com/Aetherspace/green-stack-starter-demo#readme) or get access to it some other way
- Fork or generate a new repository from the official or free [aetherspace/green-stack-starter](https://github.com/Aetherspace/green-stack-starter#readme) github repo
- Choose the "✅ Include all branches" option during the fork / generation process
- [Set up an account on clerk.com](https://clerk.com/)
- Add your `CLERK_SECRET_KEY` and `NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY` to `apps/next/.env`

```shell
git merge with/clerk-auth
```

<p style={{ padding: "12px" }} />

## Cross platform authentication with Clerk and Aetherspace:

After a yarn install, you'll be able to use `@aetherspace/clerk-auth` to import a bunch of handy clerk auth helpers:

```shell
Expand All @@ -42,7 +50,36 @@ We'll also set you up with a `features/user-managment` workspace for some exampl
│ └── schemas/ # ➡️ Zod schemas for state management & auth request context
│ └── hooks/ # ➡️ Typed form states for the clerk based auth forms
│ └── screens/ # ➡️ Example SignIn, SignUp, UserInfo screens
│ └── routes/ # ➡️ Example routes for /sign-in, /sign-up, /me
│ └── routes/ # ➡️ Example routes for '/sign-in', '/sign-up', '/me'
│ └── resolvers/ # ➡️ Retrieve user data from the request context
``````
```

<p style={{ padding: "12px" }} />

## Checking the authenticated user context on the server

After signing in through our example `/sign-in` or `/sign-up` routes, you can check the user context on the data resolver context in our example `/api/context` route.

We've also provided an example `UserInfoScreen.tsx` that integrates with this `getRequestContext()` resolver and prints it out in the browser or the mobile app on the `/me` route.

This auth context will be available on all resolvers when logged in, and will be `null` or `undefined` when not authenticated. Feel free to use this context to to integrate with user data in DB queries, check for permissions or roles in your resolvers, or do stuff with e.g. the users name / email / etc.

> Do note that this is just the recommended way to get the user context **on the server**, but Clerk provides better ways to **retrieve this data on the client**:
- [Clerk Docs - useAuth()](https://clerk.com/docs/references/react/use-auth) -- importable as `useAuth` from `@aetherspace/clerk-auth/hooks`
- [Clerk Docs - useUser()](https://clerk.com/docs/references/react/use-user) -- importable as `useUser` from `@aetherspace/clerk-auth/hooks`

In general, you should **use the Clerk hooks on the client**, and the **request context in your resolvers on the server**.

<p style={{ padding: "12px" }} />

### Next.js `middleware.ts` and `createRequestContext()`

Merging this plugin will edit the `middleware.ts` file in your `apps/nextjs` workspace to include a `createRequestContext()` function. This is what will actually create the request context for you on all requests under the hood. Feel free to edit this helper function, but know that due to limitations in Next.js middleware, you can only have it include JSON serializable data. (and not e.g. Classes or functions)

You can find it at `packages/@aetherspace-clerk-auth/utils/createRequestContext.ts`

> For further reading on integrating Clerk auth with next.js middleware:
- [Clerk Docs - authMiddleware()](https://clerk.com/docs/references/nextjs/auth-middleware) -- importable as `authMiddleware` from `@clerk/nextjs`
- [Next.js Docs - Middleware](https://nextjs.org/docs/app/building-your-application/routing/middleware) -- defined at `apps/nextjs/middleware.ts`
5 changes: 4 additions & 1 deletion packages/@aetherspace-mongoose/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<img src="/packages/@aetherspace/assets/AetherspaceLogo.svg" width="50" height="50" />
<a href="https://github.com/Aetherspace/green-stack-starter-demo#readme" target="_blank">
<img src="/packages/@aetherspace/assets/AetherspaceLogo.svg" width="50" height="50" />
</a>

<p>
<a href="https://mongoosejs.com/docs/" target="_blank">
Expand All @@ -12,6 +14,7 @@ Prerequisites:
- [Sponsor Aetherspace on Github](https://github.com/Aetherspace/green-stack-starter-demo#readme) or get access to it some other way
- Fork or generate a new repository from the official or free [aetherspace/green-stack-starter](https://github.com/Aetherspace/green-stack-starter#readme) github repo
- Choose the "✅ Include all branches" option during the fork / generation process
- Add `MONGODB_URI` to `apps/next/.env`

```shell
git merge with/mongoose
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,11 @@ type AetherSchemaInput<Z extends z.ZodRawShape> = z.ZodObject<Z>

/** --- createMongooseDataModel() ----------------------------------------------------------------- */
/** -i- Turn an aetherSchema with fields defined with zod into a usable mongoose model */
export const createMongooseDataModel = <Z extends z.ZodRawShape>(
export const createMongooseDataModel = <
Z extends z.ZodRawShape,
SchemaDoc = Document & z.infer<AetherSchemaInput<Z>>,
SchemaModel = Model<SchemaDoc> & { aetherSchema: AetherSchemaInput<Z> },
>(
schema: AetherSchemaInput<Z>,
schemaOptions: SchemaOptions = {}
) => {
Expand All @@ -87,13 +91,10 @@ export const createMongooseDataModel = <Z extends z.ZodRawShape>(
throw new Error('createMongooseDataModel() requires a named aetherSchema (did you pass a regular zod object instead?)') // prettier-ignore
}

// Define resulting Interface
type SchemaDoc = Document & z.infer<AetherSchemaInput<Z>>
type SchemaModel = Model<SchemaDoc> & { aetherSchema: typeof schema }

// Check for existing model before creating a new one
const existingModel = mongoose.models[schema.schemaName] as SchemaModel
if (existingModel) {
// @ts-ignore
existingModel.aetherSchema = schema
return existingModel
}
Expand Down Expand Up @@ -141,6 +142,7 @@ export const createMongooseDataModel = <Z extends z.ZodRawShape>(
const schemaModel = model<SchemaDoc>(schema.schemaName, mongooseSchema) as SchemaModel

// Attach schema to model
// @ts-ignore
schemaModel.aetherSchema = schema

// Return model
Expand Down
6 changes: 4 additions & 2 deletions packages/@aetherspace/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<img src="/packages/@aetherspace/assets/AetherspaceLogo.svg" width="50" height="50" />
<a href="https://github.com/Aetherspace/green-stack-starter-demo#readme" target="_blank">
<img src="/packages/@aetherspace/assets/AetherspaceLogo.svg" width="50" height="50" />
</a>

# GREEN stack quickstart ⚡

Expand All @@ -20,8 +22,8 @@ It comes out of the box with setup for:
- Next.js web app (File based app dir routing, Serverside rendering, Static site generation, ...)
- Expo mobile app (Android & iOS with Expo-Router and react-native)
- A REST & GraphQL API (with Apollo Server & Next.js API routes)
- Documentation for Aetherspace and its components + docgen as a side effect of our [recommended way of working](/packages/@aetherspace/scripts/README.md)
- [Generators and Automation scripts](/packages/@aetherspace/scripts/README.md) to automatically generate API & component documentation
- Documentation for Aetherspace and its components (with docgen being a side effect of our [recommended way of working](/packages/@aetherspace/scripts/README.md))
- Github actions for mobile deployments, linting your code & building your documentation

**When you're ready to start developing, run `yarn install` to install all dependencies, followed by:**
Expand Down
8 changes: 5 additions & 3 deletions packages/@aetherspace/core/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<img src="/packages/@aetherspace/assets/AetherspaceLogo.svg" width="50" height="50" />
<a href="https://github.com/Aetherspace/green-stack-starter-demo#readme" target="_blank">
<img src="/packages/@aetherspace/assets/AetherspaceLogo.svg" width="50" height="50" />
</a>

# Aetherspace Core Concepts

Expand Down Expand Up @@ -206,9 +208,9 @@ Similar to adding recurring features, removing features or packages from a fresh

---

I firmly believe the opinionated toolbelt and core-concepts provided by the template repo will bring major benefits in terms of speed and efficiency.
I firmly believe the opinionated toolbelt and core-concepts will bring major benefits in terms of speed and efficiency.

However, **if you wish, you can actually ignore most of these core-concepts** Aetherspace promotes, and still benefit from (only) the universal setup.
However, **if you wish, you can actually ignore most of these core-concepts**, and still benefit from (only) the universal setup.

For example:
- You can avoid using `SWR`, `@expo/html-elements` or even `Zod` schemas yourself, even when keeping automations
Expand Down
4 changes: 3 additions & 1 deletion packages/@aetherspace/schemas/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<img src="/packages/@aetherspace/assets/AetherspaceLogo.svg" width="50" height="50" />
<a href="https://github.com/Aetherspace/green-stack-starter-demo#readme" target="_blank">
<img src="/packages/@aetherspace/assets/AetherspaceLogo.svg" width="50" height="50" />
</a>

# Schemas as Single Sources of Truth in Aetherspace

Expand Down
4 changes: 3 additions & 1 deletion packages/@aetherspace/scripts/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<img src="/packages/@aetherspace/assets/AetherspaceLogo.svg" width="50" height="50" />
<a href="https://github.com/Aetherspace/green-stack-starter-demo#readme" target="_blank">
<img src="/packages/@aetherspace/assets/AetherspaceLogo.svg" width="50" height="50" />
</a>

# Aetherspace's Recommended Way of Working

Expand Down
4 changes: 3 additions & 1 deletion packages/@green-stack-icons/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<img src="/packages/@aetherspace/assets/AetherspaceLogo.svg" width="50" height="50" />
<a href="https://github.com/Aetherspace/green-stack-starter-demo#readme" target="_blank">
<img src="/packages/@aetherspace/assets/AetherspaceLogo.svg" width="50" height="50" />
</a>

<p>
<a href="/?path=/docs/aetherspace-icon-management--page">
Expand Down

1 comment on commit 01af2e0

@vercel
Copy link

@vercel vercel bot commented on 01af2e0 Jan 12, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.