Skip to content

Commit

Permalink
Redirect logged in users back to the page they were on (#9)
Browse files Browse the repository at this point in the history
Signed-off-by: miam-miam100 <[email protected]>
  • Loading branch information
miam-miam authored Mar 3, 2024
1 parent d3358b2 commit 570277e
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 154 deletions.
22 changes: 0 additions & 22 deletions src/app/(pages)/login/LoginForm/index.module.scss

This file was deleted.

87 changes: 0 additions & 87 deletions src/app/(pages)/login/LoginForm/index.tsx

This file was deleted.

9 changes: 0 additions & 9 deletions src/app/(pages)/login/index.module.scss

This file was deleted.

22 changes: 0 additions & 22 deletions src/app/(pages)/login/page.tsx

This file was deleted.

14 changes: 14 additions & 0 deletions src/app/(routes)/login-redirect/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { cookies } from 'next/headers'

export async function GET(): Promise<Response> {
const redirect = cookies().get('login-redirect')

let redirectUrl = process.env.NEXT_PUBLIC_SERVER_URL

if (redirect) {
redirectUrl = new URL(redirect.value, process.env.NEXT_PUBLIC_SERVER_URL).toString()
cookies().delete('login-redirect')
}

return Response.redirect(redirectUrl)
}
11 changes: 11 additions & 0 deletions src/app/(routes)/login/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { cookies } from 'next/headers'

export async function GET(req: Request): Promise<Response> {
const { searchParams } = new URL(req.url)
const redirect = searchParams.get('redirect')
if (redirect) {
cookies().set('login-redirect', redirect, { maxAge: 60 * 5, path: '/', httpOnly: true })
}

return Response.redirect(`${process.env.NEXT_PUBLIC_SERVER_URL}/oauth2/authorize`)
}
16 changes: 7 additions & 9 deletions src/payload/components/BeforeLogin/index.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
import React from 'react'
import { Button } from 'payload/components'

const BeforeLogin: React.FC = () => {
const OAuthButton: React.FC = () => {
return (
<div>
<p>
<b>Welcome to your dashboard!</b>
{' This is where site admins will log in to manage your website. Users will need to '}
<a href={`${process.env.PAYLOAD_PUBLIC_SERVER_URL}/login`}>log in to the site instead</a>
{' to access their user account, comment history, and more.'}
</p>
<div style={{ marginBottom: 20 }} className="custom">
<Button el="anchor" url="/login?redirect=/admin">
Sign in with oAuth
</Button>
</div>
)
}

export default BeforeLogin
export default OAuthButton
10 changes: 5 additions & 5 deletions src/payload/payload.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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],
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 570277e

Please sign in to comment.