forked from nhost/nhost
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Sync Fork
committed
Sep 30, 2023
1 parent
326143b
commit f498a3d
Showing
191 changed files
with
3,711 additions
and
505 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 2 additions & 0 deletions
2
examples/quickstarts/nextjs-server-components/.env.development
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
NEXT_PUBLIC_NHOST_SUBDOMAIN=local | ||
NEXT_PUBLIC_NHOST_REGION= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
module.exports = { | ||
extends: ['../../config/.eslintrc.js', 'plugin:@next/next/recommended'], | ||
rules: { | ||
'react/react-in-jsx-scope': 'off' | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files. | ||
|
||
# dependencies | ||
/node_modules | ||
/.pnp | ||
.pnp.js | ||
|
||
# testing | ||
/coverage | ||
|
||
# next.js | ||
/.next/ | ||
/out/ | ||
|
||
# production | ||
/build | ||
|
||
# misc | ||
.DS_Store | ||
*.pem | ||
|
||
# debug | ||
npm-debug.log* | ||
yarn-debug.log* | ||
yarn-error.log* | ||
|
||
# local env files | ||
.env*.local | ||
|
||
# vercel | ||
.vercel | ||
|
||
# typescript | ||
*.tsbuildinfo | ||
next-env.d.ts |
11 changes: 11 additions & 0 deletions
11
examples/quickstarts/nextjs-server-components/CHANGELOG.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# @nhost-examples/nextjs-server-components | ||
|
||
## 0.1.0 | ||
|
||
### Minor Changes | ||
|
||
- 4fe4a1696: new quickstart project that demonstrates how to use the Nhost SDK with Next.js 13 server components | ||
|
||
### Patch Changes | ||
|
||
- @nhost/nhost-js@2.2.17 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
# Nhost with Next.js Server Components | ||
|
||
This quickstart showcases how to correctly add authentication to a Next.js 13 project using the new App Router and Server Components. The other parts of the SDK (Storage / GraphQL/ Functions) should work the same as before. | ||
|
||
## Authentication | ||
|
||
1. **Saving the auth session** | ||
|
||
To enable authentication with Server Components we have to store the auth session in a cookie. This should be done right after any **signIn** or **signUp** operation. See example [here](https://github.com/nhost/nhost/blob/main/examples/quickstarts/nextjs-server-components/src/app/server-actions/auth/sign-in-email-password.ts). | ||
|
||
2. **Oauth & refresh session middleware** | ||
|
||
Create a middleware at the root of your project that calls the helper method `manageAuthSession`. Feel free to copy paste the the contents of the `/utils` folder to your project. The second argument for `manageAuthSession` is for handling the case where there's an error refreshing the current session with the `refreshToken` stored in the cookie. | ||
|
||
```typescript | ||
import { manageAuthSession } from '@utils/nhost' | ||
import { NextRequest, NextResponse } from 'next/server' | ||
|
||
export async function middleware(request: NextRequest) { | ||
return manageAuthSession(request, () => | ||
NextResponse.redirect(new URL('/auth/sign-in', request.url)) | ||
) | ||
} | ||
``` | ||
|
||
3. **Protected routes** | ||
|
||
To make sure only authenticated users access some Server Components, wrap them in the Higher Order Server Component `withAuthAsync`. | ||
|
||
```typescript | ||
import withAuthAsync from '@utils/auth-guard' | ||
|
||
const MyProtectedServerComponent = async () => { | ||
return <h2>Protected</h2> | ||
} | ||
|
||
export default withAuthAsync(MyProtectedServerComponent) | ||
``` | ||
|
||
## Get Started | ||
|
||
1. Clone the repository | ||
|
||
```sh | ||
git clone https://github.com/nhost/nhost | ||
cd nhost | ||
``` | ||
|
||
2. Install and build dependencies | ||
|
||
```sh | ||
pnpm install | ||
pnpm build | ||
``` | ||
|
||
3. Terminal 1: Start the Nhost Backend | ||
|
||
> Make sure you have the [Nhost CLI installed](https://docs.nhost.io/platform/cli). | ||
```sh | ||
cd examples/quickstarts/nhost-backend | ||
nhost up | ||
``` | ||
|
||
4. Terminal 2: Start the Next.js application | ||
|
||
```sh | ||
cd examples/quickstarts/nextjs-server-components | ||
pnpm dev | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
/** @type {import('next').NextConfig} */ | ||
const nextConfig = { | ||
experimental: { | ||
serverActions: true | ||
} | ||
} | ||
|
||
module.exports = nextConfig |
35 changes: 35 additions & 0 deletions
35
examples/quickstarts/nextjs-server-components/package.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
{ | ||
"name": "@nhost-examples/nextjs-server-components", | ||
"version": "0.1.0", | ||
"private": true, | ||
"scripts": { | ||
"dev": "next dev", | ||
"build": "next build", | ||
"start": "next start", | ||
"lint": "next lint" | ||
}, | ||
"dependencies": { | ||
"@apollo/client": "^3.8.2", | ||
"@nhost/nhost-js": "workspace:^", | ||
"autoprefixer": "10.4.15", | ||
"cookies-next": "^3.0.0", | ||
"eslint": "8.48.0", | ||
"eslint-config-next": "13.4.19", | ||
"form-data": "^4.0.0", | ||
"js-cookie": "^3.0.5", | ||
"next": "13.4.19", | ||
"postcss": "8.4.29", | ||
"react": "18.2.0", | ||
"react-dom": "18.2.0", | ||
"tailwind-merge": "^1.8.0", | ||
"tailwindcss": "3.3.3", | ||
"typescript": "5.2.2", | ||
"xstate": "^4.38.2" | ||
}, | ||
"devDependencies": { | ||
"@types/js-cookie": "^3.0.2", | ||
"@types/node": "20.5.6", | ||
"@types/react": "18.2.21", | ||
"@types/react-dom": "18.2.7" | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
examples/quickstarts/nextjs-server-components/postcss.config.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
module.exports = { | ||
plugins: { | ||
tailwindcss: {}, | ||
autoprefixer: {}, | ||
}, | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions
1
examples/quickstarts/nextjs-server-components/public/vercel.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
36 changes: 36 additions & 0 deletions
36
examples/quickstarts/nextjs-server-components/src/app/auth/sign-in/email-password/page.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
'use client' | ||
|
||
import Input from '@components/input' | ||
import SubmitButton from '@components/submit-button' | ||
import { signIn } from '@server-actions/auth' | ||
import { useState } from 'react' | ||
|
||
export default function SignInWithEmailAndPassword() { | ||
const [error, setError] = useState('') | ||
|
||
async function handleSignIn(formData: FormData) { | ||
const response = await signIn(formData) | ||
|
||
if (response?.error) { | ||
setError(response.error) | ||
} | ||
} | ||
|
||
return ( | ||
<div className="flex flex-col items-center"> | ||
<h1 className="text-2xl font-semibold text-center">Sign in with email and password</h1> | ||
|
||
{error && <p className="mt-3 font-semibold text-center text-red-500">{error}</p>} | ||
|
||
<form className="w-full max-w-lg space-y-5" action={handleSignIn}> | ||
<Input label="Email" id="email" name="email" type="email" required /> | ||
|
||
<Input label="Password" id="password" name="password" type="password" required /> | ||
|
||
<SubmitButton type="submit" className="w-full"> | ||
Sign in | ||
</SubmitButton> | ||
</form> | ||
</div> | ||
) | ||
} |
Oops, something went wrong.