Skip to content

Commit 96cd0b6

Browse files
authoredMar 8, 2024
feat(core): redirect to previous path after login (#5932)
1 parent f103967 commit 96cd0b6

File tree

3 files changed

+34
-17
lines changed

3 files changed

+34
-17
lines changed
 

‎packages/sanity/src/core/store/_legacy/authStore/createLoginComponent.tsx

+13-11
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ interface CreateLoginComponentOptions extends AuthConfig {
5959
}
6060

6161
interface CreateHrefForProviderOptions {
62-
basePath: string
62+
redirectPath: string
6363
loginMethod: AuthConfig['loginMethod']
6464
projectId: string
6565
url: string
@@ -69,10 +69,10 @@ function createHrefForProvider({
6969
loginMethod = 'dual',
7070
projectId,
7171
url,
72-
basePath,
72+
redirectPath,
7373
}: CreateHrefForProviderOptions) {
7474
const params = new URLSearchParams()
75-
params.set('origin', `${window.location.origin}${basePath}`)
75+
params.set('origin', `${window.location.origin}${redirectPath}`)
7676
params.set('projectId', projectId)
7777

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

99-
function LoginComponent({projectId, basePath}: LoginComponentProps) {
99+
function LoginComponent({projectId, ...props}: LoginComponentProps) {
100+
const redirectPath = props.redirectPath || props.basePath || '/'
101+
100102
const [providers, setProviders] = useState<AuthProvider[] | null>(null)
101103
const [error, setError] = useState<unknown>(null)
102104
if (error) throw error
@@ -113,24 +115,24 @@ export function createLoginComponent({
113115

114116
// only create a direct URL if `redirectOnSingle` is true and there is only
115117
// one provider available
116-
const redirectUrl =
118+
const redirectUrlForRedirectOnSingle =
117119
redirectOnSingle &&
118120
providers?.length === 1 &&
119121
providers?.[0] &&
120122
createHrefForProvider({
121123
loginMethod,
122124
projectId,
123125
url: providers[0].url,
124-
basePath,
126+
redirectPath,
125127
})
126128

127-
const loading = !providers || redirectUrl
129+
const loading = !providers || redirectUrlForRedirectOnSingle
128130

129131
useEffect(() => {
130-
if (redirectUrl) {
131-
window.location.href = redirectUrl
132+
if (redirectUrlForRedirectOnSingle) {
133+
window.location.href = redirectUrlForRedirectOnSingle
132134
}
133-
}, [redirectUrl])
135+
}, [redirectUrlForRedirectOnSingle])
134136

135137
if (loading) {
136138
return <LoadingBlock showText />
@@ -153,7 +155,7 @@ export function createLoginComponent({
153155
loginMethod,
154156
projectId,
155157
url: provider.url,
156-
basePath,
158+
redirectPath,
157159
})}
158160
mode="ghost"
159161
size="large"

‎packages/sanity/src/core/store/_legacy/authStore/types.ts

+13-4
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,16 @@ export interface AuthState {
7878
* @beta
7979
* @hidden
8080
*/
81-
export interface LoginComponentProps {
82-
projectId: string
83-
basePath: string
84-
}
81+
export type LoginComponentProps =
82+
| {
83+
projectId: string
84+
/** @deprecated use redirectPath instead */
85+
basePath: string
86+
redirectPath?: string
87+
}
88+
| {
89+
projectId: string
90+
redirectPath: string
91+
/** @deprecated use redirectPath instead */
92+
basePath?: string
93+
}

‎packages/sanity/src/core/studio/components/navbar/workspace/WorkspaceAuth/WorkspaceAuth.tsx

+8-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import {AddIcon, ArrowLeftIcon, ChevronRightIcon} from '@sanity/icons'
22
import {Box, Card, Flex, Stack} from '@sanity/ui'
3-
import {omit} from 'lodash'
43
import {useCallback, useState} from 'react'
54

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

0 commit comments

Comments
 (0)
Please sign in to comment.