Skip to content

Commit 0118bce

Browse files
authored
fix(next): set the user data before redirect after login (#8135)
## Description Fixes #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
1 parent 46707e4 commit 0118bce

File tree

1 file changed

+8
-2
lines changed
  • packages/next/src/views/Login/LoginForm

1 file changed

+8
-2
lines changed

packages/next/src/views/Login/LoginForm/index.tsx

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ import React from 'react'
66
const baseClass = 'login__form'
77
const Link = (LinkImport.default || LinkImport) as unknown as typeof LinkImport.default
88

9-
import type { FormState } from 'payload'
9+
import type { ClientUser, FormState } from 'payload'
1010

11-
import { Form, FormSubmit, PasswordField, useConfig, useTranslation } from '@payloadcms/ui'
11+
import { Form, FormSubmit, PasswordField, useAuth, useConfig, useTranslation } from '@payloadcms/ui'
1212
import { formatAdminURL } from '@payloadcms/ui/shared'
1313

1414
import type { LoginFieldProps } from '../LoginField/index.js'
@@ -50,6 +50,7 @@ export const LoginForm: React.FC<{
5050
})
5151

5252
const { t } = useTranslation()
53+
const { setUser } = useAuth()
5354

5455
const initialState: FormState = {
5556
password: {
@@ -73,13 +74,18 @@ export const LoginForm: React.FC<{
7374
}
7475
}
7576

77+
const handleLogin = (data: { user: ClientUser }) => {
78+
setUser(data.user)
79+
}
80+
7681
return (
7782
<Form
7883
action={`${apiRoute}/${userSlug}/login`}
7984
className={baseClass}
8085
disableSuccessStatus
8186
initialState={initialState}
8287
method="POST"
88+
onSuccess={handleLogin}
8389
redirect={typeof searchParams?.redirect === 'string' ? searchParams.redirect : adminRoute}
8490
waitForAutocomplete
8591
>

0 commit comments

Comments
 (0)