Skip to content

Commit

Permalink
Solved flutter's bug on web release with some library (#728)
Browse files Browse the repository at this point in the history
  • Loading branch information
omerfaunal authored Dec 25, 2023
1 parent d000984 commit c49d3af
Showing 1 changed file with 16 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,6 @@ class Auth with ChangeNotifier {
'Accept': 'application/json',
'Authorization': "Token $token"
};

//Add token to SharedPreferences
SharedPreferences prefs = await SharedPreferences.getInstance();
prefs.setString('token', token);

final tokenResponse = await http.get(url, headers: tokenHeaders);
if (tokenResponse.statusCode == 200) {
final userData = json.decode(tokenResponse.body);
Expand All @@ -69,13 +64,23 @@ class Auth with ChangeNotifier {
} else {
throw Exception("Something has happened");
}

//Add token to SharedPreferences
try {
SharedPreferences prefs = await SharedPreferences.getInstance();
prefs.setString('token', token);
} catch (e) {
print("Error: $e");
}

notifyListeners();
} else if (response.statusCode == 400) {
throw WrongPasswordException(message: 'Your credentials are wrong');
} else {
throw Exception("Something has happened");
}
} catch (e) {
print(e);
rethrow;
}
}
Expand Down Expand Up @@ -121,7 +126,12 @@ class Auth with ChangeNotifier {
Future<bool> checkTokenAndLogin() async {
// Step 1: Retrieve the token from SharedPreferences
SharedPreferences prefs = await SharedPreferences.getInstance();
String? token = prefs.getString('token');
String? token;
try {
token = prefs.getString('token');
} catch (e) {
// If there is no token, then return false
}

if (token != null) {
// Step 2: Make the HTTP request with the token
Expand Down

0 comments on commit c49d3af

Please sign in to comment.