Last active
August 9, 2019 07:47
-
-
Save Hellomik2002/00744fa84dbf40e1eaf52c29d8a659e4 to your computer and use it in GitHub Desktop.
This file contains 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
//in class auth | |
Future<void> _authenticate( | |
String email, String password, String urlSegment) async { | |
final url = | |
'https://www.googleapis.com/identitytoolkit/v3/relyingparty/$urlSegment?key=hjey'; | |
try { | |
final response = await http.post( | |
url, | |
body: json.encode( | |
{ | |
'email': email, | |
'password': password, | |
'returnSecureToken': true, | |
}, | |
), | |
); | |
final responseData = json.decode(response.body); | |
if (responseData['error'] != null) { | |
throw HttpException(responseData['error']['message']); | |
} | |
_token = responseData['idToken']; | |
_userId = responseData['localId']; | |
_expiryDate = DateTime.now().add( | |
Duration( | |
seconds: int.parse( | |
responseData['expiresIn'], | |
), | |
), | |
); | |
_autoLogout(); | |
notifyListeners(); | |
final prefs = await SharedPreferences.getInstance(); | |
final userData = json.encode( | |
{ | |
'token': _token, | |
'userId': _userId, | |
'expiryDate': _expiryDate.toIso8601String(), | |
}, | |
); | |
prefs.setString('userData', userData); | |
} catch (error) { | |
print(error); | |
throw error; | |
} | |
} | |
Future<void> signup(String email, String password) async { | |
return _authenticate(email, password, 'signupNewUser'); | |
} | |
Future<void> login(String email, String password) async { | |
return _authenticate(email, password, 'verifyPassword'); | |
} | |
// in Raised Button | |
Future<void> _submit() async { | |
if (!_formKey.currentState.validate()) { | |
// Invalid! | |
return; | |
} | |
_formKey.currentState.save(); | |
setState(() { | |
_isLoading = true; | |
}); | |
try { | |
if (_authMode == AuthMode.Login) { | |
// Log user in | |
await Provider.of<Auth>(context, listen: false).login( | |
_authData['email'], | |
_authData['password'], | |
); | |
} else { | |
// Sign user up | |
await Provider.of<Auth>(context, listen: false).signup( | |
_authData['email'], | |
_authData['password'], | |
); | |
} | |
} on HttpException catch (error) { | |
var errorMessage = 'Authentication failed'; | |
if (error.toString().contains('EMAIL_EXISTS')) { | |
errorMessage = 'This email address is already in use.'; | |
} else if (error.toString().contains('INVALID_EMAIL')) { | |
errorMessage = 'This is not a valid email address'; | |
} else if (error.toString().contains('WEAK_PASSWORD')) { | |
errorMessage = 'This password is too weak.'; | |
} else if (error.toString().contains('EMAIL_NOT_FOUND')) { | |
errorMessage = 'Could not find a user with that email.'; | |
} else if (error.toString().contains('INVALID_PASSWORD')) { | |
errorMessage = 'Invalid password.'; | |
} | |
_showErrorDialog(errorMessage); | |
} catch (error) { | |
const errorMessage = | |
'Could not authenticate you. Please try again later.'; | |
_showErrorDialog(errorMessage); | |
} | |
setState(() { | |
_isLoading = false; | |
}); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment