kotlin 我如何重新验证一个由firebase adminsdk创建的用户

5kgi1eie  于 2023-04-21  发布在  Kotlin
关注(0)|答案(2)|浏览(129)

我已经使用admin SDK和Google身份提供程序将所有的用户创建和身份验证移动到Firebase。我可以很好地创建用户,并且我正在添加密码(还有我用于创建密码的代码在那里,我不知道如何重新验证用户。我已经看到了一些使用EmailAuthProvider的答案,但是该类不存在。我的创建用户代码

fun createUser(request: HttpServletRequest?, response: HttpServletResponse?, createUserRequest: CreateUserRequest): User {

        val hashedPassword = PasswordUtil.hashPassword(createUserRequest.password.trim())

        val user: User = Mapper.convert(createUserRequest)
        val trimmedEmail = user.email.trim()
        user.email = trimmedEmail

        usersRepo.save(user)

        CoroutineScope(Dispatchers.IO).launch {
            FirestoreClient.getFirestore().collection("users").document(user.id.toString()).set(user)
        }

        val auth = FirebaseAuth.getInstance()

        val request = CreateRequest()
            .setUid(user.id.toString())
            .setDisplayName(user.username)
            .setEmailVerified(false)
            .setEmail(user.email)
            .setPassword(createUserRequest.password)

        auth.createUser(request)

        val password = Password()
        password.password = hashedPassword
        password.user = user
        passwordsRepo.save(password)

        CoroutineScope(Dispatchers.IO).launch {
            try {
                marketplace.generateUsersWallets(user)
            } catch (e: Exception) {
                e.printStackTrace()
            }
        }

        val link = FirebaseAuth.getInstance().generateEmailVerificationLink(user.email)
        awsSesService.sendEmailVerification(user.email, link)

        return user
    }

我使用的软件包是

//firebase
    implementation("com.google.firebase:firebase-admin:9.1.1")
    // Import the BoM for the Firebase platform
    implementation(platform("com.google.firebase:firebase-bom:30.4.1"))

    implementation("com.google.firebase:firebase-firestore-ktx")

FirebaseAuth可以很好地注册,但我如何重新登录?谢谢

bpzcxfmw

bpzcxfmw1#

好吧,我基本上找到了答案,我不知道为什么谷歌有这个埋葬了。基本上管理sdk的不能认证,但谷歌身份休息API可以。https://cloud.google.com/identity-platform/docs/use-rest-api#section-sign-in-email-password
现在奇怪的是,没有办法像在firebase SDK中那样登录并获取自定义的JWT令牌,而是获得一个带有IdToken和Refresh令牌的有效负载。现在,由于自定义令牌的全部意义是让客户端使用它们登录,然后发送id令牌,因此您必须进行两次循环,使用email/password登录,然后,如果通过创建一个新的自定义令牌。我会后完成的代码完成时

tsm1rwdh

tsm1rwdh2#

工作代码,唯一缺少的部分是进行REST API调用,您可以做任何事情,但点应该很清楚

/**
 * Logs in a user if the password hash is equal
 */
@Throws(IllegalArgumentException::class, FirebaseAuthException::class)
fun login(request: HttpServletRequest, response: HttpServletResponse, loginUserRequest: LoginUserRequest): User? = runBlocking {

    //find the user in google
    val userRecord = FirebaseAuth.getInstance().getUserByEmail(loginUserRequest.email)

    if (userRecord != null) {
        //find the user by our UUID
        val user = usersRepo.findUserById(userRecord.uid.toUUID())

        if (user != null) { //log them in

            try {

                googleIdentityService.apiClient.signInWithEmailAndPassword(SignInRequest(userRecord.email.toString(), loginUserRequest.password))

                val customToken = FirebaseAuth.getInstance().createCustomToken(user.id.toString())
                response.setHeader("JWT", customToken)

                return@runBlocking user

            } catch (e: Exception) {
                e.printStackTrace()
            }
        }
    }
    throw ResponseStatusException(HttpStatus.NOT_FOUND, "invalid email or password")
}

相关问题