使用auth0_flutter登出时触发a0.authentication_canceled异常求助
我正在使用auth0_flutter实现Auth0登出功能,触发异常:a0.authentication_canceled: The user closed the browser app so the logout was cancelled.
异常出现前日志显示的登出URI如下:
D/LogoutManager(25194): Using the following Logout URI:
AUTH0_DOMAIN/v2/logout?auth0Client=Auth0Client%3D&returnTo=AUTH0_REDIRECT_URI&client_id=AUTH0_CLIENT_ID
登出实现代码:
await _auth0 .webAuthentication(scheme: _environmentParamsService.getScheme()) .logout( returnTo: _environmentParamsService.getAuth0RedirectUrl() );
登录实现代码:
await _auth0.webAuthentication( scheme: _environmentParamsService.getScheme() ).login( audience: _environmentParamsService.getAudience(), parameters: { 'prompt': 'login', } );
检查returnTo地址的控制台配置
确保Auth0控制台的「Allowed Logout URLs」列表中已添加你设置的returnTo地址,且地址完全匹配(包括自定义Scheme,比如your-app-scheme://callback这类格式,不能有拼写、大小写错误)。验证Scheme的全局一致性
登出时使用的Scheme必须和登录时完全一致,同时要确认Android的AndroidManifest.xml或iOS的Info.plist里已正确配置该Scheme的跳转规则,保证浏览器能正常跳回你的应用。添加参数强制清除全局会话
在登出调用中加入parameters参数,强制触发全局登出,减少浏览器跳转异常概率:await _auth0 .webAuthentication(scheme: _environmentParamsService.getScheme()) .logout( returnTo: _environmentParamsService.getAuth0RedirectUrl(), parameters: {'federated': 'true'} );测试主流浏览器的兼容性
部分设备的默认浏览器可能拦截自定义Scheme跳转,建议测试Chrome、Safari等主流浏览器,或者使用Auth0 Universal Login的默认配置。捕获异常做兼容处理
如果仍无法避免该异常,可以捕获错误并做兼容(实际会话可能已清除,仅跳转失败触发异常):try { await _auth0 .webAuthentication(scheme: _environmentParamsService.getScheme()) .logout( returnTo: _environmentParamsService.getAuth0RedirectUrl() ); } on WebAuthenticationException catch (e) { if (e.message.contains('authentication_canceled')) { // 自定义逻辑:清除本地缓存、提示用户等 print('登出会话已清除'); } else { rethrow; } }
内容的提问来源于stack exchange,提问作者BorkoC

