Created
November 2, 2018 02:08
-
-
Save zerox1212/fb961ee09b9869cb29b8a9894a90ea57 to your computer and use it in GitHub Desktop.
Scoped Model for User
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Copyright 2018 zerox1212. All rights reserved. | |
// Use of this source code is governed by the MIT license that can be found | |
// in the LICENSE file. | |
import 'dart:async'; | |
import 'dart:io'; | |
import 'package:scoped_model/scoped_model.dart'; | |
import 'package:firebase_auth/firebase_auth.dart'; | |
class UserModel extends Model { | |
final FirebaseAuth _auth = FirebaseAuth.instance; | |
FirebaseUser _user; | |
FirebaseUser get user => _user; | |
Future<Null> logInAnonymously() async { | |
_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); | |
notifyListeners(); | |
} | |
Future<Null> logOut() async { | |
await _auth.signOut(); | |
_user = null; | |
notifyListeners(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment