Firebase邮箱链接认证:Compose导航组件参数配置咨询
Firebase邮箱认证DeepLink参数配置与用户登录实现
1. 配置带查询参数的DeepLink URI模式
Firebase邮箱验证链接默认包含mode、oobCode、apiKey等核心参数,需要在uriPattern中明确这些参数占位符,确保能捕获链接中的数据:
composable( route = "auth_verify_email", deepLinks = listOf( navDeepLink { // 替换<APPLICATION_ID>为你的Firebase应用ID uriPattern = "https://<APPLICATION_ID>.firebaseapp.com/__/auth/action?mode={mode}&oobCode={oobCode}&apiKey={apiKey}" action = Intent.ACTION_VIEW } ), // 2. 定义对应参数类型 arguments = listOf( navArgument("mode") { type = NavType.StringType nullable = true }, navArgument("oobCode") { type = NavType.StringType nullable = true }, navArgument("apiKey") { type = NavType.StringType nullable = true } ) ) { backStackEntry -> // 3. 获取参数并执行认证逻辑 val mode = backStackEntry.arguments?.getString("mode") val oobCode = backStackEntry.arguments?.getString("oobCode") oobCode?.let { code -> when (mode) { "verifyEmail" -> { FirebaseAuth.getInstance().applyActionCode(code) .addOnCompleteListener { task -> if (task.isSuccessful) { // 邮箱验证成功:可刷新用户信息、跳转首页或完成登录 FirebaseAuth.getInstance().currentUser?.reload() } else { // 处理验证失败逻辑(如链接过期、无效) } } } // 如需支持其他认证动作(如重置密码),可扩展对应mode分支 } } }
关键注意事项
- 确保Firebase控制台的认证>模板>邮箱验证中,回调URL与配置的
uriPattern路径一致(默认路径为__/auth/action)。 - 设置
nullable = true避免因链接参数缺失导致崩溃,实际业务中需先判断参数非空再执行逻辑。 - 调用
applyActionCode完成邮箱验证后,用户的isEmailVerified状态会自动更新,可通过currentUser获取最新状态。
内容的提问来源于stack exchange,提问作者Marco Antonio
相关产品推荐
相关产品推荐

