简体   繁体   English

使用 Twitter 登录不会将用户添加到 Flutter 上的 Firebase

[英]Logging in with Twitter doesn't add user to Firebase on Flutter

I have coded a login page with Google and Twitter auth, but the Twitter part doesn't add the user to the user's section on Firebase. The login part shows, but when you log in the account isn't added to Firebase. This is the code for my Twitter authentication:我已经使用 Google 和 Twitter auth 编写了一个登录页面,但是 Twitter 部分没有将用户添加到 Firebase 上的用户部分。登录部分显示,但是当您登录时,帐户没有添加到 Firebase。这是我的 Twitter 身份验证代码:

import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';
import 'package:twitter_login/twitter_login.dart';

class TwitterSignInProvider extends ChangeNotifier {
  final twitterLogin = TwitterLogin(
    /// Consumer API keys
    apiKey: ####,

    /// Consumer API Secret keys
    apiSecretKey: ####,

    /// Registered Callback URLs in TwitterApp
    redirectURI: '####',
  );
  Future twitterLog() async {
    final authResult = await twitterLogin.login();
    switch (authResult.status) {
      case TwitterLoginStatus.loggedIn:
        // success

        final twitterCredential = TwitterAuthProvider.credential(
          accessToken: authResult.authToken.toString(),
          secret: authResult.authTokenSecret.toString(),
        );

        await FirebaseAuth.instance.signInWithCredential(twitterCredential);
        notifyListeners();
        break;
      case TwitterLoginStatus.cancelledByUser:
        return;
      case TwitterLoginStatus.error:
      case null:
        return;
    }
  }
}

This is the section for the login screen that matters:这是重要的登录屏幕部分:

@override
  Widget build(BuildContext context) {
    return MultiProvider(
      providers: [
        ChangeNotifierProvider(create: (context) => GoogleSignInProvider()),
        ChangeNotifierProvider(create: (context) => TwitterSignInProvider()),
      ],
      builder: ((context, child) => MaterialApp(
              home: Scaffold(
            backgroundColor: Colors.grey[300],
            body: SafeArea(
              child: Center(
                child: SingleChildScrollView(
                  child: Column(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: [ 
                      Row(
                        mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                        children: [
                          //google sign in
                          RawMaterialButton(
                            onPressed: () {
                              final provider =
                                  Provider.of<GoogleSignInProvider>(context,
                                      listen: false);
                              provider.googleLogin();
                            },
                            elevation: 1.0,
                            fillColor: Colors.white,
                            child: FaIcon(
                              FontAwesomeIcons.google,
                              color: Colors.red[500],
                              size: 35,
                            ),
                            padding: EdgeInsets.all(15.0),
                            shape: CircleBorder(),
                          ),
                          //twitter login
                          RawMaterialButton(
                            onPressed: () async {
                              final provider =
                                  Provider.of<TwitterSignInProvider>(context,
                                      listen: false);
                              provider.twitterLog();
                            },
                            elevation: 1.0,
                            fillColor: Colors.white,
                            child: FaIcon(
                              FontAwesomeIcons.twitter,
                              color: Colors.lightBlue[500],
                              size: 35,
                            ),
                            padding: EdgeInsets.all(15.0),
                            shape: CircleBorder(),
                          ),
                        ],
                      )
                    ],
                  ),
                ),
              ),
            ),
          ))),
    );
  }
}

Any help would be greatly appreciated!任何帮助将不胜感激!

As I understand from your question/comments you expect that once you're authenticated with Twitter to have the user data written in the Realtime Database.据我从您的问题/评论中了解到,您希望一旦您通过 Twitter 进行身份验证,就可以将用户数据写入实时数据库。 But this is not how it works.但这不是它的工作原理。 If you want to have the user's data stored in the database, you have to write some code for that.如果您想将用户的数据存储在数据库中,则必须为此编写一些代码。 There is nothing performed automatically.没有什么是自动执行的。 Meaning that once the user is successfully authenticated, then you can perform a write operation in the Realtime Database to store the desired data.这意味着一旦用户成功通过身份验证,您就可以在实时数据库中执行写入操作以存储所需的数据。 The schema might look like this:该架构可能如下所示:

db-root
 |
 --- users
      |
      --- $uid
           |
           --- //user fileds.

There is no need to add an extra level in your database called authentication .无需在您的数据库中添加一个额外的级别,称为authentication

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM