Flutter-可以在没有Firebase的情况下使用Google登录吗?

时间:2019-08-16 21:50:13

标签: firebase authentication flutter dart

pub.dev上有google_sign_in软件包,它提供了用于将应用程序启动点设置为https://firebase.google.com/?platform=android ...

的链接。

谢谢!

3 个答案:

答案 0 :(得分:4)

是的!可以。

  1. 在云控制台中创建项目。
  2. 创建OAuth 2.0 clientId(通过使用程序包名称对其进行限制)
  3. 下载JSON并将其放置在android-> app文件夹中,就这样。
  4. 按照google_sign_in软件包中的步骤进行操作,而无需进入Firebase

    GoogleSignIn googleSignIn = GoogleSignIn( clientId:“ xxxx.apps.googleusercontent.com”);

clientId是可选的-但在Flutter Web中是必需的

也请参考flutter web google sign in without firebase

答案 1 :(得分:0)

有很多方法可以通过Google进行身份验证。这是其中之一,无需使用firebase。

  

1-在Google控制台中注册您的应用

您必须按照以下步骤通过Google设置Android / iOS应用。

Android集成 要访问Google登录,您需要确保注册您的应用程序。 您需要启用Google People API。

iOS集成 首先,注册您的应用程序。 打开Xcode。您必须将其粘贴到Xcode中才能正确注册GoogleServices-Info.plist

从文件管理器中选择GoogleServices-Info.plist,然后将该文件拖到Runner目录[my_project]/ios/Runner/GoogleServices-Info.plist中。 将出现一个对话框,要求您选择目标,然后选择Runner目标。 然后将下面的CFBundleURLTypes属性添加到[my_project]/ios/Runner/Info.plist文件中。

<!-- Put me in the [my_project]/ios/Runner/Info.plist file -->
<!-- Google Sign-in Section -->
<key>CFBundleURLTypes</key>
<array>
    <dict>
        <key>CFBundleTypeRole</key>
        <string>Editor</string>
        <key>CFBundleURLSchemes</key>
        <array>
            <!-- TODO Replace this value: -->
            <!-- Copied from GoogleServices-Info.plist key REVERSE_CLIENT_ID -->
            <string>com.googleusercontent.apps.861823949799-vc35cprkp249096uujjn0vvnmcvjppkn</string>
        </array>
    </dict>
</array>
<!-- End of the Google Sign-in Section -->
  

2-安装google_sign_in插件

我们必须在第一步中安装google_sign_in插件。因此,转到您的 pubspec.yaml 文件(位于项目的根目录中),然后在dependencies:部分中添加以下行

google_sign_in: "^3.0.4"

注意:检查最新版本。我使用的是3.0.4,但您必须使用最新的版本以保持最新版本

  

3-使用GoogleSignIn对象

现在,我们必须设置GoogleSignIn对象以从Google身份验证开始。

GoogleSignIn _googleSignIn = new GoogleSignIn(
   scopes: [
      'email',
      'https://www.googleapis.com/auth/contacts.readonly',
   ],
);

主要作用是获取只读的联系方式。 这个想法是检查应用程序是否已经登录(如果过去执行过与Google的身份验证)。因此,我们应该听取“当前用户”的任何更改。为此,我们必须添加监听器来检查这些更改:


    _googleSignIn.onCurrentUserChanged.listen((GoogleSignInAccount account) async {
      if (account != null) {
        // user logged
      } else {
        // user NOT logged
      }
    });

如果account != null应用已登录,则必须打开主屏幕(或流程的下一个屏幕)。否则,您无需执行任何操作,然后等待用户互动(我们将在以后看到用户界面)。 之后,您必须执行静默登录才能在没有用户交互的情况下进行登录。回调将在我们添加的上一个侦听器中返回。因此,要执行静默登录,我们添加了以下代码:

_googleSignIn.signInSilently().whenComplete(() => dismissLoading());

请注意,signInSilently()返回一个Future,因此我们可以调用whenComplete(...)来知道此执行何时完成。然后我们可以消除一些负担。 这是完整的代码:

  GoogleSignIn _googleSignIn = new GoogleSignIn(
    scopes: [
      'email',
      'https://www.googleapis.com/auth/contacts.readonly',
    ],
  );

  initLogin() {
    _googleSignIn.onCurrentUserChanged.listen((GoogleSignInAccount account) async {
      if (account != null) {
        // user logged
      } else {
        // user NOT logged
      }
    });
    _googleSignIn.signInSilently().whenComplete(() => dismissLoading());
  }

此后,我们必须创建一个函数,该函数按需执行signIn动作。该功能将从UI调用(如果您正在其他类中使用它)。


  doLogin() async {
    showLoading();
    await _googleSignIn.signIn();
  }

我们只需使用_googleSignIn.signIn()修饰符调用await来等待执行。结果将被侦听我们设置的上一个回调。

  

4-构建UI登录按钮

我们可以创建自己的登录按钮,或在那里找人。我将在这里发布自己的实现(非常基础的?,但可以正常工作)

    var loginBtn = RaisedButton(
      onPressed: () => doLogin(),
      child: Text("Login G+"),
      color: Colors.primaries[0],
    );

当用户点击此按钮时,它将调用我们先前定义的doLogin函数。

答案 2 :(得分:0)

是的。尝试签证 - https://github.com/e-oj/visa

这是一个使用 Facebook 身份验证的示例(它还支持 google、twitch、discord 和 Github):

import 'package:visa/auth-data.dart';
import 'package:visa/fb.dart';

class AuthPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      /// Simply Provide all the necessary credentials
      body: FaceBookAuth().visa.authenticate(
          clientID: '139732240983759',
          redirectUri: 'https://www.e-oj.com/oauth',
          scope: 'public_profile,email',
          state: 'fbAuth',
          onDone: done
      )
    );
  }
}

和“完成”回调:

done(AuthData authData){
  print(authData);

  /// You can pass the [AuthData] object to a 
  /// post-authentication screen. It contaions 
  /// all the user and OAuth data collected during
  /// the authentication process. In this example,
  /// our post-authentication screen is "complete-profile".
  Navigator.pushReplacementNamed(
      context, '/complete-profile', arguments: authData
  );
}