Trying to build something that does the following. I have a login page. If the login fails, I want to redirect to the same page but give an error message. I for the life of me cannot get anything working. Is there a way to redirect but also pass through some data?
'use server'
import { revalidatePath } from 'next/cache'
import { redirect } from 'next/navigation'
import { createClient } from 'utils/supabase/server'
export async function login() {
const supabase = await createClient()
const { data, error } = await supabase.auth.signInAnonymously()
if (error) {
redirect('/anonlogin');
} else {
revalidatePath('/', 'layout')
redirect('/')
}
}
import { login } from '@/anonlogin/actions'
import styles from '@/components/global/button.module.css'
export default function LoginPage() {
return (
<div>
<form>
<button className={styles.button} formAction={login}>Log in</button>
</form>
</div>
)
}
1
Why not use a Stateless option and go with a param i.e. redirect(/login?error=${encodeURIComponent(error.message)})
Then on the client-side useSearchParams#get('error')
, then if you want to be fancy, you can mask the URL on the client-side and replace the URL in order to keep it clean.
Monotoo is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.