The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.
TypeScript aialternativeauthdatabasedenoembeddingsexamplefirebasenextjsoauth2pgvectorpostgispostgrespostgresqlpostgrestrealtimesupabasevectorswebsockets
Prettified and added useEffect for onAuthStateChange
meera-dateyae2caf50e22d
diff --git a/examples/realtime/nextjs-auth-presence/README.md b/examples/realtime/nextjs-auth-presence/README.mdindex 5ee9ad1e426..7949860a68a 100644--- a/examples/realtime/nextjs-auth-presence/README.md+++ b/examples/realtime/nextjs-auth-presence/README.md@@ -28,7 +28,8 @@ Go to the Project Settings (the cog icon), open the API tab, and find your API UThe `anon` key is your client-side API key. It allows "anonymous access" to your database, until the user has logged in. Once they have logged in, the keys will switch to the user's own login token.-++### 4. Pull this example git repository@@ -39,8 +40,8 @@ The `anon` key is your client-side API key. It allows "anonymous access" to yourCreate a .env.local file and add following environment variables.- ```- NEXT_PUBLIC_SUPABASE_URL=<<insert-your-db-url-here>>+```+NEXT_PUBLIC_SUPABASE_URL=<<insert-your-db-url-here>>NEXT_PUBLIC_SUPABASE_ANON_KEY=<<insert-your-anon-key-here>>```diff --git a/examples/realtime/nextjs-auth-presence/package.json b/examples/realtime/nextjs-auth-presence/package.jsonindex 4a76c6439b9..0f63cf9d4cb 100644--- a/examples/realtime/nextjs-auth-presence/package.json+++ b/examples/realtime/nextjs-auth-presence/package.json@@ -6,7 +6,8 @@"dev": "next dev","build": "next build","start": "next start",- "lint": "next lint"+ "lint": "next lint",+ "format": "prettier --write \"pages/**/*.{js,jsx,ts,tsx,css,md,json}\""},"dependencies": {"@supabase/auth-helpers-nextjs": "^0.4.1",diff --git a/examples/realtime/nextjs-auth-presence/pages/_app.tsx b/examples/realtime/nextjs-auth-presence/pages/_app.tsxindex 2458f4b6351..a940a7c6d40 100644--- a/examples/realtime/nextjs-auth-presence/pages/_app.tsx+++ b/examples/realtime/nextjs-auth-presence/pages/_app.tsx@@ -1,39 +1,44 @@import '../styles/globals.css'-import { useState } from 'react';+import { useState, useEffect } from 'react'import type { AppProps } from 'next/app'-import { useRouter } from 'next/router';-import { createBrowserSupabaseClient, Session } from "@supabase/auth-helpers-nextjs";-import { SessionContextProvider } from "@supabase/auth-helpers-react"+import { useRouter } from 'next/router'+import { createBrowserSupabaseClient, Session } from '@supabase/auth-helpers-nextjs'+import { SessionContextProvider } from '@supabase/auth-helpers-react'-function MyApp({ Component, pageProps }: AppProps<{- initialSession: Session;+function MyApp({+ Component,+ pageProps,+}: AppProps<{+ initialSession: Session}>) {- const router = useRouter();- const [supabaseClient] = useState(() => createBrowserSupabaseClient());+ const router = useRouter()+ const [supabaseClient] = useState(() => createBrowserSupabaseClient())- supabaseClient.auth.onAuthStateChange((event, session) => {- switch( event)- { case 'SIGNED_IN':- router.push('/');- return;+ useEffect(() => {+ const {+ data: { subscription },+ } = supabaseClient.auth.onAuthStateChange((event, session) => {+ switch (event) {+ case 'SIGNED_IN':+ router.push('/')+ returncase 'SIGNED_OUT':- router.push('/login');- return;+ router.push('/login')+ return}- })+ });+ return subscription.unsubscribe;++ }, []);return (<SessionContextProvidersupabaseClient={supabaseClient}initialSession={pageProps.initialSession}>----<Component {...pageProps} /></SessionContextProvider>- );+ )}export default MyAppdiff --git a/examples/realtime/nextjs-auth-presence/pages/index.tsx b/examples/realtime/nextjs-auth-presence/pages/index.tsxindex e14b8306f7f..03006bd1bc5 100644--- a/examples/realtime/nextjs-auth-presence/pages/index.tsx+++ b/examples/realtime/nextjs-auth-presence/pages/index.tsx@@ -12,9 +12,8 @@ const HomePage: NextPage = () => {const [userState, setUserState] = useState<RealtimePresenceState>({})useEffect(() => {- console.log('user : ', this_user);+ console.log('user : ', this_user)-const channel = supabaseClient.channel('online-users', {config: {presence: {@@ -28,24 +27,21 @@ const HomePage: NextPage = () => {console.log(' inside presence ', presentState)- setUserState({...presentState});-+ setUserState({ ...presentState })})channel.on('presence', { event: 'join' }, ({ newPresences }) => {console.log('New users have joined: ', newPresences)})- channel.subscribe(- async (status) => {+ channel.subscribe(async (status) => {if (status === 'SUBSCRIBED') {const status = await channel.track({user_name: this_user?.email ? this_user?.email : 'Unknown',})console.log(status)}- }- )+ })}, [])return (<>diff --git a/examples/realtime/nextjs-auth-presence/pages/login.tsx b/examples/realtime/nextjs-auth-presence/pages/login.tsxindex 874f3421b1b..b570b97c2a6 100644--- a/examples/realtime/nextjs-auth-presence/pages/login.tsx+++ b/examples/realtime/nextjs-auth-presence/pages/login.tsx@@ -2,38 +2,36 @@ import type { NextPage } from 'next'import Head from 'next/head'import Image from 'next/image'import styles from '../styles/Home.module.css'-import {- useUser,- useSupabaseClient,-} from '@supabase/auth-helpers-react';-import { Auth, ThemeSupa} from '@supabase/auth-ui-react';-import Link from 'next/link';+import { useUser, useSupabaseClient } from '@supabase/auth-helpers-react'+import { Auth, ThemeSupa } from '@supabase/auth-ui-react'+import Link from 'next/link'import { useEffect, useState } from 'react'import { createBrowserSupabaseClient } from '@supabase/auth-helpers-nextjs'const LoginPage: NextPage = () => {- const supabaseClient = useSupabaseClient();- const user = useUser();-- if (!user){- return (- <main className={styles.main}>- <Auth- redirectTo="http://localhost:3000/"- appearance={{ theme: ThemeSupa }}- supabaseClient={supabaseClient}- />- </main>- )}+ const supabaseClient = useSupabaseClient()+ const user = useUser()++ if (!user) {+ return (+ <main className={styles.main}>+ <Auth+ redirectTo="http://localhost:3000/"+ appearance={{ theme: ThemeSupa }}+ supabaseClient={supabaseClient}+ />+ </main>+ )+ }-return (- <>- <button onClick={() => supabaseClient.auth.signOut()}>Sign out</button>- <p>user:</p>- <pre>{JSON.stringify(user, null, 2)}</pre>- </>-)+ return (+ <>+ <button onClick={() => supabaseClient.auth.signOut()}>Sign out</button>+ <p>user:</p>+ <pre>{JSON.stringify(user, null, 2)}</pre>+ </>+ )return <h1> Hi</h1>}diff --git a/examples/realtime/nextjs-auth-presence/pages/profile.tsx b/examples/realtime/nextjs-auth-presence/pages/profile.tsxindex ebdd6b86231..60a6e7ee420 100644--- a/examples/realtime/nextjs-auth-presence/pages/profile.tsx+++ b/examples/realtime/nextjs-auth-presence/pages/profile.tsx@@ -1,18 +1,17 @@// pages/profile.js-import { withPageAuth, User } from '@supabase/auth-helpers-nextjs';-import Link from 'next/link';+import { withPageAuth, User } from '@supabase/auth-helpers-nextjs'+import Link from 'next/link'export default function Profile({ user }: { user: User }) {return (<><p>- [<Link href="/">Home</Link>] | [- <Link href="/protected-page">supabaseServerClient</Link>]+ [<Link href="/">Home</Link>] | [<Link href="/protected-page">supabaseServerClient</Link>]</p><div>Hello {user.email}</div><pre>{JSON.stringify(user, null, 2)}</pre></>- );+ )}-export const getServerSideProps = withPageAuth({ redirectTo: '/' });\ No newline at end of file+export const getServerSideProps = withPageAuth({ redirectTo: '/' })diff --git a/examples/realtime/nextjs-auth-presence/supabase_anon_key.jpg b/examples/realtime/nextjs-auth-presence/supabase_anon_key.jpgnew file mode 100644index 00000000000..17b3cbd5f93Binary files /dev/null and b/examples/realtime/nextjs-auth-presence/supabase_anon_key.jpg differ