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

examples: update with-supabase example to App Router #51335

Merged
merged 14 commits into from
Jun 16, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ To see examples with other authentication providers, check out the [examples fol
- [Magic](https://github.com/vercel/next.js/tree/canary/examples/with-magic)
- [Nhost](https://github.com/vercel/next.js/tree/canary/examples/with-nhost-auth-realtime-graphql)
- [Ory](https://github.com/vercel/examples/tree/main/solutions/auth-with-ory)
- [Supabase](https://github.com/vercel/next.js/tree/canary/examples/with-supabase-auth-realtime-db)
- [Supabase](https://github.com/vercel/next.js/tree/canary/examples/with-supabase)
- [Supertokens](https://github.com/vercel/next.js/tree/canary/examples/with-supertokens)
- [Userbase](https://github.com/vercel/next.js/tree/canary/examples/with-userbase)

Expand Down
3 changes: 0 additions & 3 deletions examples/with-supabase-auth-realtime-db/.env.local.example

This file was deleted.

29 changes: 0 additions & 29 deletions examples/with-supabase-auth-realtime-db/README.md

This file was deleted.

6 changes: 0 additions & 6 deletions examples/with-supabase-auth-realtime-db/lib/initSupabase.js

This file was deleted.

16 changes: 0 additions & 16 deletions examples/with-supabase-auth-realtime-db/package.json

This file was deleted.

13 changes: 0 additions & 13 deletions examples/with-supabase-auth-realtime-db/pages/_app.js

This file was deleted.

8 changes: 0 additions & 8 deletions examples/with-supabase-auth-realtime-db/pages/api/auth.js

This file was deleted.

13 changes: 0 additions & 13 deletions examples/with-supabase-auth-realtime-db/pages/api/getUser.js

This file was deleted.

119 changes: 0 additions & 119 deletions examples/with-supabase-auth-realtime-db/pages/index.js

This file was deleted.

39 changes: 0 additions & 39 deletions examples/with-supabase-auth-realtime-db/pages/profile.js

This file was deleted.

4 changes: 0 additions & 4 deletions examples/with-supabase-auth-realtime-db/style.css

This file was deleted.

4 changes: 4 additions & 0 deletions examples/with-supabase/.env.local.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Update these with your Supabase details from your project settings > API
# https://app.supabase.com/project/_/settings/api
NEXT_PUBLIC_SUPABASE_URL=your-project-url
NEXT_PUBLIC_SUPABASE_ANON_KEY=your-anon-key
3 changes: 3 additions & 0 deletions examples/with-supabase/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "next/core-web-vitals"
}
28 changes: 28 additions & 0 deletions examples/with-supabase/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Supabase Starter

This starter configures Supabase Auth to use cookies, making the user's session available throughout the entire Next.js app - Client Components, Server Components, Route Handlers, Server Actions and Middleware.

## Deploy with Vercel

The Vercel deployment will guide you through creating a Supabase account and project. After installation of the Supabase integration, all relevant environment variables will be set up so that the project is usable immediately after deployment 🚀

[![Deploy with Vercel](https://vercel.com/button)](https://vercel.com/new/clone?repository-url=https://github.com/vercel/next.js/tree/canary/examples/with-supabase&project-name=nextjs-with-supabase&repository-name=nextjs-with-supabase&integration-ids=oac_jUduyjQgOyzev1fjrW83NYOv)

## Running Locally

1. `cd` into this directory
1. Run `npm install` to install dependencies
1. Create a [new Supabase project](https://database.new)
1. Rename `.env.local.example` to `.env.local` and update the values for `NEXT_PUBLIC_SUPABASE_URL` and `NEXT_PUBLIC_SUPABASE_ANON_KEY` from [your Supabase project's API settings](https://app.supabase.com/project/_/settings/api)
1. Run `npm run dev` to start the local development server

## Feedback and issues

Please file feedback and issues over on the [Supabase GitHub org](https://github.com/supabase/supabase/issues/new/choose).

## More Supabase examples

- [Next.js Subscription Payments Starter](https://github.com/vercel/nextjs-subscription-payments)
- [Cookie-based Auth and the Next.js 13 App Router (free course)](https://youtube.com/playlist?list=PL5S4mPUpp4OtMhpnp93EFSo42iQ40XjbF)
- [Supabase Auth and the Next.js App Router](https://github.com/supabase/supabase/tree/master/examples/auth/nextjs)
- [Next.js Auth Helpers Docs](https://supabase.com/docs/guides/auth/auth-helpers/nextjs)
19 changes: 19 additions & 0 deletions examples/with-supabase/app/auth/callback/route.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { createRouteHandlerClient } from '@supabase/auth-helpers-nextjs'
import { cookies } from 'next/headers'
import { NextResponse } from 'next/server'

export async function GET(request) {
// The `/auth/callback` route is required for the server-side auth flow implemented
// by the Auth Helpers package. It exchanges an auth code for the user's session.
// https://supabase.com/docs/guides/auth/auth-helpers/nextjs#managing-sign-in-with-code-exchange
const requestUrl = new URL(request.url)
const code = requestUrl.searchParams.get('code')

if (code) {
const supabase = createRouteHandlerClient({ cookies })
await supabase.auth.exchangeCodeForSession(code)
}

// URL to redirect to after sign in process completes
return NextResponse.redirect(requestUrl.origin)
}
25 changes: 25 additions & 0 deletions examples/with-supabase/app/client-component-example/page.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
'use client'

import { createClientComponentClient } from '@supabase/auth-helpers-nextjs'
import { useEffect } from 'react'

export default function ClientComponent() {
const [todos, setTodos] = useState([])

// create a Supabase client configured to use cookies
const supabase = createClientComponentClient()

useEffect(() => {
const getTodos = async () => {
// this assumes you have a `todos` table
// head over to your Supabase project's Table Editor to create a table
// https://app.supabase.com/project/_/editor
const { data } = await supabase.from('todos').select()
setTodos(data)
}

getTodos()
}, [supabase, setTodos])

return <pre>{JSON.stringify(todos, null, 2)}</pre>
}
Binary file added examples/with-supabase/app/favicon.ico
Binary file not shown.
27 changes: 27 additions & 0 deletions examples/with-supabase/app/globals.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
@tailwind base;
@tailwind components;
@tailwind utilities;

:root {
--foreground-rgb: 0, 0, 0;
--background-start-rgb: 214, 219, 220;
--background-end-rgb: 255, 255, 255;
}

@media (prefers-color-scheme: dark) {
:root {
--foreground-rgb: 255, 255, 255;
--background-start-rgb: 0, 0, 0;
--background-end-rgb: 0, 0, 0;
}
}

body {
color: rgb(var(--foreground-rgb));
background: linear-gradient(
to bottom,
transparent,
rgb(var(--background-end-rgb))
)
rgb(var(--background-start-rgb));
}