improvement: save gitlab id

This commit is contained in:
Rongjian Zhang 2019-12-30 20:50:31 +08:00
parent 396fe550c4
commit 259d25d376
3 changed files with 10 additions and 3 deletions

View File

@ -10,6 +10,7 @@ class Account {
String token;
String login;
String avatarUrl;
int gitlabId;
equals(Account a) {
final uri = Uri.parse(domain);
@ -18,6 +19,7 @@ class Account {
// Treat domains as the same if they have the same hosts and ports
return a.platform == platform &&
a.login == login &&
a.gitlabId == gitlabId &&
uri.host == uriA.host &&
uri.port == uriA.port;
}
@ -28,6 +30,7 @@ class Account {
@required this.token,
@required this.login,
@required this.avatarUrl,
this.gitlabId,
});
factory Account.fromJson(Map<String, dynamic> json) =>

View File

@ -13,6 +13,7 @@ Account _$AccountFromJson(Map<String, dynamic> json) {
token: json['token'] as String,
login: json['login'] as String,
avatarUrl: json['avatarUrl'] as String,
gitlabId: json['gitlabId'] as int,
);
}
@ -22,4 +23,5 @@ Map<String, dynamic> _$AccountToJson(Account instance) => <String, dynamic>{
'token': instance.token,
'login': instance.login,
'avatarUrl': instance.avatarUrl,
'gitlabId': instance.gitlabId,
};

View File

@ -15,6 +15,7 @@ import 'package:shared_preferences/shared_preferences.dart';
import '../utils/constants.dart';
import '../utils/utils.dart';
import 'account.dart';
import 'gitlab.dart';
class PlatformType {
static const github = 'github';
@ -111,17 +112,18 @@ class AuthModel with ChangeNotifier {
final res = await http
.get('$domain/api/v4/user', headers: {'Private-Token': token});
final info = json.decode(res.body);
if (info['message'] != null) {
throw info['message'];
}
final user = GitlabUser.fromJson(info);
await _addAccount(Account(
platform: PlatformType.gitlab,
domain: domain,
token: token,
login: info['username'] as String,
avatarUrl: info['avatar_url'] as String,
login: user.username,
avatarUrl: user.avatarUrl,
gitlabId: user.id,
));
} catch (err) {
Fimber.e('loginToGitlab failed', ex: err);