Enhance AuthContext component by adding support for processing authentication codes from URL parameters. Introduce a reference to track processed auth codes, ensuring proper handling of new logins and session management. Update modal visibility logic for unauthorized access and improve loading state handling.
This commit is contained in:
parent
938bea5342
commit
48ff9b83e8
@ -69,6 +69,7 @@ const AuthProvider = ({ children }) => {
|
|||||||
const [userProfile, setUserProfile] = useState(null)
|
const [userProfile, setUserProfile] = useState(null)
|
||||||
const [profileImageUrl, setProfileImageUrl] = useState(null)
|
const [profileImageUrl, setProfileImageUrl] = useState(null)
|
||||||
const profileImageUrlRef = useRef(null)
|
const profileImageUrlRef = useRef(null)
|
||||||
|
const processedAuthCodeRef = useRef(null)
|
||||||
const [showSessionExpiredModal, setShowSessionExpiredModal] = useState(false)
|
const [showSessionExpiredModal, setShowSessionExpiredModal] = useState(false)
|
||||||
const [showUnauthorizedModal, setShowUnauthorizedModal] = useState(false)
|
const [showUnauthorizedModal, setShowUnauthorizedModal] = useState(false)
|
||||||
const [showAuthErrorModal, setShowAuthErrorModal] = useState(false)
|
const [showAuthErrorModal, setShowAuthErrorModal] = useState(false)
|
||||||
@ -165,6 +166,8 @@ const AuthProvider = ({ children }) => {
|
|||||||
// Read token from cookies (web) or electron session storage if present
|
// Read token from cookies (web) or electron session storage if present
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
let cancelled = false
|
let cancelled = false
|
||||||
|
const authCode =
|
||||||
|
new URLSearchParams(location.search).get('authCode') || null
|
||||||
|
|
||||||
const load = async () => {
|
const load = async () => {
|
||||||
try {
|
try {
|
||||||
@ -227,7 +230,9 @@ const AuthProvider = ({ children }) => {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (location.pathname === '/applaunch') {
|
// An auth code represents a newly completed login and must take precedence
|
||||||
|
// over any existing persisted session.
|
||||||
|
if (location.pathname === '/applaunch' || authCode != null) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -431,6 +436,7 @@ const AuthProvider = ({ children }) => {
|
|||||||
setExpiresAt(nextExpiresAt)
|
setExpiresAt(nextExpiresAt)
|
||||||
setUserProfile(nextUser)
|
setUserProfile(nextUser)
|
||||||
setAuthenticated(true)
|
setAuthenticated(true)
|
||||||
|
setShowUnauthorizedModal(false)
|
||||||
|
|
||||||
// Persist session (cookies on web, electron storage on desktop)
|
// Persist session (cookies on web, electron storage on desktop)
|
||||||
const persisted = await persistSession({
|
const persisted = await persistSession({
|
||||||
@ -469,6 +475,7 @@ const AuthProvider = ({ children }) => {
|
|||||||
setAuthenticated(false)
|
setAuthenticated(false)
|
||||||
} finally {
|
} finally {
|
||||||
setLoading(false)
|
setLoading(false)
|
||||||
|
setRetreivedTokenFromCookies(true)
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
[
|
[
|
||||||
@ -723,7 +730,11 @@ const AuthProvider = ({ children }) => {
|
|||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const authCode =
|
const authCode =
|
||||||
new URLSearchParams(location.search).get('authCode') || null
|
new URLSearchParams(location.search).get('authCode') || null
|
||||||
if (authCode != null) {
|
if (
|
||||||
|
authCode != null &&
|
||||||
|
processedAuthCodeRef.current !== authCode
|
||||||
|
) {
|
||||||
|
processedAuthCodeRef.current = authCode
|
||||||
getLoginToken(authCode)
|
getLoginToken(authCode)
|
||||||
} else if (
|
} else if (
|
||||||
token == null &&
|
token == null &&
|
||||||
@ -800,7 +811,12 @@ const AuthProvider = ({ children }) => {
|
|||||||
Please log in to continue
|
Please log in to continue
|
||||||
</Space>
|
</Space>
|
||||||
}
|
}
|
||||||
open={showUnauthorizedModal && !loading && !showAuthErrorModal}
|
open={
|
||||||
|
showUnauthorizedModal &&
|
||||||
|
!authenticated &&
|
||||||
|
!loading &&
|
||||||
|
!showAuthErrorModal
|
||||||
|
}
|
||||||
onOk={() => {
|
onOk={() => {
|
||||||
setShowUnauthorizedModal(false)
|
setShowUnauthorizedModal(false)
|
||||||
loginWithSSO()
|
loginWithSSO()
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user