@@ -2,7 +2,7 @@ import React, { useState, useRef, useEffect } from 'react';
22import { Button , Input , Box , VStack , Image , CloseButton , HStack , Text , Progress } from '@chakra-ui/react' ;
33import { Select , CreatableSelect } from "chakra-react-select" ;
44import { login } from '../../state/auth/reducer'
5- import { dispatch } from '../../state/dispatch'
5+ import { dispatch , logoutState , resetState } from '../../state/dispatch'
66import { auth , auth as authModule } from '../../app/api'
77import { useSelector } from 'react-redux' ;
88import logo from '../../assets/img/logo.svg'
@@ -14,10 +14,11 @@ import { TelemetryToggle } from '../devtools/Settings';
1414import { getParsedIframeInfo } from '../../helpers/origin' ;
1515import { toast } from '../../app/toast' ;
1616import { get } from 'lodash' ;
17- import { setMinusxMode } from '../../app/rpc' ;
17+ import { getMXToken , setMinusxMode } from '../../app/rpc' ;
1818import { RootState } from '../../state/store' ;
1919import { getApp } from '../../helpers/app' ;
2020import { Markdown } from './Markdown' ;
21+ import { setCurrentEmail } from '../../state/settings/reducer' ;
2122
2223
2324const useAppStore = getApp ( ) . useStore ( )
@@ -177,6 +178,35 @@ const Auth = () => {
177178 const [ discoverySource , setDiscoverySource ] = useState ( "" ) ;
178179 const isOTPMode = authJWT ? true : false
179180 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!"
181+ const isEmbedded = getParsedIframeInfo ( ) . isEmbedded as unknown === 'true'
182+ const currentEmail = useSelector ( ( state : RootState ) => state . settings . currentEmail )
183+
184+ useEffect ( ( ) => {
185+ const checkToken = async ( ) => {
186+ const mx_token = await getMXToken ( )
187+
188+ if ( mx_token ) {
189+ try {
190+ const embedAuthResult = await authModule . embedAuth ( mx_token )
191+ const { session_jwt, profile_id, email } = embedAuthResult
192+ if ( email != currentEmail ) {
193+ resetState ( )
194+ }
195+ dispatch ( login ( {
196+ session_jwt,
197+ profile_id,
198+ email,
199+ } ) )
200+ dispatch ( setCurrentEmail ( email ) )
201+ } catch ( error ) {
202+ console . error ( 'Failed to authenticate embed token:' , error )
203+ }
204+ }
205+ }
206+ if ( isEmbedded ) {
207+ checkToken ( )
208+ }
209+ } , [ ] )
180210
181211
182212 useEffect ( ( ) => {
@@ -217,6 +247,7 @@ const Auth = () => {
217247 profile_id,
218248 email,
219249 } ) )
250+ dispatch ( setCurrentEmail ( email ) )
220251 if ( is_new_user ) {
221252 captureEvent ( GLOBAL_EVENTS . user_signup , { email, profile_id, discoverySource } )
222253 } else {
@@ -330,12 +361,13 @@ const Auth = () => {
330361 borderWidth = { 1.5 } borderLeftColor = { "minusxBW.500" } >
331362 < Image src = { logo } alt = "MinusX" maxWidth = '150px' />
332363 < VStack spacing = { 4 } mt = { 5 } position = { "relative" } >
364+ { isEmbedded && < Text > Logging you in...</ Text > }
333365 < Input
334366 type = "email"
335367 placeholder = "Enter work email ID"
336368 aria-label = "Enter work email ID"
337369 value = { email }
338- disabled = { isOTPMode ? true : false }
370+ disabled = { isEmbedded ? true : isOTPMode ? true : false }
339371 onChange = { ( e ) => setEmail ( e . target . value ) }
340372 // just trigger the handleSignin function when enter is pressed in this input as well
341373 onKeyUp = { ( e ) => {
0 commit comments