From 570277eb02b5774c423caf1f9c4469245d85285c Mon Sep 17 00:00:00 2001 From: miam-miam100 <49870539+miam-miam100@users.noreply.github.com> Date: Sun, 3 Mar 2024 20:29:40 +0000 Subject: [PATCH] Redirect logged in users back to the page they were on (#9) Signed-off-by: miam-miam100 <49870539+miam-miam100@users.noreply.github.com> --- .../(pages)/login/LoginForm/index.module.scss | 22 ----- src/app/(pages)/login/LoginForm/index.tsx | 87 ------------------- src/app/(pages)/login/index.module.scss | 9 -- src/app/(pages)/login/page.tsx | 22 ----- src/app/(routes)/login-redirect/route.ts | 14 +++ src/app/(routes)/login/route.ts | 11 +++ src/payload/components/BeforeLogin/index.tsx | 16 ++-- src/payload/payload.config.ts | 10 +-- 8 files changed, 37 insertions(+), 154 deletions(-) delete mode 100644 src/app/(pages)/login/LoginForm/index.module.scss delete mode 100644 src/app/(pages)/login/LoginForm/index.tsx delete mode 100644 src/app/(pages)/login/index.module.scss delete mode 100644 src/app/(pages)/login/page.tsx create mode 100644 src/app/(routes)/login-redirect/route.ts create mode 100644 src/app/(routes)/login/route.ts diff --git a/src/app/(pages)/login/LoginForm/index.module.scss b/src/app/(pages)/login/LoginForm/index.module.scss deleted file mode 100644 index d2fcd65..0000000 --- a/src/app/(pages)/login/LoginForm/index.module.scss +++ /dev/null @@ -1,22 +0,0 @@ -@import "../../../_css/common"; - -.form { - margin-bottom: var(--base); - display: flex; - flex-direction: column; - gap: calc(var(--base) / 2); - align-items: flex-start; - width: 66.66%; - - @include mid-break { - width: 100%; - } -} - -.submit { - margin-top: calc(var(--base) / 2); -} - -.message { - margin-bottom: var(--base); -} diff --git a/src/app/(pages)/login/LoginForm/index.tsx b/src/app/(pages)/login/LoginForm/index.tsx deleted file mode 100644 index 60c5419..0000000 --- a/src/app/(pages)/login/LoginForm/index.tsx +++ /dev/null @@ -1,87 +0,0 @@ -'use client' - -import React, { useCallback, useRef } from 'react' -import { useForm } from 'react-hook-form' -import Link from 'next/link' -import { useRouter, useSearchParams } from 'next/navigation' - -import { Button } from '../../../_components/Button' -import { Input } from '../../../_components/Input' -import { Message } from '../../../_components/Message' -import { useAuth } from '../../../_providers/Auth' - -import classes from './index.module.scss' - -type FormData = { - email: string - password: string -} - -const LoginForm: React.FC = () => { - const searchParams = useSearchParams() - const allParams = searchParams.toString() ? `?${searchParams.toString()}` : '' - const redirect = useRef(searchParams.get('redirect')) - const { login } = useAuth() - const router = useRouter() - const [error, setError] = React.useState(null) - - const { - register, - handleSubmit, - formState: { errors, isLoading }, - } = useForm() - - const onSubmit = useCallback( - async (data: FormData) => { - try { - await login(data) - if (redirect?.current) router.push(redirect.current as string) - else router.push('/account') - } catch (_) { - setError('There was an error with the credentials provided. Please try again.') - } - }, - [login, router], - ) - - return ( -
-

- {`This is where your users will login to manage their account, view their comment history, and more. To manage all users, `} - login to the admin dashboard - {'.'} -

- - - - ) } -export default BeforeLogin +export default OAuthButton diff --git a/src/payload/payload.config.ts b/src/payload/payload.config.ts index 28e08c1..066b0e7 100644 --- a/src/payload/payload.config.ts +++ b/src/payload/payload.config.ts @@ -34,7 +34,7 @@ import Tickets from './collections/Tickets' import Users from './collections/Users' import Votes from './collections/Votes' import BeforeDashboard from './components/BeforeDashboard' -import BeforeLogin from './components/BeforeLogin' +import OAuthButton from './components/BeforeLogin' import { seed } from './endpoints/seed' import { Footer } from './globals/Footer' import { Header } from './globals/Header' @@ -53,9 +53,6 @@ export default buildConfig({ user: Users.slug, bundler: webpackBundler(), // bundler-config components: { - // The `BeforeLogin` component renders a message that you see while logging into your admin panel. - // Feel free to delete this at any time. Simply remove the line below and the import `BeforeLogin` statement on line 15. - beforeLogin: [BeforeLogin], // The `BeforeDashboard` component renders the 'welcome' block that you see after logging into your admin panel. // Feel free to delete this at any time. Simply remove the line below and the import `BeforeDashboard` statement on line 15. beforeDashboard: [BeforeDashboard], @@ -175,9 +172,12 @@ export default buildConfig({ email: sotonData.mail, } }, + components: { + Button: OAuthButton, + }, userCollection: Users, subField: { name: 'username' }, - successRedirect: '/', + successRedirect: '/login-redirect', sessionOptions: { resave: false, saveUninitialized: false,