类别

在扑腾中实现Google登录

在Firebase中设置所有内容 for Google Sign-In in flutter, we are now ready to code. First of all, in the pubspec.yaml file, we will add the following dependencies by searching from pub.dev.:

(一世)。 firebase_auth.

(ii)。 google_sign_in.

在扑腾中实现Google登录

在扑动中完成了登录设计后,我们将为Google登录实施以下内容。

(一世)。步骤我:

为Google登录验证的代码创建一个单独的文件。

在第一步中,导入所需的包并保存信息。

(ii)。第二步:

Instance for the FirebaseAuth and GoogleSignIn is kept in auth and googleSignIn respectively.

后that, the function is created googleSignInProcess() which will handle the google sign in process.

So, inside the function, the variable is created of the type GoogleSignInAccount which is named googleSignInAccount.

Now googleSignInAccount will be available to us if and only if there is the process handled by googleSignIn which is the signIn() method. Since the process will happen in the future, the user have to wait, so the await is used.

(iii)。步骤三世:

Now if the Sign-In is successful we will get some values. So, we will use if-else statements to get the response values.

In the first statement, we will check for the value if it is not null.

现在,如果该值不为NULL,我们将获得Google身份验证。

The authentication comes from googleSignInAccount and authentication gives us data.

Since the process takes time await is used.

(iv)。第四步:

因此,在进程完成后,我们会收到我们可以使用Google帐户的身份验证。

The credential comes from GoogleAuthProvider. So, we have to get the credential from GoogleAuthProvider which will gives us id token and access token.

Both will come from googleSignInAccount.

(v)。步骤v:

Now we have to call firebaseAuth for Sign-In. We have to now sign in the user with credential. So, we use signInWithCredential which gives us the value.

We insert the value in result which is of the type auth Result. We will get the user who is currently logging in from the result. Now  the user will be of the type User. With the currentUser, we get the current user.

要确保用户可用,我们会打印用户ID。

(vi)。步骤vi:

最后如果进程成功完成,我们将将来的值设置为true,这将确认没有任何问题,登录是成功的。否则我们将打印错误。

结论

我们在Flutter应用程序中完成了Google登录的系列。我们开始了在Firebase中设置项目,然后增加支持安卓 & iOS.平台。最后,在这部分中,我们展示了一种基本的编码机制。

希望这是一个有用的系列。

了解如何在这里扑振中构建费用管理应用。