bradgarropy
03/04/2022, 5:23 PM@supabase/supabase-auth-helpers
package to enable authentication in Next.js on the client and the server.
https://github.com/supabase-community/supabase-auth-helpers/tree/main/src/nextjs
After logging in, I redirect over to /todos
.
const handleSignin: FormEventHandler<HTMLFormElement> = async event => {
event.preventDefault()
await supabase.auth.signIn({
email,
password,
})
router.push("/todos")
}
But the redirect doesn't work the first time. Or it's bouncing back to /login
. After logging in a second time, then it work. I presume because the session cookie has not yet been set.
const getServerSideProps = withAuthRequired({
redirectTo: "/login",
getServerSideProps: async ctx => {
const todos = await readAllTodos(ctx)
return {
props: {
initialTodos: todos,
},
}
},
})
Here's the live site and the code.
https://next-todo-one.vercel.app
https://github.com/bradgarropy/next-todothorwebdev
03/04/2022, 5:39 PMbradgarropy
03/04/2022, 5:40 PMexport {supabaseClient as supabase} from "@supabase/supabase-auth-helpers/nextjs"
https://github.com/bradgarropy/next-todo/blob/master/src/utils/supabase.tsthorwebdev
03/04/2022, 5:40 PMbradgarropy
03/04/2022, 5:41 PMthorwebdev
03/04/2022, 5:42 PMthorwebdev
03/04/2022, 5:44 PMthorwebdev
03/04/2022, 5:51 PMbradgarropy
03/04/2022, 5:53 PMconst handleSignin: FormEventHandler<HTMLFormElement> = async event => {
event.preventDefault()
await supabase.auth.signIn({
email,
password,
})
}
useEffect(() => {
if (user) {
router.push("/todos")
}
}, [user, router])