'Twitter Login with flutter and firebase not working 100%
I wanted to implement twitter login into my app... so far so good I got it to run but now Im kinda stuck and I don't know whats going on... I wrote a function (see below) that lets you sign in with the apikey and so on but when I click the button I get redirected to the twitter API but there is no new user on firebase... I don't know why this is happening... see the code below: (for "example" in the API keys I inserted my API keys I just didn't want to publish them here...)
Furthermore when I dismiss the Google Sign In dialog on my iPhone 13 emulator I get an Error (PlatformException) although I use a try-catch block... On my physical android device it worked properly... idk why this is happening...
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';
import 'package:google_fonts/google_fonts.dart';
import 'package:google_sign_in/google_sign_in.dart';
import 'package:twitter_login/twitter_login.dart';
import 'package:wineapp/constants.dart';
class AuthService {
FirebaseAuth firebaseAuth = FirebaseAuth.instance;
//Register User
Future<User?> emailRegister(
String email, String password, BuildContext context) async {
try {
UserCredential userCredential =
await firebaseAuth.createUserWithEmailAndPassword(
email: email,
password: password,
);
return userCredential.user;
} on FirebaseAuthException catch (e) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text(
e.message.toString(),
style: GoogleFonts.poppins(
textStyle: const TextStyle(
color: mainTextColor,
fontSize: 12,
fontWeight: FontWeight.w600,
),
),
),
backgroundColor: primaryColor,
),
);
} catch (e) {
print(e);
}
}
//User login
Future<User?> emailLogin(
String email, String password, BuildContext context) async {
try {
UserCredential userCredential =
await firebaseAuth.signInWithEmailAndPassword(
email: email,
password: password,
);
return userCredential.user;
} on FirebaseAuthException catch (e) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text(
e.message.toString(),
style: GoogleFonts.poppins(
textStyle: const TextStyle(
color: mainTextColor,
fontSize: 12,
fontWeight: FontWeight.w600,
),
),
),
backgroundColor: primaryColor,
),
);
}
}
//User SignIn with Google
Future<User?> signInWithGoogle() async {
try {
//Triger the authentication flow
final GoogleSignInAccount? googleUser = await GoogleSignIn().signIn();
if (googleUser != null) {
//Obtain the auth details from the request
final GoogleSignInAuthentication googleAuth =
await googleUser.authentication;
//Create a new credential
final credential = GoogleAuthProvider.credential(
accessToken: googleAuth.accessToken,
idToken: googleAuth.idToken,
);
//Once signed in, return the UserCredential
UserCredential userCredential =
await firebaseAuth.signInWithCredential(credential);
return userCredential.user;
}
} on FirebaseAuthException catch (e) {
print(
e.toString(),
);
}
return null;
}
//Sign Out function
Future googleSignOut() async {
await GoogleSignIn().signOut();
await firebaseAuth.signOut();
}
void twitterLogin() async {
// Create a TwitterLogin instance
final twitterLogin = TwitterLogin(
apiKey: 'example',
apiSecretKey: 'example',
redirectURI: 'flutter-twitter-login://');
// Trigger the sign-in flow
await twitterLogin.login().then((value) async {
if (value.authToken != null || value.authTokenSecret != null) {
final twitterAuthCredential = TwitterAuthProvider.credential(
accessToken: value.authToken,
secret: value.authTokenSecret,
);
await FirebaseAuth.instance.signInWithCredential(twitterAuthCredential);
}
});
}
}
Thanks for your help in advance:)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
