Skip to content

Commit

Permalink
fix(next): set the user data before redirect after login (#8135)
Browse files Browse the repository at this point in the history
## 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
  • Loading branch information
r1tsuu authored Sep 10, 2024
1 parent 46707e4 commit 0118bce
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions packages/next/src/views/Login/LoginForm/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import React from 'react'
const baseClass = 'login__form'
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 type { LoginFieldProps } from '../LoginField/index.js'
Expand Down Expand Up @@ -50,6 +50,7 @@ export const LoginForm: React.FC<{
})

const { t } = useTranslation()
const { setUser } = useAuth()

const initialState: FormState = {
password: {
Expand All @@ -73,13 +74,18 @@ export const LoginForm: React.FC<{
}
}

const handleLogin = (data: { user: ClientUser }) => {
setUser(data.user)
}

return (
<Form
action={`${apiRoute}/${userSlug}/login`}
className={baseClass}
disableSuccessStatus
initialState={initialState}
method="POST"
onSuccess={handleLogin}
redirect={typeof searchParams?.redirect === 'string' ? searchParams.redirect : adminRoute}
waitForAutocomplete
>
Expand Down

0 comments on commit 0118bce

Please sign in to comment.