stacker.news/pages/login.js

56 lines
1.5 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'
import { StaticLayout } from '../components/layout'
2021-10-15 17:56:54 +00:00
import Login from '../components/login'
2023-05-07 13:33:51 +00:00
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 getSession({ req })
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
}
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 (
<StaticLayout footerLinks={false}>
<Login
Footer={() => <LoginFooter callbackUrl={props.callbackUrl} />}
{...props}
/>
</StaticLayout>
2023-01-10 00:33:44 +00:00
)
}