Files
supabase/apps/docs/lib/userAuth.ts
T
CharisandJoshen Lim 0e8ee2e890 feat: display api urls and keys instead of placeholders in docs (#19672)
**feat:** adds a component to display API endpoints and anon keys in docs if user is authenticated
makes the getting started UX smoother so users don't have to jump back and forth between docs and trying to find their keys in dashboard

**out of scope:** it would be very cool to display these directly in the code blocks. I looked into it and it would require quite a bit of work forking and modifying code-hike, so out of scope for now

Co-authored-by: Joshen Lim <joshenlimek@gmail.com>
2024-01-09 15:00:31 -05:00

37 lines
676 B
TypeScript

import { gotrueClient } from 'common'
import { useEffect } from 'react'
export const auth = gotrueClient
export async function getAccessToken() {
// ignore if server-side
if (typeof window === 'undefined') {
return
}
const {
data: { session },
error,
} = await auth.getSession()
if (error) {
throw error
}
return session?.access_token
}
export function useOnLogout(callback: () => void) {
useEffect(() => {
const {
data: {
subscription: { unsubscribe },
},
} = auth.onAuthStateChange((event) => {
if (event === 'SIGNED_OUT') {
callback()
}
})
return unsubscribe
}, [callback])
}