Skip to content

Commit

Permalink
feat(core): redirect to previous path after login (#5932)
Browse files Browse the repository at this point in the history
  • Loading branch information
ricokahler authored Mar 8, 2024
1 parent f103967 commit 96cd0b6
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ interface CreateLoginComponentOptions extends AuthConfig {
}

interface CreateHrefForProviderOptions {
basePath: string
redirectPath: string
loginMethod: AuthConfig['loginMethod']
projectId: string
url: string
Expand All @@ -69,10 +69,10 @@ function createHrefForProvider({
loginMethod = 'dual',
projectId,
url,
basePath,
redirectPath,
}: CreateHrefForProviderOptions) {
const params = new URLSearchParams()
params.set('origin', `${window.location.origin}${basePath}`)
params.set('origin', `${window.location.origin}${redirectPath}`)
params.set('projectId', projectId)

// Setting `type=token` will return the sid as part of the _query_, which may end up in
Expand All @@ -96,7 +96,9 @@ export function createLoginComponent({
}: CreateLoginComponentOptions) {
const useClient = createHookFromObservableFactory(getClient)

function LoginComponent({projectId, basePath}: LoginComponentProps) {
function LoginComponent({projectId, ...props}: LoginComponentProps) {
const redirectPath = props.redirectPath || props.basePath || '/'

const [providers, setProviders] = useState<AuthProvider[] | null>(null)
const [error, setError] = useState<unknown>(null)
if (error) throw error
Expand All @@ -113,24 +115,24 @@ export function createLoginComponent({

// only create a direct URL if `redirectOnSingle` is true and there is only
// one provider available
const redirectUrl =
const redirectUrlForRedirectOnSingle =
redirectOnSingle &&
providers?.length === 1 &&
providers?.[0] &&
createHrefForProvider({
loginMethod,
projectId,
url: providers[0].url,
basePath,
redirectPath,
})

const loading = !providers || redirectUrl
const loading = !providers || redirectUrlForRedirectOnSingle

useEffect(() => {
if (redirectUrl) {
window.location.href = redirectUrl
if (redirectUrlForRedirectOnSingle) {
window.location.href = redirectUrlForRedirectOnSingle
}
}, [redirectUrl])
}, [redirectUrlForRedirectOnSingle])

if (loading) {
return <LoadingBlock showText />
Expand All @@ -153,7 +155,7 @@ export function createLoginComponent({
loginMethod,
projectId,
url: provider.url,
basePath,
redirectPath,
})}
mode="ghost"
size="large"
Expand Down
17 changes: 13 additions & 4 deletions packages/sanity/src/core/store/_legacy/authStore/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,16 @@ export interface AuthState {
* @beta
* @hidden
*/
export interface LoginComponentProps {
projectId: string
basePath: string
}
export type LoginComponentProps =
| {
projectId: string
/** @deprecated use redirectPath instead */
basePath: string
redirectPath?: string
}
| {
projectId: string
redirectPath: string
/** @deprecated use redirectPath instead */
basePath?: string
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import {AddIcon, ArrowLeftIcon, ChevronRightIcon} from '@sanity/icons'
import {Box, Card, Flex, Stack} from '@sanity/ui'
import {omit} from 'lodash'
import {useCallback, useState} from 'react'

import {Button} from '../../../../../../ui-components'
Expand Down Expand Up @@ -58,7 +57,14 @@ export function WorkspaceAuth() {
>
<Stack padding={2} paddingBottom={3} paddingTop={4}>
<LoginComponent
{...omit(selectedWorkspace, ['type', '__internal'])}
projectId={selectedWorkspace.projectId}
redirectPath={
window.location.pathname.startsWith(selectedWorkspace.basePath)
? // NOTE: the fragment cannot be preserved because it's used
// to transfer an sid to a token
`${window.location.pathname}${window.location.search}`
: selectedWorkspace.basePath
}
key={selectedWorkspaceName}
/>
</Stack>
Expand Down

0 comments on commit 96cd0b6

Please sign in to comment.