简体   繁体   中英

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:

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. 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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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