stacker.news/pages/login.js

79 lines
2.1 KiB
JavaScript
Raw Normal View History

import { getProviders } from 'next-auth/react'
import { getServerSession } from 'next-auth/next'
import { getAuthOptions } from './api/auth/[...nextauth]'
2023-01-10 00:33:44 +00:00
import Link from 'next/link'
import { StaticLayout } from '@/components/layout'
import Login from '@/components/login'
import { isExternal } from '@/lib/url'
2021-04-24 21:05:07 +00:00
export async function getServerSideProps ({ req, res, query: { callbackUrl, error = null } }) {
const session = await getServerSession(req, res, getAuthOptions(req))
2021-04-24 21:05:07 +00:00
2023-07-10 17:18:17 +00:00
// prevent open redirects. See https://github.com/stackernews/stacker.news/issues/264
// let undefined urls through without redirect ... otherwise this interferes with multiple auth linking
2023-07-11 00:20:38 +00:00
let external = true
try {
external = isExternal(decodeURIComponent(callbackUrl))
} catch (err) {
console.error('error decoding callback:', callbackUrl, err)
}
2023-07-10 17:18:17 +00:00
2023-05-07 11:59:19 +00:00
if (external) {
2023-05-07 13:33:51 +00:00
callbackUrl = '/'
2023-05-07 11:59:19 +00:00
}
if (session && callbackUrl) {
// in the cause of auth linking we want to pass the error back to
// settings
if (error) {
2024-04-08 22:54:39 +00:00
const url = new URL(callbackUrl, process.env.NEXT_PUBLIC_URL)
url.searchParams.set('error', error)
callbackUrl = url.pathname + url.search
}
return {
redirect: {
destination: callbackUrl,
permanent: false
}
}
2021-04-24 21:05:07 +00:00
}
return {
props: {
providers: await getProviders(),
2021-10-15 17:56:54 +00:00
callbackUrl,
2021-04-24 21:05:07 +00:00
error
}
}
}
2023-01-10 00:33:44 +00:00
function LoginFooter ({ callbackUrl }) {
return (
2024-02-23 15:32:20 +00:00
<small className='fw-bold text-muted pt-4'>New to town? <Link href={{ pathname: '/signup', query: { callbackUrl } }}>sign up</Link></small>
)
}
function LoginHeader () {
return (
<>
<h3 className='w-100 pb-2'>
Login
</h3>
<div className='fw-bold text-muted w-100 text-start pb-4'>Ain't you a sight for sore eyes.</div>
</>
2023-01-10 00:33:44 +00:00
)
}
export default function LoginPage (props) {
return (
<StaticLayout footerLinks={false}>
<Login
Footer={() => <LoginFooter callbackUrl={props.callbackUrl} />}
2024-02-23 15:32:20 +00:00
Header={() => <LoginHeader />}
{...props}
/>
</StaticLayout>
2023-01-10 00:33:44 +00:00
)
}