Skip to content

Commit a3a5873

Browse files
committed
Fix embed auth restructuring
1 parent 2ac4132 commit a3a5873

File tree

2 files changed

+39
-38
lines changed

2 files changed

+39
-38
lines changed

web/src/components/common/App.tsx

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,11 @@ import { BsFillPatchQuestionFill } from "react-icons/bs";
2727

2828
import { useSelector } from 'react-redux'
2929
import { login, register } from '../../state/auth/reducer'
30-
import { dispatch, logoutState } from '../../state/dispatch'
31-
import {auth as authModule} from '../../app/api'
30+
import { dispatch, logoutState, resetState } from '../../state/dispatch'
31+
import {auth as authModule, setAxiosJwt} from '../../app/api'
3232
import Auth from './Auth'
3333
import _, { attempt } from 'lodash'
34-
import { updateAppMode, setAnalystMode, setDRMode } from '../../state/settings/reducer'
34+
import { updateAppMode, setAnalystMode, setDRMode, setCurrentEmail } from '../../state/settings/reducer'
3535
import { DevToolsBox } from '../devtools';
3636
import { RootState } from '../../state/store'
3737
import { getPlatformShortcut } from '../../helpers/platformCustomization'
@@ -42,7 +42,7 @@ import { setBillingInfo } from '../../state/billing/reducer'
4242
import { useGetUserStateQuery } from '../../app/api/userStateApi'
4343
import { SupportButton } from './Support'
4444
import { Markdown } from './Markdown'
45-
import { setMinusxMode, toggleMinusXRoot } from '../../app/rpc'
45+
import { getMXToken, setMinusxMode, toggleMinusXRoot } from '../../app/rpc'
4646
import { configs } from '../../constants'
4747
import { abortPlan, startNewThread, updateThreadID } from '../../state/chat/reducer'
4848

@@ -401,6 +401,40 @@ const AppBody = forwardRef((_props, ref) => {
401401
// borderLeftColor={"minusxBW.500"} borderRightColor={"transparent"}
402402
// borderTopColor={"minusxBW.500"} borderBottomColor={"minusxBW.500"}/>
403403
// )
404+
405+
useEffect(() => {
406+
let tries = 1
407+
const checkToken = async () => {
408+
const mx_token = await getMXToken()
409+
410+
if (mx_token) {
411+
try {
412+
const embedAuthResult = await authModule.embedAuth(mx_token)
413+
const { session_jwt, profile_id, email } = embedAuthResult
414+
if (email != currentEmail) {
415+
resetState()
416+
}
417+
dispatch(login({
418+
session_jwt,
419+
profile_id,
420+
email,
421+
}))
422+
setAxiosJwt(session_jwt)
423+
dispatch(setCurrentEmail(email))
424+
return
425+
} catch (error) {
426+
console.error('Failed to authenticate embed token:', error)
427+
}
428+
}
429+
if (tries < 3) {
430+
setTimeout(checkToken, (tries++) * 1000)
431+
}
432+
}
433+
if (isEmbedded) {
434+
checkToken()
435+
}
436+
}, [])
437+
404438
if (!auth.is_authenticated) {
405439
return <Auth />
406440
}

web/src/components/common/Auth.tsx

Lines changed: 1 addition & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -179,40 +179,7 @@ const Auth = () => {
179179
const isOTPMode = authJWT ? true : false
180180
const helperMessage = useAppStore((state) => state.helperMessage)?.split('---')[1] || "Welcome to MinusX! You can ask us anything related to your data, and our agents will take care of the rest!"
181181
const isEmbedded = getParsedIframeInfo().isEmbedded as unknown === 'true'
182-
const currentEmail = useSelector((state: RootState) => state.settings.currentEmail)
183-
184-
useEffect(() => {
185-
let tries = 1
186-
const checkToken = async () => {
187-
const mx_token = await getMXToken()
188-
189-
if (mx_token) {
190-
try {
191-
const embedAuthResult = await authModule.embedAuth(mx_token)
192-
const { session_jwt, profile_id, email } = embedAuthResult
193-
if (email != currentEmail) {
194-
resetState()
195-
}
196-
dispatch(login({
197-
session_jwt,
198-
profile_id,
199-
email,
200-
}))
201-
setAxiosJwt(session_jwt)
202-
dispatch(setCurrentEmail(email))
203-
return
204-
} catch (error) {
205-
console.error('Failed to authenticate embed token:', error)
206-
}
207-
}
208-
if (tries < 3) {
209-
setTimeout(checkToken, (tries++) * 1000)
210-
}
211-
}
212-
if (isEmbedded) {
213-
checkToken()
214-
}
215-
}, [])
182+
const currentEmail = useSelector((state: RootState) => state.settings.currentEmail)
216183

217184

218185
useEffect(() => {

0 commit comments

Comments
 (0)