1
0
mirror of https://github.com/git-touch/git-touch synced 2024-12-15 09:56:15 +01:00

feat(gitee): add token login step

This commit is contained in:
Rongjian Zhang 2020-10-14 00:43:11 +08:00
parent d37a5c1981
commit f480dc048e
4 changed files with 76 additions and 0 deletions

View File

@ -3,6 +3,7 @@ import 'dart:convert';
import 'dart:async'; import 'dart:async';
import 'package:git_touch/models/bitbucket.dart'; import 'package:git_touch/models/bitbucket.dart';
import 'package:git_touch/models/gitea.dart'; import 'package:git_touch/models/gitea.dart';
import 'package:git_touch/models/gitee.dart';
import 'package:git_touch/utils/request_serilizer.dart'; import 'package:git_touch/utils/request_serilizer.dart';
import 'package:github/github.dart'; import 'package:github/github.dart';
import 'package:gql_http_link/gql_http_link.dart'; import 'package:gql_http_link/gql_http_link.dart';
@ -26,6 +27,7 @@ class PlatformType {
static const gitlab = 'gitlab'; static const gitlab = 'gitlab';
static const bitbucket = 'bitbucket'; static const bitbucket = 'bitbucket';
static const gitea = 'gitea'; static const gitea = 'gitea';
static const gitee = 'gitee';
} }
class DataWithPage<T> { class DataWithPage<T> {
@ -314,6 +316,32 @@ class AuthModel with ChangeNotifier {
); );
} }
Future loginToGitee(String token) async {
token = token.trim();
try {
loading = true;
notifyListeners();
final res = await http.get('https://gitee.com/api/v5/user',
headers: {'Authorization': 'token $token'});
final info = json.decode(res.body);
if (info['message'] != null) {
throw info['message'];
}
final user = GiteeUser.fromJson(info);
await _addAccount(Account(
platform: PlatformType.gitea,
domain: 'https://gitee.com',
token: token,
login: user.login,
avatarUrl: user.avatarUrl,
));
} finally {
loading = false;
notifyListeners();
}
}
Future<void> init() async { Future<void> init() async {
// Listen scheme // Listen scheme
_sub = getUriLinksStream().listen(_onSchemeDetected, onError: (err) { _sub = getUriLinksStream().listen(_onSchemeDetected, onError: (err) {

12
lib/models/gitee.dart Normal file
View File

@ -0,0 +1,12 @@
import 'package:json_annotation/json_annotation.dart';
part 'gitee.g.dart';
@JsonSerializable(fieldRename: FieldRename.snake)
class GiteeUser {
String login;
String avatarUrl;
GiteeUser();
factory GiteeUser.fromJson(Map<String, dynamic> json) =>
_$GiteeUserFromJson(json);
}

18
lib/models/gitee.g.dart Normal file
View File

@ -0,0 +1,18 @@
// GENERATED CODE - DO NOT MODIFY BY HAND
part of 'gitee.dart';
// **************************************************************************
// JsonSerializableGenerator
// **************************************************************************
GiteeUser _$GiteeUserFromJson(Map<String, dynamic> json) {
return GiteeUser()
..login = json['login'] as String
..avatarUrl = json['avatar_url'] as String;
}
Map<String, dynamic> _$GiteeUserToJson(GiteeUser instance) => <String, dynamic>{
'login': instance.login,
'avatar_url': instance.avatarUrl,
};

View File

@ -293,6 +293,24 @@ class _LoginScreenState extends State<LoginScreen> {
} }
}, },
), ),
_buildAddItem(
text: 'Gitee Account',
brand: Octicons.git_branch, // TODO: brand icon
onTap: () async {
final result = await theme.showConfirm(
context,
_buildPopup(context),
);
if (result == true) {
try {
await auth.loginToGitee(_tokenController.text);
_tokenController.clear();
} catch (err) {
showError(err);
}
}
},
),
Container( Container(
padding: CommonStyle.padding, padding: CommonStyle.padding,
child: Text( child: Text(