Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: force scope with params & redirect params #3038

Merged
merged 1 commit into from
Jun 21, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 46 additions & 3 deletions src/components/AppRoutes/AppRoutes.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import Sidebar from '~/components/Layout/Sidebar'
import Logout from '../Logout/Logout'
import * as Sentry from '@sentry/react'
import ErrorComponent from '~/components/ErrorComponent'
import { useLocalStorage } from 'react-use'

const publicPathsArray = [
publicPaths.signup,
Expand All @@ -30,22 +31,64 @@ const publicPathsArray = [

const PrivatePages = ({ children }) => {
const initializeAuth = useInitializeAuth()
const { pathname } = useLocation()
const { pathname, search } = useLocation()
const isUserLoggedIn = useSelector(isUserLogged)
const currentUser = useSelector(getCurrentUser)
const [currentScope] = useUserScope()
const [currentScope, updateCurrentScope] = useUserScope()
const userScopes = useSelector(getUserScopes)
const [, updateUserData] = useGetUserData()
const forceScope = new URLSearchParams(search).get('scope')
const redirect = new URLSearchParams(search).get('redirect')
const [localCurrentScope, setLocalCurrentScope] = useLocalStorage('forceScope')
const [tempRedirect, setTempRedirect] = useLocalStorage('tempRedirect')

useEffect(() => {
if (forceScope) {
setLocalCurrentScope(forceScope)
}

if (redirect && !isUserLoggedIn) {
setTempRedirect(redirect)
}

if ((tempRedirect || redirect) && isUserLoggedIn && currentScope !== null) {
const tr = tempRedirect ?? redirect
setTempRedirect(null)
if (tr) {
window.location.href = window.location.origin + tr
}
}

if (localCurrentScope && userScopes.length > 0) {
const forcedCurrentScope = userScopes.find(scope => scope.code === localCurrentScope)
if (forcedCurrentScope) {
updateCurrentScope(forcedCurrentScope.code).then(() => setLocalCurrentScope(null))
}
}

if (isUserLoggedIn) {
if (currentUser === null) {
updateUserData()
}
} else if (!publicPathsArray.includes(pathname)) {
initializeAuth()
}
}, [currentUser, initializeAuth, isUserLoggedIn, pathname, updateUserData])
}, [
currentUser,
initializeAuth,
isUserLoggedIn,
pathname,
updateUserData,
forceScope,
localCurrentScope,
userScopes,
updateCurrentScope,
setLocalCurrentScope,
redirect,
setTempRedirect,
tempRedirect,
currentScope,
])

if (!currentUser || userScopes.length === 0) {
return <BootPage />
Expand Down
Loading