*Next.js SSR Authentication* I'm using the `@supa...
# help
b
Next.js SSR Authentication I'm using the
@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
.
Copy code
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.
Copy code
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-todo
t
Just had a quick look. Can you try using the supabaseClient that’s exported from the library?
b
I believe I am, although it might not look like it.
Copy code
export {supabaseClient as supabase} from "@supabase/supabase-auth-helpers/nextjs"
https://github.com/bradgarropy/next-todo/blob/master/src/utils/supabase.ts
b
Just did a little reexport to avoid renaming all of my imports of the client
t
Gotcha. Sorry, about to go offline here. Will take a deeper look tmr. Thanks for testing it out!
You could try making sure user from useUser is set before doing the router.push
b
dude! that was it! I was staring at that example for so long, thank you!
Copy code
const handleSignin: FormEventHandler<HTMLFormElement> = async event => {
        event.preventDefault()

        await supabase.auth.signIn({
            email,
            password,
        })
    }

    useEffect(() => {
        if (user) {
            router.push("/todos")
        }
    }, [user, router])