使用angular / spring-boot通过firebase生成验证邮件

时间:2019-05-16 13:42:11

标签: angular firebase spring-boot firebase-authentication

我正在开发一个带有Firebase(仅仅身份验证),角度启动和弹簧启动的应用。创建用户后,我想发送电子邮件验证。我有两个通过提供电子邮件和密码来创建用户的选项。

  1. 通过注册页面注册(任何人)
  2. 管理员可以在登录系统后创建用户。

Angular 7

SignUp(email, password) {
    return this.afAuth.auth.createUserWithEmailAndPassword(email, password)
    .then((result) => {
        // You have been successfully registered!"  
        this.afAuth.auth.currentUser.sendEmailVerification()
            .then(() => {
                // Please verify your email
            })
    }).catch((error) => {
        // Error while registering a user 
    })
}
  

如果创建了新帐户,则用户将自动登录。 -Firebase Ref

上面的代码以当前用户的身份返回用户数据。 因此第二种情况失败(即使管理员登录,一旦他创建了用户,它也会自动将管理员状态更改为new用户状态)。因此,我通过后端创建了一个新用户。

Spring Boot

CreateRequest request = new CreateRequest().setEmail(user.getEmail()).setPassword(user.getPassword());
UserRecord userRecord = FirebaseAuth.getInstance().createUser(request);

这成功创建了一个用户,而没有更改登录用户的状态。

是否有任何方法(通过后端或前端)将电子邮件验证发送到第二种情况?预先感谢。

1 个答案:

答案 0 :(得分:0)

好吧,我更新了后端{Spring-boot}中具有generateEmailVerificationLink();的最新依赖项

<dependency>
    <groupId>com.google.firebase</groupId>
    <artifactId>firebase-admin</artifactId>
    <version>6.8.1</version>
</dependency>

因此可以生成验证邮件并通过自定义邮件服务发送。 Generate verification link

// Generating verification link with the help of firebase
String link=FirebaseAuth.getInstance().generateEmailVerificationLink(user.getEmail());

// Sending the link through custome mail service
emailService.sendMail("Your mail id", user.getEmail(), "Verfication email", link);