Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/auth-error #364

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
208 changes: 104 additions & 104 deletions lib/auth/providers/openid_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -188,24 +188,23 @@ class OpenIdTokenProvider
popupWin = null;
}
try {
if (token != null && token.isNotEmpty) {
final resp = await openIdRepository.getToken(
token,
clientId,
redirectUri.toString(),
codeVerifier,
"authorization_code",
);
final accessToken = resp[tokenKey]!;
final refreshToken = resp[refreshTokenKey]!;
await _secureStorage.write(key: tokenName, value: refreshToken);
state = AsyncValue.data({
tokenKey: accessToken,
refreshTokenKey: refreshToken,
});
} else {
if (token == null || token.isEmpty) {
throw Exception('Wrong credentials');
}
final resp = await openIdRepository.getToken(
token,
clientId,
redirectUri.toString(),
codeVerifier,
"authorization_code",
);
final accessToken = resp[tokenKey]!;
final refreshToken = resp[refreshTokenKey]!;
await _secureStorage.write(key: tokenName, value: refreshToken);
state = AsyncValue.data({
tokenKey: accessToken,
refreshTokenKey: refreshToken,
});
} on TimeoutException catch (_) {
throw Exception('No response from server');
} catch (e) {
Expand All @@ -229,15 +228,16 @@ class OpenIdTokenProvider
allowInsecureConnections: kDebugMode,
),
);
if (resp != null) {
await _secureStorage.write(key: tokenName, value: resp.refreshToken);
state = AsyncValue.data({
tokenKey: resp.accessToken!,
refreshTokenKey: resp.refreshToken!,
});
} else {
state = const AsyncValue.error("Error", StackTrace.empty);
if (resp == null) {
state = const AsyncValue.error(
"No response from server", StackTrace.empty);
return;
}
await _secureStorage.write(key: tokenName, value: resp.refreshToken);
state = AsyncValue.data({
tokenKey: resp.accessToken!,
refreshTokenKey: resp.refreshToken!,
});
}
} catch (e) {
state = AsyncValue.error("Error $e", StackTrace.empty);
Expand All @@ -247,49 +247,50 @@ class OpenIdTokenProvider
Future getTokenFromStorage() async {
state = const AsyncValue.loading();
_secureStorage.read(key: tokenName).then((token) async {
if (token != null) {
try {
if (kIsWeb) {
final resp = await openIdRepository.getToken(
token,
if (token == null) {
state = const AsyncValue.error("No token found", StackTrace.empty);
return;
}
try {
if (kIsWeb) {
final resp = await openIdRepository.getToken(
token,
clientId,
"",
"",
refreshTokenKey,
);
final accessToken = resp[tokenKey]!;
final refreshToken = resp[refreshTokenKey]!;
await _secureStorage.write(key: tokenName, value: refreshToken);
state = AsyncValue.data({
tokenKey: accessToken,
refreshTokenKey: refreshToken,
});
} else {
final resp = await appAuth.token(
TokenRequest(
clientId,
"",
"",
refreshTokenKey,
);
final accessToken = resp[tokenKey]!;
final refreshToken = resp[refreshTokenKey]!;
await _secureStorage.write(key: tokenName, value: refreshToken);
state = AsyncValue.data({
tokenKey: accessToken,
refreshTokenKey: refreshToken,
});
} else {
final resp = await appAuth.token(
TokenRequest(
clientId,
redirectUrl,
discoveryUrl: discoveryUrl,
scopes: scopes,
refreshToken: token,
allowInsecureConnections: kDebugMode,
),
);
if (resp != null) {
state = AsyncValue.data({
tokenKey: resp.accessToken!,
refreshTokenKey: resp.refreshToken!,
});
storeToken();
} else {
state = const AsyncValue.error("Error", StackTrace.empty);
}
redirectUrl,
discoveryUrl: discoveryUrl,
scopes: scopes,
refreshToken: token,
allowInsecureConnections: kDebugMode,
),
);
if (resp == null) {
state = const AsyncValue.error(
"No response from server", StackTrace.empty);
return;
}
} catch (e) {
state = AsyncValue.error(e, StackTrace.empty);
state = AsyncValue.data({
tokenKey: resp.accessToken!,
refreshTokenKey: resp.refreshToken!,
});
storeToken();
}
} else {
state = const AsyncValue.error("No token found", StackTrace.empty);
} catch (e) {
state = AsyncValue.error(e, StackTrace.empty);
}
});
}
Expand All @@ -307,53 +308,52 @@ class OpenIdTokenProvider
),
)
.then((resp) {
if (resp != null) {
state = AsyncValue.data({
tokenKey: resp.accessToken!,
refreshTokenKey: resp.refreshToken!,
});
} else {
state = const AsyncValue.error("Error", StackTrace.empty);
if (resp == null) {
state =
const AsyncValue.error("No response from server", StackTrace.empty);
return;
}
state = AsyncValue.data({
tokenKey: resp.accessToken!,
refreshTokenKey: resp.refreshToken!,
});
});
}

Future<bool> refreshToken() async {
return state.when(
data: (token) async {
if (token[refreshTokenKey] != null && token[refreshTokenKey] != "") {
TokenResponse? resp = await appAuth.token(
TokenRequest(
clientId,
redirectUrl,
discoveryUrl: discoveryUrl,
scopes: scopes,
refreshToken: token[refreshTokenKey] as String,
allowInsecureConnections: kDebugMode,
),
);
if (resp == null) {
state = const AsyncValue.error("Error", StackTrace.empty);
return false;
data: (token) async {
if (token[refreshTokenKey] != null && token[refreshTokenKey] != "") {
TokenResponse? resp = await appAuth.token(
TokenRequest(
clientId,
redirectUrl,
discoveryUrl: discoveryUrl,
scopes: scopes,
refreshToken: token[refreshTokenKey] as String,
allowInsecureConnections: kDebugMode,
),
);
if (resp == null) {
state = const AsyncValue.error(
"No response from server", StackTrace.empty);
return false;
}
state = AsyncValue.data({
tokenKey: resp.accessToken!,
refreshTokenKey: resp.refreshToken!,
});
storeToken();
return true;
}
state = AsyncValue.data({
tokenKey: resp.accessToken!,
refreshTokenKey: resp.refreshToken!,
});
storeToken();
return true;
}
state = const AsyncValue.error(e, StackTrace.empty);
return false;
},
error: (error, stackTrace) {
state = AsyncValue.error(error, stackTrace);
return false;
},
loading: () {
return false;
},
);
state = const AsyncValue.error(e, StackTrace.empty);
return false;
},
error: (error, stackTrace) {
state = AsyncValue.error(error, stackTrace);
return false;
},
loading: () => false);
}

void storeToken() {
Expand Down
4 changes: 3 additions & 1 deletion lib/login/ui/app_sign_in.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import 'package:myecl/login/ui/components/sign_in_up_bar.dart';
import 'package:myecl/tools/constants.dart';
import 'package:myecl/tools/functions.dart';
import 'package:myecl/tools/providers/path_forwarding_provider.dart';
import 'package:myecl/tools/repository/repository.dart';
import 'package:qlevar_router/qlevar_router.dart';

class AppSignIn extends HookConsumerWidget {
Expand Down Expand Up @@ -74,10 +75,11 @@ class AppSignIn extends HookConsumerWidget {
QR.to(pathForwarding.path);
},
error: (e, s) {
Repository.logger.error(e.toString());
displayToast(
context,
TypeMsg.error,
LoginTextConstants.loginFailed,
"${LoginTextConstants.loginFailed} : $e",
);
},
loading: () {},
Expand Down