Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

chore: update with-nextjs guide

Alaister Young583373208abe
diff --git a/apps/reference/docs/guides/with-nextjs.mdx b/apps/reference/docs/guides/with-nextjs.mdx
index 0f82541674a..66d8e6f0dd9 100644
--- a/apps/reference/docs/guides/with-nextjs.mdx
+++ b/apps/reference/docs/guides/with-nextjs.mdx
@@ -214,7 +214,7 @@ export default function Auth() {
const handleLogin = async (email) => {
try {
setLoading(true)
- const { error } = await supabase.auth.signIn({ email })
+ const { error } = await supabase.auth.signInWithOtp({ email })
if (error) throw error
alert('Check your email for the login link!')
} catch (error) {
@@ -278,10 +278,23 @@ export default function Account({ session }) {
getProfile()
}, [session])
+ async function getCurrentUser() {
+ const {
+ data: { session },
+ error,
+ } = await supabase.auth.getSession()
+
+ if (error) {
+ throw error
+ }
+
+ return session.user
+ }
+
async function getProfile() {
try {
setLoading(true)
- const user = supabase.auth.user()
+ const user = await getCurrentUser()
let { data, error, status } = await supabase
.from('profiles')
@@ -308,7 +321,7 @@ export default function Account({ session }) {
async function updateProfile({ username, website, avatar_url }) {
try {
setLoading(true)
- const user = supabase.auth.user()
+ const user = await getCurrentUser()
const updates = {
id: user.id,
@@ -318,9 +331,7 @@ export default function Account({ session }) {
updated_at: new Date(),
}
- let { error } = await supabase.from('profiles').upsert(updates, {
- returning: 'minimal', // Don't return the value after inserting
- })
+ let { error } = await supabase.from('profiles').upsert(updates)
if (error) {
throw error
@@ -391,14 +402,40 @@ import Auth from '../components/Auth'
import Account from '../components/Account'
export default function Home() {
+ const [isLoading, setIsLoading] = useState(true)
const [session, setSession] = useState(null)
useEffect(() => {
- setSession(supabase.auth.session())
+ let mounted = true
+
+ async function getInitialSession() {
+ const {
+ data: { session },
+ } = await supabase.auth.getSession()
- supabase.auth.onAuthStateChange((_event, session) => {
- setSession(session)
- })
+ // only update the react state if the component is still mounted
+ if (mounted) {
+ if (session) {
+ setSession(session)
+ }
+
+ setIsLoading(false)
+ }
+ }
+
+ getInitialSession()
+
+ const { subscription } = supabase.auth.onAuthStateChange(
+ (_event, session) => {
+ setSession(session)
+ }
+ )
+
+ return () => {
+ mounted = false
+
+ subscription?.unsubscribe()
+ }
}, [])
return (