Skip to content
This repository has been archived by the owner on Oct 11, 2021. It is now read-only.

Commit

Permalink
improvements to login screen to support redirect
Browse files Browse the repository at this point in the history
  • Loading branch information
erikdstock committed Nov 13, 2020
1 parent fddff78 commit df7bf6d
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 5 deletions.
4 changes: 4 additions & 0 deletions src/lib/auth/hooks/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,24 @@ export function useUser(options?: UseUserOptions) {

const { redirectTo, redirectIfFound } = { ...defaults, ...options }

console.log({ redirectTo })

const { data, error } = useSWR('/api/user', userFetcher)

const user = data?.user
const finished = Boolean(data)
const hasUser = Boolean(user)

useEffect(() => {
console.log({ redirectTo, redirectIfFound, finished, hasUser })
if (!redirectTo || !finished) return
if (
// If redirectTo is set, redirect if the user was not found.
(redirectTo && !redirectIfFound && !hasUser) ||
// If redirectIfFound is also set, redirect if the user was found
(redirectIfFound && hasUser)
) {
console.warn('LOGGED IN, REDIRECTING TO ' + redirectTo)
Router.push(redirectTo)
}
}, [redirectTo, redirectIfFound, finished, hasUser])
Expand Down
3 changes: 1 addition & 2 deletions src/pages/api/causality-graphql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,7 @@ export default async function causalityGraphql(
},
body: JSON.stringify({
query,
// hack: shove userId in from session
variables: { ...variables, userId: session.userId },
variables,
}),
}
)
Expand Down
9 changes: 8 additions & 1 deletion src/pages/auctions/[id].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,16 @@ import {
const lotIdx = 1

import { Sale } from 'components/Types'
import { useUser } from 'lib/auth/hooks'

const Auction: React.FC<{ sale: Sale }> = ({ sale }) => {
const router = useRouter()
const user =
useUser({
redirectTo: `/login?redirectTo=${encodeURIComponent(router.asPath)}`,
}) || {}

const { userId } = user

const lots =
sale?.saleArtworksConnection?.edges?.map(({ node }) => node) || []
Expand All @@ -72,7 +79,7 @@ const Auction: React.FC<{ sale: Sale }> = ({ sale }) => {
auctionDataQuery,
{
saleId: sale?.internalID,
userId: 'throwaway value',
userId,
}
)

Expand Down
11 changes: 9 additions & 2 deletions src/pages/login.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
import { useRouter } from 'next/router'
import { useState, FormEvent } from 'react'
import { useUser } from '../lib/auth/hooks'

const TWO_FACTOR_MISSING_RESPONSE = 'missing two-factor'

const Login = () => {
const user = useUser({ redirectIfFound: true, redirectTo: '/' })
const {
query: { redirectTo = '/' },
} = useRouter()
const user = useUser({
redirectIfFound: true,
redirectTo: '/',
})

const [errorMsg, setErrorMsg] = useState('')
const [isTwoFactorEnabled, setTwoFactorEnabled] = useState(false)
Expand All @@ -28,7 +35,7 @@ const Login = () => {
body: JSON.stringify(body),
})
if (res.status === 200) {
window.location.href = '/'
window.location.href = redirectTo as string
} else {
throw new Error(await res.text())
}
Expand Down

0 comments on commit df7bf6d

Please sign in to comment.