Skip to content

Commit

Permalink
fix: refresh token
Browse files Browse the repository at this point in the history
  • Loading branch information
hashtagnulla committed Oct 7, 2024
1 parent 765752d commit 4cc5bb3
Show file tree
Hide file tree
Showing 9 changed files with 270 additions and 401 deletions.
39 changes: 31 additions & 8 deletions apps/sensenet/src/context/sn-auth-repository-provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,21 @@ export function SnAuthRepositoryProvider({ children }: { children: React.ReactNo
config: null,
})
const repoFromUrl = useQuery().get('repoUrl')
const configString = window.localStorage.getItem(authConfigKey)
const cancelledLogin = useQuery().get('cancelledLogin')
const [configString, setConfigString] = useState<any>()
const [authServerUrl, setAuthServerUrl] = useState()

const clearState = useCallback(() => setAuthState({ repoUrl: '', config: null }), [])

useEffect(() => {
if (cancelledLogin) {
window.localStorage.removeItem(authConfigKey)
setAuthState((oldState) => ({ ...oldState, repoUrl: '' }))
} else {
setConfigString(window.localStorage.getItem(authConfigKey))
}
}, [cancelledLogin])

useEffect(() => {
if (configString) {
const prevAuthConfig = JSON.parse(configString)
Expand Down Expand Up @@ -56,8 +66,15 @@ export function SnAuthRepositoryProvider({ children }: { children: React.ReactNo
try {
setIsLoginInProgress(true)
const config = await getAuthConfig(authState.repoUrl)
window.localStorage.setItem(authConfigKey, JSON.stringify(config))
setAuthState((oldState) => ({ ...oldState, config: config.userManagerSettings }))
if (config.authServerSettings.type === 'SNAuth') {
window.localStorage.setItem(authConfigKey, JSON.stringify(config))
setConfigString(window.localStorage.getItem(authConfigKey))
setAuthState((oldState) => ({ ...oldState, config: config.userManagerSettings }))
} else {
logger.error({ message: 'Incompatible authentication server type' })
window.localStorage.removeItem(authConfigKey)
setAuthState((oldState) => ({ ...oldState, repoUrl: '' }))
}
} catch (error) {
logger.warning({ data: error, message: `Couldn't connect to ${authState.repoUrl}` })
window.localStorage.removeItem(authConfigKey)
Expand All @@ -71,7 +88,7 @@ export function SnAuthRepositoryProvider({ children }: { children: React.ReactNo
getConfig()
}, [getConfig])

if (!authState.config || !authState.repoUrl) {
if (!authState.config || !authState.repoUrl || !authServerUrl) {
return (
<div className={globalClasses.full}>
<CssBaseline />
Expand Down Expand Up @@ -101,6 +118,13 @@ export function SnAuthRepositoryProvider({ children }: { children: React.ReactNo
repoUrl={authState.repoUrl}
snAuthConfiguration={{
callbackUri: '/authentication/callback',
}}
eventCallbacks={{
onLogout() {
setConfigString(null)
window.localStorage.removeItem(authConfigKey)
clearState()
},
}}>
<RepoProvider repoUrl={authState.repoUrl} authServerUrl={authServerUrl} clearAuthState={clearState}>
{children}
Expand Down Expand Up @@ -165,18 +189,17 @@ const RepoProvider = ({
useEffect(() => {
;(async () => {
const configString = window.localStorage.getItem(authConfigKey)
if (!user && !isLoading && !accessToken && configString) {
if (!user && !isLoading && !accessToken && authServerUrl && configString) {
try {
await externalLogin()
} catch (error) {
const config = JSON.parse(configString)
logger.error({ data: error, message: `Couldn't connect to ${config.authority}` })
logger.error({ data: error, message: `Couldn't connect to ${authServerUrl}` })
window.localStorage.removeItem(authConfigKey)
clearAuthState()
}
}
})()
}, [clearAuthState, logger, externalLogin, logout, user, isLoading, accessToken])
}, [clearAuthState, logger, externalLogin, logout, user, isLoading, accessToken, authServerUrl])

if (!user || !repo) {
return null
Expand Down
6 changes: 5 additions & 1 deletion packages/sn-auth-react/src/components/auth-routes.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { memo, ReactNode } from 'react'
import React, { memo, ReactNode, useEffect } from 'react'
import { Authenticating } from './authenticating'

export type AuthRoutesProps = {
Expand All @@ -8,6 +8,10 @@ export type AuthRoutesProps = {
}

const AuthRoutesComponent = ({ callbackUri, children, currentPath }: AuthRoutesProps) => {
useEffect(() => {
console.log(currentPath)
}, [currentPath])

switch (currentPath) {
case callbackUri:
return <Authenticating />
Expand Down
Loading

0 comments on commit 4cc5bb3

Please sign in to comment.