auth-ui icon indicating copy to clipboard operation
auth-ui copied to clipboard

Document how to use Auth UI with SSR

Open louwers opened this issue 3 years ago • 15 comments

Chore

Describe the chore

It takes some work to use stiches with SSR: https://stitches.dev/docs/server-side-rendering

If someone tries to use this library now they will see an flash of unstyled components (including blown up images for the logos) and then a rehydration failure logged in the console.

louwers avatar Oct 16 '22 19:10 louwers

I've exposed the getCssText function from stitches from the component itself in the latest release 0.2.5. I need to test this to be sure its working as expected, please report if it doesn't work as expected if you get to test it before I do.

silentworks avatar Nov 03 '22 00:11 silentworks

Just ran into the same issue with SSR. The exposed getCssText doesn't seem to be working. It returns an empty string.

jasperhartong avatar Dec 09 '22 12:12 jasperhartong

@silentworks Doesn't seem to be working well with Next.js 13. Would love a proper guide here. I added the getCssText function but getting a server render mismatch and hence flash of unstyled content .

image

imbhargav5 avatar Dec 13 '22 18:12 imbhargav5

Hey everyone - Might have been a bit of an oversight by me to rely on stitches to inject css variables (this was before Next13 though..), hence what I think is causing the 'flash of styles'. There might be a better way for us to handle CSS properties for SSR situations, but I've never really looked into it.

As a workaround, you could use the className customization, and don't use any of the token based theming. It's not ideal but it should avoid the problem.

@silentworks mentioned maybe we should provide some plain CSS to emulate the themes* which you can easily add to your CSS setup. We could also provide tailwind classes as well since that might be a popular choice.

* there is still only 1 theme, I was meaning to add a few more at some point 😅

MildTomato avatar Mar 01 '23 01:03 MildTomato

I'm facing this too with Next 13. Plain CSS would be a good workaround – Tailwind classes even better in my case.

darkostanimirovic avatar Mar 10 '23 18:03 darkostanimirovic

Currently you can render this with Tailwind classes or your own classes for it to render in Next 13 or any SSR framework. I'm currently using these classes.

<Auth
  supabaseClient={supabase}
  showLinks={false}
  providers={[]}
  appearance={{
    extend: false, // necessary in order to not render default styles
    className: {
      container: "space-y-2 mb-4",
      label: "text-gray-500 py-2 mb-2 block w-full",
      input: "py-2 px-3 border text-gray-900 placeholder:text-gray-400 sm:text-sm sm:leading-6 rounded w-full mb-2",
      button: "w-full block bg-emerald-500 border border-emerald-700 hover:border-emerald-400 hover:bg-emerald-400 focus:bg-emerald-400 text-white font-semibold rounded px-4 py-3 mt-6",
      message: "font-regular text-center mb-4 block w-full p-4 text-base text-red-500 mt-5",
    },
  }}
/>

silentworks avatar Mar 29 '23 21:03 silentworks

Adding here that this package doesn't work in next 13 for me. Hoping for an update soon!

Signup / sign in / auth providers all also not working.

dclark27 avatar Apr 28 '23 21:04 dclark27

Has supabase given up on supporting nextjs?

JohnShahawy avatar May 02 '23 21:05 JohnShahawy

@JohnShahawy not that I know of. We have been working away at supporting NextJS as much as we can along with all the other SSR frameworks.

silentworks avatar May 02 '23 23:05 silentworks

@JohnShahawy not that I know of. We have been working away at supporting NextJS as much as we can along with all the other SSR frameworks.

any help? its not that hard to switch to tailwind...

Darren120 avatar May 25 '23 20:05 Darren120

Any PR we can look into this?

muhaimincs avatar Jun 18 '23 09:06 muhaimincs

bump

Nedi11 avatar Oct 15 '23 17:10 Nedi11

I receive the error if not using client component

 ⨯ node_modules/.pnpm/@[email protected]_@[email protected]/node_modules/@supabase/auth-ui-react/dist/index.es.js (1180:14) @ eval
 ⨯ TypeError: (0 , react__WEBPACK_IMPORTED_MODULE_1__.createContext) is not a function

akarabach avatar Nov 01 '23 18:11 akarabach

Getting the same error as @akarabach

Next: 14.0.0 React: 18.2.0 @supabase/auth-ui-react: 0.4.6

 ⨯ ../node_modules/@supabase/auth-ui-react/dist/index.es.js (1180:14) @ eval
 ⨯ TypeError: (0 , react__WEBPACK_IMPORTED_MODULE_1__.createContext) is not a function
    at eval (webpack-internal:///(rsc)/../../node_modules/@supabase/auth-ui-react/dist/index.es.js:945:78)

jon301 avatar Nov 07 '23 07:11 jon301

As @akarabach said (cc @jon301 ) this happens because with the new app directory introduced in Next.js 13+, Next.js use server components by default.

Components that need hooks (context, use..., or as such as your case with Formik) needs to be classified as Client Components and they need to have the directive "use client" at the top of the source file.

So in my case, doing this was enough to get rid of the error and make it work:

'use client';

import React from 'react'
import {createClient } from '@supabase/supabase-js'
import { Auth } from '@supabase/auth-ui-react'

const supabase = createClient("...","...")

export default function Login() {
  return (
    <Auth supabaseClient={supabase}/>
  )
}

More information in Stack Overflow (it talks about Formik, but the problem, and thus the explanation, is the same)

tairosonloa avatar Nov 12 '23 17:11 tairosonloa