stacker.news/pages/login.js

50 lines
1.3 KiB
JavaScript
Raw Normal View History

2021-10-15 17:56:54 +00:00
import { providers, getSession } from 'next-auth/client'
2023-01-10 00:33:44 +00:00
import Link from 'next/link'
2023-05-01 20:58:30 +00:00
import LayoutStatic from '../components/layout-static'
2021-10-15 17:56:54 +00:00
import Login from '../components/login'
2021-04-24 21:05:07 +00:00
export async function getServerSideProps ({ req, res, query: { callbackUrl, error = null } }) {
const session = await getSession({ req })
const regex = /^https?:\/\/stacker.news\//
2023-05-07 11:59:19 +00:00
const external = !regex.test(decodeURIComponent(callbackUrl))
if (external) {
// This is a hotfix for open redirects. See https://github.com/stackernews/stacker.news/issues/264
// TODO: Add redirect notice to warn users
return res.status(500).end()
}
2021-08-12 23:48:27 +00:00
if (session && res && callbackUrl) {
2021-04-24 21:05:07 +00:00
res.writeHead(302, {
Location: callbackUrl
})
res.end()
2021-04-27 21:30:58 +00:00
return { props: {} }
2021-04-24 21:05:07 +00:00
}
return {
props: {
providers: await providers({ req, res }),
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 (
<small className='font-weight-bold text-muted pt-4'>Don't have an account? <Link href={{ pathname: '/signup', query: { callbackUrl } }}>sign up</Link></small>
)
}
export default function LoginPage (props) {
return (
2023-05-01 20:58:30 +00:00
<LayoutStatic>
<Login
Footer={() => <LoginFooter callbackUrl={props.callbackUrl} />}
{...props}
/>
2023-05-01 20:58:30 +00:00
</LayoutStatic>
2023-01-10 00:33:44 +00:00
)
}