redirect to login on pages that require auth (#269)
This commit is contained in:
parent
169c762a1f
commit
7bbcc86270
|
@ -52,15 +52,24 @@ export default async function getSSRApolloClient ({ req, res, me = null }) {
|
|||
return client
|
||||
}
|
||||
|
||||
export function getGetServerSideProps (queryOrFunc, variablesOrFunc = null, notFoundFunc, requireVar) {
|
||||
/**
|
||||
* Takes a query and variables and returns a getServerSideProps function
|
||||
*
|
||||
* @param opts Options
|
||||
* @param opts.query graphql query or function that return graphql query
|
||||
* @param opts.variables graphql variables or function that return graphql variables
|
||||
* @param opts.notFound function that tests data to determine if 404
|
||||
* @param opts.authRequired boolean that determines if auth is required
|
||||
*/
|
||||
export function getGetServerSideProps ({ query, variables, notFound, authRequired }) {
|
||||
return async function ({ req, res, query: params }) {
|
||||
const { nodata, ...realParams } = params
|
||||
// we want to use client-side cache
|
||||
if (nodata) return { props: { } }
|
||||
|
||||
const variables = typeof variablesOrFunc === 'function' ? variablesOrFunc(realParams) : variablesOrFunc
|
||||
variables = typeof variables === 'function' ? variables(realParams) : variables
|
||||
const vars = { ...realParams, ...variables }
|
||||
const query = typeof queryOrFunc === 'function' ? queryOrFunc(vars) : queryOrFunc
|
||||
query = typeof query === 'function' ? query(vars) : query
|
||||
|
||||
const client = await getSSRApolloClient({ req, res })
|
||||
|
||||
|
@ -69,36 +78,27 @@ export function getGetServerSideProps (queryOrFunc, variablesOrFunc = null, notF
|
|||
variables: { skipUpdate: true }
|
||||
})
|
||||
|
||||
if (authRequired && !me) {
|
||||
const callback = process.env.PUBLIC_URL + req.url
|
||||
return {
|
||||
redirect: {
|
||||
destination: `/login?callbackUrl=${encodeURIComponent(callback)}`
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const { data: { price } } = await client.query({
|
||||
query: PRICE, variables: { fiatCurrency: me?.fiatCurrency }
|
||||
})
|
||||
|
||||
if (requireVar && !vars[requireVar]) {
|
||||
return {
|
||||
notFound: true
|
||||
}
|
||||
}
|
||||
|
||||
let error = null; let data = null; let props = {}
|
||||
if (query) {
|
||||
try {
|
||||
({ error, data } = await client.query({
|
||||
query,
|
||||
variables: vars
|
||||
}))
|
||||
} catch (err) {
|
||||
if (err.message === 'you must be logged in') {
|
||||
const callback = process.env.PUBLIC_URL + req.url
|
||||
return {
|
||||
redirect: {
|
||||
destination: `/login?callbackUrl=${encodeURIComponent(callback)}`
|
||||
}
|
||||
}
|
||||
}
|
||||
throw err
|
||||
}
|
||||
({ error, data } = await client.query({
|
||||
query,
|
||||
variables: vars
|
||||
}))
|
||||
|
||||
if (error || !data || (notFoundFunc && notFoundFunc(data, vars))) {
|
||||
if (error || !data || (notFound && notFound(data, vars))) {
|
||||
return {
|
||||
notFound: true
|
||||
}
|
||||
|
|
|
@ -10,8 +10,13 @@ import { Form, Select } from '../../components/form'
|
|||
|
||||
const staticVariables = { sort: 'user' }
|
||||
const variablesFunc = vars =>
|
||||
({ includeComments: COMMENT_TYPE_QUERY.includes(vars.type), ...staticVariables, ...vars })
|
||||
export const getServerSideProps = getGetServerSideProps(USER_WITH_ITEMS, variablesFunc, data => !data.user)
|
||||
({
|
||||
includeComments: COMMENT_TYPE_QUERY.includes(vars.type),
|
||||
...staticVariables,
|
||||
...vars
|
||||
})
|
||||
export const getServerSideProps = getGetServerSideProps(
|
||||
{ query: USER_WITH_ITEMS, variables: variablesFunc, notFound: data => !data.user })
|
||||
|
||||
export default function UserItems ({ ssrData }) {
|
||||
const router = useRouter()
|
||||
|
|
|
@ -16,8 +16,10 @@ import CancelButton from '../../components/cancel-button'
|
|||
import { useRouter } from 'next/router'
|
||||
import PageLoading from '../../components/page-loading'
|
||||
|
||||
export const getServerSideProps = getGetServerSideProps(USER_FULL, null,
|
||||
data => !data.user)
|
||||
export const getServerSideProps = getGetServerSideProps({
|
||||
query: USER_FULL,
|
||||
notFound: data => !data.user
|
||||
})
|
||||
|
||||
export function BioForm ({ handleDone, bio }) {
|
||||
const [upsertBio] = useMutation(
|
||||
|
|
|
@ -12,8 +12,11 @@ import { useQuery } from '@apollo/client'
|
|||
import { useRouter } from 'next/router'
|
||||
import PageLoading from '../../../components/page-loading'
|
||||
|
||||
export const getServerSideProps = getGetServerSideProps(ITEM, null,
|
||||
data => !data.item)
|
||||
export const getServerSideProps = getGetServerSideProps({
|
||||
query: ITEM,
|
||||
notFound: data => !data.item,
|
||||
authRequired: true
|
||||
})
|
||||
|
||||
export default function PostEdit ({ ssrData }) {
|
||||
const router = useRouter()
|
||||
|
|
|
@ -6,8 +6,10 @@ import { useQuery } from '@apollo/client'
|
|||
import { useRouter } from 'next/router'
|
||||
import PageLoading from '../../../components/page-loading'
|
||||
|
||||
export const getServerSideProps = getGetServerSideProps(ITEM_FULL, null,
|
||||
data => !data.item || (data.item.status === 'STOPPED' && !data.item.mine))
|
||||
export const getServerSideProps = getGetServerSideProps({
|
||||
query: ITEM_FULL,
|
||||
notFound: data => !data.item || (data.item.status === 'STOPPED' && !data.item.mine)
|
||||
})
|
||||
|
||||
export default function Item ({ ssrData }) {
|
||||
const router = useRouter()
|
||||
|
|
|
@ -7,8 +7,10 @@ import { useQuery } from '@apollo/client'
|
|||
import { useRouter } from 'next/router'
|
||||
import PageLoading from '../../../components/page-loading'
|
||||
|
||||
export const getServerSideProps = getGetServerSideProps(ITEM_OTS, null,
|
||||
data => !data.item || !data.item.otsHash)
|
||||
export const getServerSideProps = getGetServerSideProps({
|
||||
query: ITEM_OTS,
|
||||
notFound: data => !data.item || !data.item.otsHash
|
||||
})
|
||||
|
||||
export default function OtsItem ({ ssrData }) {
|
||||
const router = useRouter()
|
||||
|
|
|
@ -9,8 +9,10 @@ import Item from '../../../components/item'
|
|||
import { useQuery } from '@apollo/client'
|
||||
import PageLoading from '../../../components/page-loading'
|
||||
|
||||
export const getServerSideProps = getGetServerSideProps(RELATED_ITEMS_WITH_ITEM, null,
|
||||
data => !data.item)
|
||||
export const getServerSideProps = getGetServerSideProps({
|
||||
query: RELATED_ITEMS_WITH_ITEM,
|
||||
notFound: data => !data.item
|
||||
})
|
||||
|
||||
export default function Related ({ ssrData }) {
|
||||
const router = useRouter()
|
||||
|
|
|
@ -5,7 +5,7 @@ import Notifications, { NotificationAlert } from '../components/notifications'
|
|||
import { HAS_NOTIFICATIONS, NOTIFICATIONS } from '../fragments/notifications'
|
||||
import { useApolloClient } from '@apollo/client'
|
||||
|
||||
export const getServerSideProps = getGetServerSideProps(NOTIFICATIONS)
|
||||
export const getServerSideProps = getGetServerSideProps({ query: NOTIFICATIONS, authRequired: true })
|
||||
|
||||
export default function NotificationPage ({ ssrData }) {
|
||||
const client = useApolloClient()
|
||||
|
|
|
@ -31,7 +31,7 @@ const REFERRALS = gql`
|
|||
}
|
||||
}`
|
||||
|
||||
export const getServerSideProps = getGetServerSideProps(REFERRALS)
|
||||
export const getServerSideProps = getGetServerSideProps({ query: REFERRALS, authRequired: true })
|
||||
|
||||
export default function Referrals ({ ssrData }) {
|
||||
const router = useRouter()
|
||||
|
|
|
@ -12,8 +12,10 @@ const GrowthPieChart = dynamic(() => import('../../components/charts').then(mod
|
|||
loading: () => <div>Loading...</div>
|
||||
})
|
||||
|
||||
export const getServerSideProps = getGetServerSideProps(ME_REWARDS, null,
|
||||
(data, params) => data.rewards.total === 0 || new Date(data.rewards.time) > new Date())
|
||||
export const getServerSideProps = getGetServerSideProps({
|
||||
query: ME_REWARDS,
|
||||
notFound: (data, params) => data.rewards.total === 0 || new Date(data.rewards.time) > new Date()
|
||||
})
|
||||
|
||||
const timeString = when => new Date(when).toISOString().slice(0, 10)
|
||||
|
||||
|
|
|
@ -45,7 +45,7 @@ function midnight (tz) {
|
|||
return date.getTime() + tzOffset(tz) * 60 * 60 * 1000
|
||||
}
|
||||
|
||||
export const getServerSideProps = getGetServerSideProps(REWARDS)
|
||||
export const getServerSideProps = getGetServerSideProps({ query: REWARDS })
|
||||
|
||||
export function RewardLine ({ total }) {
|
||||
const threshold = useMemo(() => midnight('America/Chicago'))
|
||||
|
|
|
@ -16,7 +16,7 @@ import { Fragment } from 'react'
|
|||
import ItemJob from '../components/item-job'
|
||||
import PageLoading from '../components/page-loading'
|
||||
|
||||
export const getServerSideProps = getGetServerSideProps(WALLET_HISTORY)
|
||||
export const getServerSideProps = getGetServerSideProps({ query: WALLET_HISTORY, authRequired: true })
|
||||
|
||||
function satusClass (status) {
|
||||
if (!status) {
|
||||
|
|
|
@ -24,7 +24,7 @@ import { authErrorMessage } from '../components/login'
|
|||
import { NostrAuth } from '../components/nostr-auth'
|
||||
import { useToast } from '../components/toast'
|
||||
|
||||
export const getServerSideProps = getGetServerSideProps(SETTINGS)
|
||||
export const getServerSideProps = getGetServerSideProps({ query: SETTINGS, authRequired: true })
|
||||
|
||||
function bech32encode (hexString) {
|
||||
return bech32.encode('npub', bech32.toWords(Buffer.from(hexString, 'hex')))
|
||||
|
|
|
@ -65,7 +65,7 @@ const GROWTH_QUERY = gql`
|
|||
}
|
||||
}`
|
||||
|
||||
export const getServerSideProps = getGetServerSideProps(GROWTH_QUERY)
|
||||
export const getServerSideProps = getGetServerSideProps({ query: GROWTH_QUERY })
|
||||
|
||||
export default function Growth ({ ssrData }) {
|
||||
const router = useRouter()
|
||||
|
|
|
@ -5,7 +5,7 @@ import UserList from '../../components/user-list'
|
|||
import { useRouter } from 'next/router'
|
||||
|
||||
const staticVariables = { limit: 21, similarity: 0.2 }
|
||||
export const getServerSideProps = getGetServerSideProps(USER_SEARCH, staticVariables)
|
||||
export const getServerSideProps = getGetServerSideProps({ query: USER_SEARCH, variables: staticVariables })
|
||||
|
||||
export default function Index ({ ssrData }) {
|
||||
const router = useRouter()
|
||||
|
|
|
@ -19,7 +19,7 @@ import { SSR } from '../lib/constants'
|
|||
import { numWithUnits } from '../lib/format'
|
||||
import styles from '../components/user-header.module.css'
|
||||
|
||||
export const getServerSideProps = getGetServerSideProps()
|
||||
export const getServerSideProps = getGetServerSideProps({ authRequired: true })
|
||||
|
||||
export default function Wallet () {
|
||||
const router = useRouter()
|
||||
|
|
|
@ -5,8 +5,10 @@ import Layout from '../../components/layout'
|
|||
import { SUB_ITEMS } from '../../fragments/subs'
|
||||
import Snl from '../../components/snl'
|
||||
|
||||
export const getServerSideProps = getGetServerSideProps(SUB_ITEMS, null,
|
||||
(data, vars) => vars.sub && !data.sub)
|
||||
export const getServerSideProps = getGetServerSideProps({
|
||||
query: SUB_ITEMS,
|
||||
notFound: (data, vars) => vars.sub && !data.sub
|
||||
})
|
||||
|
||||
export default function Sub ({ ssrData }) {
|
||||
const router = useRouter()
|
||||
|
|
|
@ -6,8 +6,10 @@ import { useQuery } from '@apollo/client'
|
|||
import { useRouter } from 'next/router'
|
||||
import PageLoading from '../../components/page-loading'
|
||||
|
||||
export const getServerSideProps = getGetServerSideProps(SUB, null,
|
||||
(data, vars) => vars.sub && !data.sub)
|
||||
export const getServerSideProps = getGetServerSideProps({
|
||||
query: SUB,
|
||||
notFound: (data, vars) => vars.sub && !data.sub
|
||||
})
|
||||
|
||||
export default function PostPage ({ ssrData }) {
|
||||
const router = useRouter()
|
||||
|
|
|
@ -9,10 +9,11 @@ import { COMMENT_TYPE_QUERY } from '../../../lib/constants'
|
|||
const staticVariables = { sort: 'recent' }
|
||||
const variablesFunc = vars =>
|
||||
({ includeComments: COMMENT_TYPE_QUERY.includes(vars.type), ...staticVariables, ...vars })
|
||||
export const getServerSideProps = getGetServerSideProps(
|
||||
SUB_ITEMS,
|
||||
variablesFunc,
|
||||
(data, vars) => vars.sub && !data.sub)
|
||||
export const getServerSideProps = getGetServerSideProps({
|
||||
query: SUB_ITEMS,
|
||||
variables: variablesFunc,
|
||||
notFound: (data, vars) => vars.sub && !data.sub
|
||||
})
|
||||
|
||||
export default function Index ({ ssrData }) {
|
||||
const router = useRouter()
|
||||
|
|
|
@ -5,8 +5,10 @@ import { SUB_SEARCH } from '../../fragments/subs'
|
|||
import Down from '../../svgs/arrow-down-line.svg'
|
||||
import Items from '../../components/items'
|
||||
|
||||
export const getServerSideProps = getGetServerSideProps(SUB_SEARCH, null,
|
||||
(data, vars) => vars.sub && !data.sub)
|
||||
export const getServerSideProps = getGetServerSideProps({
|
||||
query: SUB_SEARCH,
|
||||
notFound: (data, vars) => vars.sub && !data.sub
|
||||
})
|
||||
|
||||
export default function Index ({ ssrData }) {
|
||||
const router = useRouter()
|
||||
|
|
|
@ -9,10 +9,11 @@ import { COMMENT_TYPE_QUERY } from '../../../../lib/constants'
|
|||
const staticVariables = { sort: 'top' }
|
||||
const variablesFunc = vars =>
|
||||
({ includeComments: COMMENT_TYPE_QUERY.includes(vars.type), ...staticVariables, ...vars })
|
||||
export const getServerSideProps = getGetServerSideProps(
|
||||
SUB_ITEMS,
|
||||
variablesFunc,
|
||||
(data, vars) => vars.sub && !data.sub)
|
||||
export const getServerSideProps = getGetServerSideProps({
|
||||
query: SUB_ITEMS,
|
||||
variables: variablesFunc,
|
||||
notFound: (data, vars) => vars.sub && !data.sub
|
||||
})
|
||||
|
||||
export default function Index ({ ssrData }) {
|
||||
const router = useRouter()
|
||||
|
|
|
@ -4,7 +4,7 @@ import TopHeader from '../../../components/top-header'
|
|||
import { TOP_COWBOYS } from '../../../fragments/users'
|
||||
import UserList from '../../../components/user-list'
|
||||
|
||||
export const getServerSideProps = getGetServerSideProps(TOP_COWBOYS)
|
||||
export const getServerSideProps = getGetServerSideProps({ query: TOP_COWBOYS })
|
||||
|
||||
export default function Index ({ ssrData }) {
|
||||
return (
|
||||
|
|
|
@ -5,7 +5,7 @@ import TopHeader from '../../../../components/top-header'
|
|||
import { TOP_USERS } from '../../../../fragments/users'
|
||||
import UserList from '../../../../components/user-list'
|
||||
|
||||
export const getServerSideProps = getGetServerSideProps(TOP_USERS)
|
||||
export const getServerSideProps = getGetServerSideProps({ query: TOP_USERS })
|
||||
|
||||
export default function Index ({ ssrData }) {
|
||||
const router = useRouter()
|
||||
|
|
Loading…
Reference in New Issue