mirror of
https://github.com/supabase/supabase.git
synced 2026-10-08 19:05:06 +03:00
* wip * wip * wip * remove seeds * wip * add branching memento * remove triple dots * proofread * chore: mention direct secret usage * chore: update README.md * chore: update docs * fix: .env.local to .env.developemnt * chore: fix typo * chore: fix commands * chore: update README.md * chore: apply PR comments * chore: rename .env.local to .env * chore: simplify dotenvx setup (#32824) * chore: bump cli * chore: disable unused seed --------- Co-authored-by: Han Qiao <sweatybridge@gmail.com> Co-authored-by: Qiao Han <qiao@supabase.io>
64 lines
1.6 KiB
JavaScript
64 lines
1.6 KiB
JavaScript
import '~/styles/style.scss'
|
|
import { useState, useEffect } from 'react'
|
|
import { useRouter } from 'next/router'
|
|
import UserContext from 'lib/UserContext'
|
|
import { supabase } from 'lib/Store'
|
|
import { jwtDecode } from 'jwt-decode'
|
|
|
|
export default function SupabaseSlackClone({ Component, pageProps }) {
|
|
const [userLoaded, setUserLoaded] = useState(false)
|
|
const [user, setUser] = useState(null)
|
|
const [, setSession] = useState(null)
|
|
const router = useRouter()
|
|
|
|
useEffect(() => {
|
|
function saveSession(
|
|
/** @type {Awaited<ReturnType<typeof supabase.auth.getSession>>['data']['session']} */
|
|
session
|
|
) {
|
|
setSession(session)
|
|
const currentUser = session?.user
|
|
if (session) {
|
|
const jwt = jwtDecode(session.access_token)
|
|
currentUser.appRole = jwt.user_role
|
|
}
|
|
setUser(currentUser ?? null)
|
|
setUserLoaded(!!currentUser)
|
|
if (currentUser) {
|
|
router.push('/channels/[id]', '/channels/1')
|
|
}
|
|
}
|
|
|
|
supabase.auth.getSession().then(({ data: { session } }) => saveSession(session))
|
|
|
|
const {
|
|
data: { subscription: authListener },
|
|
} = supabase.auth.onAuthStateChange(async (_event, session) => {
|
|
saveSession(session)
|
|
})
|
|
|
|
return () => {
|
|
authListener.unsubscribe()
|
|
}
|
|
}, [])
|
|
|
|
const signOut = async () => {
|
|
const { error } = await supabase.auth.signOut()
|
|
if (!error) {
|
|
router.push('/')
|
|
}
|
|
}
|
|
|
|
return (
|
|
<UserContext.Provider
|
|
value={{
|
|
userLoaded,
|
|
user,
|
|
signOut,
|
|
}}
|
|
>
|
|
<Component {...pageProps} />
|
|
</UserContext.Provider>
|
|
)
|
|
}
|