Skip to content

Commit c6695f8

Browse files
authored
Refactor/login (#4583)
1 parent ba02edb commit c6695f8

File tree

1 file changed

+29
-20
lines changed

1 file changed

+29
-20
lines changed

lib/src/command/login.dart

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -24,59 +24,68 @@ class LoginCommand extends PubCommand {
2424
@override
2525
Future<void> runProtected() async {
2626
final credentials = oauth2.loadCredentials();
27+
final userInfo = await _retrieveUserInfo();
28+
2729
if (credentials == null) {
28-
final userInfo = await _retrieveUserInfo();
2930
if (userInfo == null) {
3031
log.warning(
31-
'Could not retrieve your user-details.\n'
32-
'You might have to run `$topLevelProgram pub logout` '
33-
'to delete your credentials and try again.',
32+
'Could not retrieve your user details.\n'
33+
'Run `$topLevelProgram pub logout` to delete credentials and try '
34+
'again.',
3435
);
3536
} else {
3637
log.message('You are now logged in as $userInfo');
3738
}
3839
} else {
39-
final userInfo = await _retrieveUserInfo();
4040
if (userInfo == null) {
4141
log.warning(
42-
'Your credentials seems broken.\n'
43-
'Run `$topLevelProgram pub logout` '
44-
'to delete your credentials and try again.',
42+
'Your credentials seem to be broken.\n'
43+
'Run `$topLevelProgram pub logout` to delete credentials and try '
44+
'again.',
4545
);
46+
} else {
47+
log.message('You are already logged in as $userInfo');
4648
}
47-
log.warning(
48-
'You are already logged in as $userInfo\n'
49-
'Run `$topLevelProgram pub logout` to log out and try again.',
50-
);
5149
}
5250
}
5351

5452
Future<_UserInfo?> _retrieveUserInfo() async {
5553
return await oauth2.withClient((client) async {
5654
final discovery = await oauth2.fetchOidcDiscoveryDocument();
5755
final userInfoEndpoint = discovery['userinfo_endpoint'];
56+
5857
if (userInfoEndpoint is! String) {
59-
log.fine('Bad discovery document. userinfo_endpoint not a String');
58+
log.fine('''
59+
Invalid discovery document: userinfo_endpoint is not a String
60+
''');
61+
6062
return null;
6163
}
64+
6265
final userInfoRequest = await client.get(Uri.parse(userInfoEndpoint));
63-
if (userInfoRequest.statusCode != 200) return null;
66+
if (userInfoRequest.statusCode != 200) {
67+
log.fine('''
68+
Failed to fetch user info: HTTP ${userInfoRequest.statusCode}
69+
''');
70+
return null;
71+
}
72+
6473
try {
6574
switch (json.decode(userInfoRequest.body)) {
6675
case {'name': final String? name, 'email': final String email}:
6776
return _UserInfo(name: name, email: email);
6877
case {'email': final String email}:
6978
return _UserInfo(name: null, email: email);
7079
default:
71-
log.fine(
72-
'Bad response from $userInfoEndpoint: ${userInfoRequest.body}',
73-
);
80+
log.fine('''
81+
Bad response from $userInfoEndpoint: ${userInfoRequest.body}
82+
''');
7483
return null;
7584
}
7685
} on FormatException catch (e) {
77-
log.fine(
78-
'Bad response from $userInfoEndpoint ($e): ${userInfoRequest.body}',
79-
);
86+
log.fine('''
87+
Failed to decode user info: $e\nResponse: ${userInfoRequest.body}
88+
''');
8089
return null;
8190
}
8291
});

0 commit comments

Comments
 (0)