firebase_auth 0.8.0+1 firebase_auth: ^0.8.0+1 copied to clipboard
Flutter plugin for Firebase Auth, enabling Android and iOS authentication using passwords, phone numbers and identity providers like Google, Facebook and Twitter.
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'dart:async';
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:google_sign_in/google_sign_in.dart';
final FirebaseAuth _auth = FirebaseAuth.instance;
final GoogleSignIn _googleSignIn = GoogleSignIn();
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Firebase Auth Demo',
home: MyHomePage(title: 'Firebase Auth Demo'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
Future<String> _message = Future<String>.value('');
TextEditingController _smsCodeController = TextEditingController();
String verificationId;
final String testSmsCode = '888888';
final String testPhoneNumber = '+1 408-555-6969';
Future<String> _testSignInAnonymously() async {
final FirebaseUser user = await _auth.signInAnonymously();
assert(user != null);
assert(user.isAnonymous);
assert(!user.isEmailVerified);
assert(await user.getIdToken() != null);
if (Platform.isIOS) {
// Anonymous auth doesn't show up as a provider on iOS
assert(user.providerData.isEmpty);
} else if (Platform.isAndroid) {
// Anonymous auth does show up as a provider on Android
assert(user.providerData.length == 1);
assert(user.providerData[0].providerId == 'firebase');
assert(user.providerData[0].uid != null);
assert(user.providerData[0].displayName == null);
assert(user.providerData[0].photoUrl == null);
assert(user.providerData[0].email == null);
}
final FirebaseUser currentUser = await _auth.currentUser();
assert(user.uid == currentUser.uid);
return 'signInAnonymously succeeded: $user';
}
Future<String> _testSignInWithGoogle() async {
final GoogleSignInAccount googleUser = await _googleSignIn.signIn();
final GoogleSignInAuthentication googleAuth =
await googleUser.authentication;
final AuthCredential credential = GoogleAuthProvider.getCredential(
accessToken: googleAuth.accessToken,
idToken: googleAuth.idToken,
);
final FirebaseUser user = await _auth.signInWithCredential(credential);
assert(user.email != null);
assert(user.displayName != null);
assert(!user.isAnonymous);
assert(await user.getIdToken() != null);
final FirebaseUser currentUser = await _auth.currentUser();
assert(user.uid == currentUser.uid);
return 'signInWithGoogle succeeded: $user';
}
Future<void> _testVerifyPhoneNumber() async {
final PhoneVerificationCompleted verificationCompleted =
(FirebaseUser user) {
setState(() {
_message =
Future<String>.value('signInWithPhoneNumber auto succeeded: $user');
});
};
final PhoneVerificationFailed verificationFailed =
(AuthException authException) {
setState(() {
_message = Future<String>.value(
'Phone number verification failed. Code: ${authException.code}. Message: ${authException.message}');
});
};
final PhoneCodeSent codeSent =
(String verificationId, [int forceResendingToken]) async {
this.verificationId = verificationId;
_smsCodeController.text = testSmsCode;
};
final PhoneCodeAutoRetrievalTimeout codeAutoRetrievalTimeout =
(String verificationId) {
this.verificationId = verificationId;
_smsCodeController.text = testSmsCode;
};
await _auth.verifyPhoneNumber(
phoneNumber: testPhoneNumber,
timeout: const Duration(seconds: 5),
verificationCompleted: verificationCompleted,
verificationFailed: verificationFailed,
codeSent: codeSent,
codeAutoRetrievalTimeout: codeAutoRetrievalTimeout);
}
Future<String> _testSignInWithPhoneNumber(String smsCode) async {
final AuthCredential credential = PhoneAuthProvider.getCredential(
verificationId: verificationId,
smsCode: smsCode,
);
final FirebaseUser user = await _auth.signInWithCredential(credential);
final FirebaseUser currentUser = await _auth.currentUser();
assert(user.uid == currentUser.uid);
_smsCodeController.text = '';
return 'signInWithPhoneNumber succeeded: $user';
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
MaterialButton(
child: const Text('Test signInAnonymously'),
onPressed: () {
setState(() {
_message = _testSignInAnonymously();
});
}),
MaterialButton(
child: const Text('Test signInWithGoogle'),
onPressed: () {
setState(() {
_message = _testSignInWithGoogle();
});
}),
MaterialButton(
child: const Text('Test verifyPhoneNumber'),
onPressed: () {
_testVerifyPhoneNumber();
}),
Container(
margin: const EdgeInsets.only(
top: 8.0,
bottom: 8.0,
left: 16.0,
right: 16.0,
),
child: TextField(
controller: _smsCodeController,
decoration: const InputDecoration(
hintText: 'SMS Code',
),
),
),
MaterialButton(
child: const Text('Test signInWithPhoneNumber'),
onPressed: () {
if (_smsCodeController.text != null) {
setState(() {
_message =
_testSignInWithPhoneNumber(_smsCodeController.text);
});
}
}),
FutureBuilder<String>(
future: _message,
builder: (_, AsyncSnapshot<String> snapshot) {
return Text(snapshot.data ?? '',
style:
const TextStyle(color: Color.fromARGB(255, 0, 155, 0)));
}),
],
),
);
}
}