Advertisement
Guest User

Untitled

a guest
Jul 27th, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.31 KB | None | 0 0
  1. import 'dart:async';
  2. import 'dart:io';
  3.  
  4. import 'package:flutter/material.dart';
  5. import 'package:firebase_auth/firebase_auth.dart';
  6. import 'package:google_sign_in/google_sign_in.dart';
  7.  
  8. final FirebaseAuth _auth = FirebaseAuth.instance;
  9. final GoogleSignIn _googleSignIn = new GoogleSignIn();
  10.  
  11. void main() {
  12. runApp(new MyApp());
  13. }
  14.  
  15. class MyApp extends StatelessWidget {
  16. @override
  17. Widget build(BuildContext context) {
  18. return new MaterialApp(
  19. title: 'Firebase Auth Demo',
  20. home: new MyHomePage(title: 'Firebase Auth Demo'),
  21. );
  22. }
  23. }
  24.  
  25. class MyHomePage extends StatefulWidget {
  26. MyHomePage({Key key, this.title}) : super(key: key);
  27.  
  28. final String title;
  29.  
  30. @override
  31. _MyHomePageState createState() => new _MyHomePageState();
  32. }
  33.  
  34. class _MyHomePageState extends State<MyHomePage> {
  35. Future<String> _message = new Future<String>.value('');
  36.  
  37. Future<String> _testSignInAnonymously() async {
  38. final FirebaseUser user = await _auth.signInAnonymously();
  39. assert(user != null);
  40. assert(user == _auth.currentUser);
  41. assert(user.isAnonymous);
  42. assert(!user.isEmailVerified);
  43. assert(await user.getToken() != null);
  44. if (Platform.isIOS) {
  45. // Anonymous auth doesn't show up as a provider on iOS
  46. assert(user.providerData.isEmpty);
  47. } else if (Platform.isAndroid) {
  48. // Anonymous auth does show up as a provider on Android
  49. assert(user.providerData.length == 1);
  50. assert(user.providerData[0].providerId == 'firebase');
  51. assert(user.providerData[0].uid != null);
  52. assert(user.providerData[0].displayName == null);
  53. assert(user.providerData[0].photoUrl == null);
  54. assert(user.providerData[0].email == null);
  55. }
  56. return 'signInAnonymously succeeded: $user';
  57. }
  58.  
  59. Future<String> _testSignInWithGoogle() async {
  60. final GoogleSignInAccount googleUser = await _googleSignIn.signIn();
  61. final GoogleSignInAuthentication googleAuth =
  62. await googleUser.authentication;
  63. final FirebaseUser user = await _auth.signInWithGoogle(
  64. accessToken: googleAuth.accessToken,
  65. idToken: googleAuth.idToken,
  66. );
  67. assert(user.email != null);
  68. assert(user.displayName != null);
  69. assert(!user.isAnonymous);
  70. assert(await user.getToken() != null);
  71. return 'signInWithGoogle succeeded: $user';
  72. }
  73.  
  74. @override
  75. Widget build(BuildContext context) {
  76. return new Scaffold(
  77. appBar: new AppBar(
  78. title: new Text(widget.title),
  79. ),
  80. body: new Column(
  81. crossAxisAlignment: CrossAxisAlignment.start,
  82. children: <Widget>[
  83. new MaterialButton(
  84. child: const Text('Test signInAnonymously'),
  85. onPressed: () {
  86. setState(() {
  87. _message = _testSignInAnonymously();
  88. });
  89. }),
  90. new MaterialButton(
  91. child: const Text('Test signInWithGoogle'),
  92. onPressed: () {
  93. setState(() {
  94. _message = _testSignInWithGoogle();
  95. });
  96. }),
  97. new FutureBuilder<String>(
  98. future: _message,
  99. builder: (_, AsyncSnapshot<String> snapshot) {
  100. return new Text(snapshot.data ?? '',
  101. style: const TextStyle(
  102. color: const Color.fromARGB(255, 0, 155, 0)));
  103. }),
  104. ],
  105. ),
  106. );
  107. }
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement