Trying to test my app on a mobile device, but it won't get cookie from server-side requests. (works as expected on chrome)
when running getServerSideProps in Next.js I call:
export async function getServerSideProps({ req }) {
const { user } = await supabase.auth.api.getUserByCookie(req)
console.log('user from cookie', user)
if (!user) {
return { props: {}, redirect: { destination: '/sign-in' } }
}
const { data: teams } = await supabase
.from('teams')
.select('*')
.eq('user_id', user.id)
// do something with the user...
return {
props: {
teams,
},
}
}
This returns a user object when I run the app on Chrome or any other web browser, however, when I try on my mobile device, it fails and returns user = null.
I assume this is something related to the cookie session, any advice would be much appreciated.
I once had a problem with cookies and supabase and fixed it by using my service key for the server side code.
create a new client in your supabase client file and export it.
export const getServiceSupabase = () =>
createClient(
process.env.NEXT_PUBLIC_SUPABASE_URL,
process.env.SUPABASE_SERVICE_KEY
);
then use this client in your getServerSideProps()