fix(next): set the user data before redirect after login (#8135)
## Description Fixes https://github.com/payloadcms/payload/issues/8134 - [x] I have read and understand the [CONTRIBUTING.md](https://github.com/payloadcms/payload/blob/main/CONTRIBUTING.md) document in this repository. ## Type of change <!-- Please delete options that are not relevant. --> - [x] Bug fix (non-breaking change which fixes an issue) ## Checklist: - [x] Existing test suite passes locally with my changes
This commit is contained in:
@@ -6,9 +6,9 @@ import React from 'react'
|
|||||||
const baseClass = 'login__form'
|
const baseClass = 'login__form'
|
||||||
const Link = (LinkImport.default || LinkImport) as unknown as typeof LinkImport.default
|
const Link = (LinkImport.default || LinkImport) as unknown as typeof LinkImport.default
|
||||||
|
|
||||||
import type { FormState } from 'payload'
|
import type { ClientUser, FormState } from 'payload'
|
||||||
|
|
||||||
import { Form, FormSubmit, PasswordField, useConfig, useTranslation } from '@payloadcms/ui'
|
import { Form, FormSubmit, PasswordField, useAuth, useConfig, useTranslation } from '@payloadcms/ui'
|
||||||
import { formatAdminURL } from '@payloadcms/ui/shared'
|
import { formatAdminURL } from '@payloadcms/ui/shared'
|
||||||
|
|
||||||
import type { LoginFieldProps } from '../LoginField/index.js'
|
import type { LoginFieldProps } from '../LoginField/index.js'
|
||||||
@@ -50,6 +50,7 @@ export const LoginForm: React.FC<{
|
|||||||
})
|
})
|
||||||
|
|
||||||
const { t } = useTranslation()
|
const { t } = useTranslation()
|
||||||
|
const { setUser } = useAuth()
|
||||||
|
|
||||||
const initialState: FormState = {
|
const initialState: FormState = {
|
||||||
password: {
|
password: {
|
||||||
@@ -73,6 +74,10 @@ export const LoginForm: React.FC<{
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const handleLogin = (data: { user: ClientUser }) => {
|
||||||
|
setUser(data.user)
|
||||||
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Form
|
<Form
|
||||||
action={`${apiRoute}/${userSlug}/login`}
|
action={`${apiRoute}/${userSlug}/login`}
|
||||||
@@ -80,6 +85,7 @@ export const LoginForm: React.FC<{
|
|||||||
disableSuccessStatus
|
disableSuccessStatus
|
||||||
initialState={initialState}
|
initialState={initialState}
|
||||||
method="POST"
|
method="POST"
|
||||||
|
onSuccess={handleLogin}
|
||||||
redirect={typeof searchParams?.redirect === 'string' ? searchParams.redirect : adminRoute}
|
redirect={typeof searchParams?.redirect === 'string' ? searchParams.redirect : adminRoute}
|
||||||
waitForAutocomplete
|
waitForAutocomplete
|
||||||
>
|
>
|
||||||
|
|||||||
Reference in New Issue
Block a user