Skip to content
This repository was archived by the owner on Aug 21, 2024. It is now read-only.

Commit 2108def

Browse files
Redirect to original location after login (#9549)
* redirect to studio after login * added redirect for admin panel * persist `redirectUrl` as search params in url * correct type error --------- Co-authored-by: Hanzla Mateen <[email protected]>
1 parent 58595de commit 2108def

File tree

4 files changed

+28
-12
lines changed

4 files changed

+28
-12
lines changed

β€Žpackages/client-core/src/admin/adminRoutes.tsxβ€Ž

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ Ethereal Engine. All Rights Reserved.
2424
*/
2525

2626
import React, { lazy, Suspense, useEffect } from 'react'
27-
import { Navigate, Route, Routes, useLocation } from 'react-router-dom'
27+
import { Route, Routes, useLocation } from 'react-router-dom'
2828

2929
import { getMutableState, useHookstate } from '@etherealengine/hyperflux'
3030
import Dashboard from '@etherealengine/ui/src/primitives/mui/Dashboard'
@@ -36,11 +36,12 @@ import Analytics from './components/Analytics'
3636
import { DefaultAdminRoutes } from './DefaultAdminRoutes'
3737

3838
import '@etherealengine/engine/src/EngineModule'
39+
import { RouterState } from '../common/services/RouterService'
3940

4041
const $allowed = lazy(() => import('@etherealengine/client-core/src/admin/allowedRoutes'))
4142

4243
const AdminRoutes = () => {
43-
const _location = useLocation()
44+
const location = useLocation()
4445
const admin = useHookstate(getMutableState(AuthState)).user
4546

4647
const allowedRoutes = useHookstate(getMutableState(AllowedAdminRoutesState))
@@ -65,12 +66,13 @@ const AdminRoutes = () => {
6566
}, [scopes])
6667

6768
if (admin?.id?.value?.length! > 0 && !admin?.scopes?.value?.find((scope) => scope.type === 'admin:admin')) {
68-
return <Navigate to={{ pathname: '/' }} />
69+
RouterState.navigate('/', { redirectUrl: location.pathname })
70+
return <></>
6971
}
7072

7173
return (
7274
<Dashboard>
73-
<Suspense fallback={<LoadingCircle message={`Loading ${_location.pathname.split('/')[2]}...`} />}>
75+
<Suspense fallback={<LoadingCircle message={`Loading ${location.pathname.split('/')[2]}...`} />}>
7476
<Routes>
7577
<Route path="/*" element={<$allowed />} />
7678
{<Route path="/" element={<Analytics />} />}

β€Žpackages/client-core/src/common/services/RouterService.tsβ€Ž

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,20 +25,26 @@ Ethereal Engine. All Rights Reserved.
2525

2626
import i18n from 'i18next'
2727
import { lazy, useEffect } from 'react'
28-
import { useNavigate } from 'react-router-dom'
28+
import { useNavigate, useSearchParams } from 'react-router-dom'
2929

3030
import { routePath, RouteType } from '@etherealengine/common/src/schema.type.module'
3131
import { Engine } from '@etherealengine/engine/src/ecs/classes/Engine'
3232
import { defineState, getMutableState, NO_PROXY, useHookstate } from '@etherealengine/hyperflux'
3333
import { loadRoute } from '@etherealengine/projects/loadRoute'
3434

35+
type QueryParamsType = { [key: string]: string }
36+
3537
export const RouterState = defineState({
3638
name: 'RouterState',
3739
initial: () => ({
38-
pathname: location.pathname
40+
pathname: location.pathname,
41+
queryParams: null as QueryParamsType | null
3942
}),
40-
navigate: (pathname: string) => {
41-
getMutableState(RouterState).pathname.set(pathname)
43+
navigate: (pathname: string, queryParams: QueryParamsType | { redirectUrl: string } = {}) => {
44+
getMutableState(RouterState).set({
45+
pathname,
46+
queryParams
47+
})
4248
}
4349
})
4450

@@ -83,6 +89,7 @@ export const useCustomRoutes = () => {
8389

8490
const navigate = useNavigate()
8591
const routerState = useHookstate(getMutableState(RouterState))
92+
const [_, setSearchParams] = useSearchParams()
8693

8794
useEffect(() => {
8895
getCustomRoutes().then((routes) => {
@@ -96,6 +103,12 @@ export const useCustomRoutes = () => {
96103
}
97104
}, [location.pathname])
98105

106+
useEffect(() => {
107+
if (routerState.queryParams.value) {
108+
setSearchParams(routerState.queryParams.value)
109+
}
110+
}, [routerState.queryParams])
111+
99112
useEffect(() => {
100113
if (location.pathname !== routerState.pathname.value) {
101114
navigate(routerState.pathname.value)

β€Žpackages/client-core/src/user/services/AuthService.tsβ€Ž

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -343,10 +343,10 @@ export const AuthService = {
343343
async loginUserByOAuth(service: string, location: any) {
344344
getMutableState(AuthState).merge({ isProcessing: true, error: '' })
345345
const token = getState(AuthState).authUser.accessToken
346-
const path = location?.state?.from || location.pathname
346+
const path = new URLSearchParams(location.search).get('redirectUrl') || location.pathname
347347

348348
const redirectConfig = {
349-
path: path
349+
path
350350
} as Record<string, string>
351351

352352
const currentUrl = new URL(window.location.href)

β€Žpackages/client/src/pages/editor/editor.tsxβ€Ž

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ Ethereal Engine. All Rights Reserved.
2626
import { useHookstate } from '@hookstate/core'
2727
import { t } from 'i18next'
2828
import React, { Suspense, useEffect, useState } from 'react'
29-
import { Route, Routes, useNavigate, useParams } from 'react-router-dom'
29+
import { Route, Routes, useLocation, useNavigate, useParams } from 'react-router-dom'
3030

3131
import { RouterState } from '@etherealengine/client-core/src/common/services/RouterService'
3232
import { LoadingCircle } from '@etherealengine/client-core/src/components/LoadingCircle'
@@ -77,6 +77,7 @@ const EditorRouter = () => {
7777
}
7878

7979
const EditorProtectedRoutes = () => {
80+
const location = useLocation()
8081
const authState = useHookstate(getMutableState(AuthState))
8182
const user = authState.user
8283
const [isAuthorized, setAuthorized] = useState<boolean | null>(null)
@@ -85,7 +86,7 @@ const EditorProtectedRoutes = () => {
8586
if (user.scopes.value) {
8687
const hasAccess = userHasAccess('editor:write')
8788
if (!hasAccess) {
88-
RouterState.navigate('/')
89+
RouterState.navigate('/', { redirectUrl: location.pathname })
8990
setAuthorized(false)
9091
} else setAuthorized(true)
9192
}

0 commit comments

Comments
Β (0)