@@ -10,6 +10,8 @@ import { useGuardButtonState } from '../../_utils/context'
1010
1111import { MfaBusinessAction , useMfaBusinessRequest } from '../businessRequest'
1212
13+ import { AwsFaceLivenessDetector } from './AwsFaceLivenessDetector'
14+
1315const { useEffect, useState } = React
1416
1517// ============================================
@@ -19,6 +21,12 @@ interface LivenessSessionResponse {
1921 sessionId : string
2022 region : string
2123 expireTime ?: number
24+ credentials ?: {
25+ AccessKeyId : string
26+ SecretAccessKey : string
27+ SessionToken : string
28+ Expiration ?: Date
29+ }
2230}
2331
2432// ============================================
@@ -49,6 +57,11 @@ export const MFAFace = (props: any) => {
4957 null
5058 )
5159 const [ livenessRegion , setLivenessRegion ] = useState < string > ( '' )
60+ const [ livenessCredentials , setLivenessCredentials ] = useState < {
61+ accessKeyId : string
62+ secretAccessKey : string
63+ sessionToken : string
64+ } | null > ( null )
5265 const [ isLoadingSession , setIsLoadingSession ] = useState ( false )
5366 const [ livenessResult , setLivenessResult ] = useState < LivenessResult | null > (
5467 null
@@ -63,61 +76,100 @@ export const MFAFace = (props: any) => {
6376 * 创建 AWS 活体检测 Session
6477 */
6578 const createLivenessSession = async ( ) => {
79+ console . log (
80+ '[FaceLiveness] 开始创建 session, mfaToken:' ,
81+ props . initData . mfaToken
82+ )
6683 setIsLoadingSession ( true )
6784 setLivenessResult ( null )
6885
6986 try {
7087 const result = await getLivenessSessionRequest ( {
7188 mfaToken : props . initData . mfaToken
7289 } )
90+ console . log ( '[FaceLiveness] 创建 session 响应:' , result )
7391
7492 // 适配后端返回格式:可能是 result.data 或直接在 result 上
7593 const responseData = result . data || result
94+ console . log ( '[FaceLiveness] 解析后的 responseData:' , responseData )
95+
7696 const sessionData : LivenessSessionResponse = responseData
7797
98+ console . log ( '[FaceLiveness] sessionId:' , sessionData . sessionId )
99+ console . log ( '[FaceLiveness] region:' , sessionData . region )
100+ console . log ( '[FaceLiveness] credentials:' , sessionData . credentials )
101+
78102 setLivenessSessionId ( sessionData . sessionId )
79103 setLivenessRegion ( sessionData . region )
104+
105+ // 保存 AWS 临时凭证
106+ if ( sessionData . credentials ) {
107+ console . log ( '[FaceLiveness] 保存临时凭证' )
108+ setLivenessCredentials ( {
109+ accessKeyId : sessionData . credentials . AccessKeyId ,
110+ secretAccessKey : sessionData . credentials . SecretAccessKey ,
111+ sessionToken : sessionData . credentials . SessionToken
112+ } )
113+ }
80114 } catch ( e : any ) {
81- console . error ( 'Failed to create liveness session' , e )
115+ console . error ( '[FaceLiveness] 创建 session 失败: ' , e )
82116 message . error ( e . message || '创建活体检测会话失败' )
83117 }
84118
85119 setIsLoadingSession ( false )
120+ console . log ( '[FaceLiveness] 创建 session 结束' )
86121 }
87122
88123 /**
89124 * 获取活体检测结果
90125 */
91126 const fetchLivenessResult = async ( ) => {
92- if ( ! livenessSessionId ) return
127+ if ( ! livenessSessionId ) {
128+ console . log ( '[FaceLiveness] 没有 sessionId,跳过获取结果' )
129+ return
130+ }
93131
132+ console . log (
133+ '[FaceLiveness] 开始获取检测结果, sessionId:' ,
134+ livenessSessionId
135+ )
94136 spinChange ( true )
95137
96138 try {
97139 const result = await getLivenessResultRequest ( {
98140 sessionId : livenessSessionId ,
99141 mfaToken : props . initData . mfaToken
100142 } )
143+ console . log ( '[FaceLiveness] 获取检测结果响应:' , result )
101144
102145 // 适配后端返回格式
103146 const responseData = result . data || result
147+ console . log ( '[FaceLiveness] 解析后的 livenessData:' , responseData )
148+
104149 const livenessData : LivenessResult = responseData
105150 setLivenessResult ( livenessData )
106151
152+ console . log ( '[FaceLiveness] isLive:' , livenessData . isLive )
153+ console . log ( '[FaceLiveness] confidence:' , livenessData . confidence )
154+ console . log ( '[FaceLiveness] status:' , livenessData . status )
155+
107156 // 根据结果处理登录
108157 if ( livenessData . isLive ) {
158+ console . log ( '[FaceLiveness] 活体检测通过,调用 mfaLogin' )
109159 // 活体检测通过,调用验证接口
110160 props . mfaLogin ( 200 , responseData )
111161 } else {
162+ console . log ( '[FaceLiveness] 活体检测未通过' )
112163 // 未通过,显示重试
113164 message . error ( livenessData . message || '活体检测未通过' )
114165 }
115166 } catch ( e : any ) {
116- console . error ( 'Failed to get liveness result ' , e )
167+ console . error ( '[FaceLiveness] 获取检测结果失败: ' , e )
117168 message . error ( e . message || '获取检测结果失败' )
118169 }
119170
120171 spinChange ( false )
172+ console . log ( '[FaceLiveness] 获取检测结果结束' )
121173 }
122174
123175 /**
@@ -140,13 +192,6 @@ export const MFAFace = (props: any) => {
140192 // 渲染 AWS 活体检测组件
141193 // ============================================
142194 const renderAwsLivenessDetector = ( ) => {
143- // 动态导入 AWS 组件(避免影响原有打包)
144- const AwsFaceLivenessDetector = React . lazy ( ( ) =>
145- import ( './AwsFaceLivenessDetector' ) . then ( module => ( {
146- default : module . AwsFaceLivenessDetector
147- } ) )
148- )
149-
150195 if ( isLoadingSession ) {
151196 return (
152197 < div
@@ -157,35 +202,27 @@ export const MFAFace = (props: any) => {
157202 )
158203 }
159204
160- // if (!livenessSessionId) {
161- // return (
162- // <div style={{ textAlign: 'center', padding: '2rem' }}>
163- // <p>无法创建检测会话</p>
164- // <SubmitButton
165- // onClick={createLivenessSession}
166- // text="重试"
167- // className="mfa-face"
168- // />
169- // </div>
170- // )
171- // }
205+ if ( ! livenessSessionId ) {
206+ return (
207+ < div style = { { textAlign : 'center' , padding : '2rem' } } >
208+ < p > 无法创建检测会话</ p >
209+ < SubmitButton
210+ onClick = { createLivenessSession }
211+ text = "重试"
212+ className = "mfa-face"
213+ />
214+ </ div >
215+ )
216+ }
172217
173218 return (
174- < React . Suspense
175- fallback = { < div className = "authing-g2-loading" > 加载组件...</ div > }
176- >
177- < AwsFaceLivenessDetector
178- sessionId = { livenessSessionId || '' }
179- region = { livenessRegion }
180- onAnalysisComplete = { handleLivenessAnalysisComplete }
181- onError = { handleLivenessError }
182- credentials = { {
183- accessKeyId : '' ,
184- secretAccessKey : '' ,
185- sessionToken : ''
186- } }
187- />
188- </ React . Suspense >
219+ < AwsFaceLivenessDetector
220+ sessionId = { livenessSessionId || '' }
221+ region = { livenessRegion }
222+ onAnalysisComplete = { handleLivenessAnalysisComplete }
223+ onError = { handleLivenessError }
224+ credentials = { livenessCredentials || undefined }
225+ />
189226 )
190227 }
191228
0 commit comments