user:
+{JSON.stringify(user, null, 2)}
+ client-side data fetching with RLS
+{JSON.stringify(data, null, 2)}
+ >
+ )
+}
+
+export default LoginPage
+```
+
+## Server-side rendering (SSR)
+
+Create a server supabase client to retrieve the logged in user's session:
+
+```jsx title=pages/profile.js
+import { createServerSupabaseClient } from '@supabase/auth-helpers-nextjs'
+
+export default function Profile({ user }) {
+ return {JSON.stringify(data, null, 2)}
+ {JSON.stringify(user, null, 2)}
+ >
+ )
+}
+
+export const getServerSideProps = async (ctx) => {
+ // Create authenticated Supabase Client
+ const supabase = createServerSupabaseClient(ctx)
+ // Check if we have a session
+ const {
+ data: { session },
+ } = await supabase.auth.getSession()
+
+ if (!session)
+ return {
+ redirect: {
+ destination: '/',
+ permanent: false,
+ },
+ }
+
+ // Run queries with RLS on the server
+ const { data } = await supabase.from('users').select('*')
+
+ return {
+ props: {
+ initialSession: session,
+ user: session.user,
+ data: data ?? [],
+ },
+ }
+}
+```
+
+{JSON.stringify(data, null, 2)}
+ {JSON.stringify(user, null, 2)}
+ >
+ )
+}
+
+export const getServerSideProps = async (ctx: GetServerSidePropsContext) => {
+ // Create authenticated Supabase Client
+ const supabase = createServerSupabaseClient(ctx)
+ // Check if we have a session
+ const {
+ data: { session },
+ } = await supabase.auth.getSession()
+
+ if (!session)
+ return {
+ redirect: {
+ destination: '/',
+ permanent: false,
+ },
+ }
+
+ // Run queries with RLS on the server
+ const { data } = await supabase.from('users').select('*')
+
+ return {
+ props: {
+ initialSession: session,
+ user: session.user,
+ data: data ?? [],
+ },
+ }
+}
+```
+
+Data fetched with provider token:
+{JSON.stringify(allRepos, null, 2)}
+ user:
+{JSON.stringify(user, null, 2)}
+ >
+ )
+}
+
+export const getServerSideProps = async (ctx) => {
+ // Create authenticated Supabase Client
+ const supabase = createServerSupabaseClient(ctx)
+ // Check if we have a session
+ const {
+ data: { session },
+ } = await supabase.auth.getSession()
+
+ if (!session)
+ return {
+ redirect: {
+ destination: '/',
+ permanent: false,
+ },
+ }
+
+ // Retrieve provider_token & logged in user's third-party id from metadata
+ const { provider_token, user } = session
+ const userId = user.user_metadata.user_name
+
+ const allRepos = await (
+ await fetch(`https://api.github.com/search/repositories?q=user:${userId}`, {
+ method: 'GET',
+ headers: {
+ Authorization: `token ${provider_token}`,
+ },
+ })
+ ).json()
+
+ return { props: { user, allRepos } }
+}
+```
+
+Data fetched with provider token:
+{JSON.stringify(allRepos, null, 2)}
+ user:
+{JSON.stringify(user, null, 2)}
+ >
+ )
+}
+
+export const getServerSideProps = async (ctx: GetServerSidePropsContext) => {
+ // Create authenticated Supabase Client
+ const supabase = createServerSupabaseClient(ctx)
+ // Check if we have a session
+ const {
+ data: { session },
+ } = await supabase.auth.getSession()
+
+ if (!session)
+ return {
+ redirect: {
+ destination: '/',
+ permanent: false,
+ },
+ }
+
+ // Retrieve provider_token & logged in user's third-party id from metadata
+ const { provider_token, user } = session
+ const userId = user.user_metadata.user_name
+
+ const allRepos = await (
+ await fetch(`https://api.github.com/search/repositories?q=user:${userId}`, {
+ method: 'GET',
+ headers: {
+ Authorization: `token ${provider_token}`,
+ },
+ })
+ ).json()
+
+ return { props: { user, allRepos } }
+}
+```
+
+{JSON.stringify(user, null, 2)}
+}
+
+export const getServerSideProps = withPageAuth({ redirectTo: '/' })
+```
+
+{JSON.stringify(user, null, 2)}
+}
+
+export const getServerSideProps = async (ctx: GetServerSidePropsContext) => {
+ // Create authenticated Supabase Client
+ const supabase = createServerSupabaseClient(ctx)
+ // Check if we have a session
+ const {
+ data: { session },
+ } = await supabase.auth.getSession()
+
+ if (!session)
+ return {
+ redirect: {
+ destination: '/',
+ permanent: false,
+ },
+ }
+
+ return {
+ props: {
+ initialSession: session,
+ user: session.user,
+ },
+ }
+}
+```
+
+{JSON.stringify(data, null, 2)}
-}
-```
-
-{JSON.stringify(data, null, 2)}
-}
-```
-
-user:
-{JSON.stringify(user, null, 2)}
- client-side data fetching with RLS
-{JSON.stringify(data, null, 2)}
- >
- )
-}
-
-export default LoginPage
-```
-
-## Server-side rendering (SSR)
-
-Create a server supabase client to retrieve the logged in user's session:
-
-```jsx title=pages/profile.js
-import { createServerSupabaseClient } from '@supabase/auth-helpers-nextjs'
-
-export default function Profile({ user }) {
- return {JSON.stringify(todos, null, 2)} : Loading todos...
} ``` -## Server-side data fetching with RLS +{JSON.stringify(todos, null, 2)}
+ ) : (
+ Loading todos...
+ ); +} +``` + +> TypeScript types can be [generated with the Supabase CLI](https://supabase.com/docs/reference/javascript/typescript-support) and passed to `createClientComponentClient` to add type support to the Supabase client. + +{JSON.stringify(data, null, 2)}
- {JSON.stringify(user, null, 2)}
- >
- )
-}
-
-export const getServerSideProps = async (ctx) => {
- // Create authenticated Supabase Client
- const supabase = createServerSupabaseClient(ctx)
- // Check if we have a session
- const {
- data: { session },
- } = await supabase.auth.getSession()
-
- if (!session)
- return {
- redirect: {
- destination: '/',
- permanent: false,
- },
- }
-
- // Run queries with RLS on the server
- const { data } = await supabase.from('users').select('*')
-
- return {
- props: {
- initialSession: session,
- user: session.user,
- data: data ?? [],
- },
- }
+export default async function Home() {
+ const supabase = createServerComponentClient({ cookies })
+ const { data } = await supabase.from('todos').select()
+ return {JSON.stringify(data, null, 2)}
}
```
{JSON.stringify(data, null, 2)}
- {JSON.stringify(user, null, 2)}
- >
- )
-}
+import type { Database } from '@/lib/database.types'
-export const getServerSideProps = async (ctx: GetServerSidePropsContext) => {
- // Create authenticated Supabase Client
- const supabase = createServerSupabaseClient(ctx)
- // Check if we have a session
- const {
- data: { session },
- } = await supabase.auth.getSession()
-
- if (!session)
- return {
- redirect: {
- destination: '/',
- permanent: false,
- },
- }
-
- // Run queries with RLS on the server
- const { data } = await supabase.from('users').select('*')
-
- return {
- props: {
- initialSession: session,
- user: session.user,
- data: data ?? [],
- },
- }
+export default async function ServerComponent() {
+ const supabase = createServerComponentClient{JSON.stringify(data, null, 2)}
}
```
+> TypeScript types can be [generated with the Supabase CLI](https://supabase.com/docs/reference/javascript/typescript-support) and passed to `createServerComponentClient` to add type support to the Supabase client.
+
Data fetched with provider token:
-{JSON.stringify(allRepos, null, 2)}
- user:
-{JSON.stringify(user, null, 2)}
- >
+
)
}
-
-export const getServerSideProps = async (ctx) => {
- // Create authenticated Supabase Client
- const supabase = createServerSupabaseClient(ctx)
- // Check if we have a session
- const {
- data: { session },
- } = await supabase.auth.getSession()
-
- if (!session)
- return {
- redirect: {
- destination: '/',
- permanent: false,
- },
- }
-
- // Retrieve provider_token & logged in user's third-party id from metadata
- const { provider_token, user } = session
- const userId = user.user_metadata.user_name
-
- const allRepos = await (
- await fetch(`https://api.github.com/search/repositories?q=user:${userId}`, {
- method: 'GET',
- headers: {
- Authorization: `token ${provider_token}`,
- },
- })
- ).json()
-
- return { props: { user, allRepos } }
-}
```
Data fetched with provider token:
-{JSON.stringify(allRepos, null, 2)}
- user:
-{JSON.stringify(user, null, 2)}
- >
+
)
}
-
-export const getServerSideProps = async (ctx: GetServerSidePropsContext) => {
- // Create authenticated Supabase Client
- const supabase = createServerSupabaseClient(ctx)
- // Check if we have a session
- const {
- data: { session },
- } = await supabase.auth.getSession()
-
- if (!session)
- return {
- redirect: {
- destination: '/',
- permanent: false,
- },
- }
-
- // Retrieve provider_token & logged in user's third-party id from metadata
- const { provider_token, user } = session
- const userId = user.user_metadata.user_name
-
- const allRepos = await (
- await fetch(`https://api.github.com/search/repositories?q=user:${userId}`, {
- method: 'GET',
- headers: {
- Authorization: `token ${provider_token}`,
- },
- })
- ).json()
-
- return { props: { user, allRepos } }
-}
```
+> TypeScript types can be [generated with the Supabase CLI](https://supabase.com/docs/reference/javascript/typescript-support) and passed to `createServerActionClient` to add type support to the Supabase client.
+
{JSON.stringify(user, null, 2)}
-}
-
-export const getServerSideProps = withPageAuth({ redirectTo: '/' })
-```
-
-{JSON.stringify(user, null, 2)}
-}
-
-export const getServerSideProps = async (ctx: GetServerSidePropsContext) => {
- // Create authenticated Supabase Client
- const supabase = createServerSupabaseClient(ctx)
- // Check if we have a session
- const {
- data: { session },
- } = await supabase.auth.getSession()
-
- if (!session)
- return {
- redirect: {
- destination: '/',
- permanent: false,
- },
- }
-
- return {
- props: {
- initialSession: session,
- user: session.user,
- },
- }
+export async function POST(request) {
+ const { title } = await request.json()
+ const supabase = createRouteHandlerClient({ cookies })
+ const { data } = await supabase.from('todos').insert({ title }).select()
+ return NextResponse.json(data)
}
```