Skip to content

Commit

Permalink
feat(gitee): add token login step
Browse files Browse the repository at this point in the history
  • Loading branch information
pd4d10 committed Oct 13, 2020
1 parent d37a5c1 commit f480dc0
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 0 deletions.
28 changes: 28 additions & 0 deletions lib/models/auth.dart
Expand Up @@ -3,6 +3,7 @@ import 'dart:convert';
import 'dart:async';
import 'package:git_touch/models/bitbucket.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:github/github.dart';
import 'package:gql_http_link/gql_http_link.dart';
Expand All @@ -26,6 +27,7 @@ class PlatformType {
static const gitlab = 'gitlab';
static const bitbucket = 'bitbucket';
static const gitea = 'gitea';
static const gitee = 'gitee';
}

class DataWithPage<T> {
Expand Down Expand Up @@ -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 {
// Listen scheme
_sub = getUriLinksStream().listen(_onSchemeDetected, onError: (err) {
Expand Down
12 changes: 12 additions & 0 deletions lib/models/gitee.dart
@@ -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 changes: 18 additions & 0 deletions lib/models/gitee.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions lib/screens/login.dart
Expand Up @@ -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(
padding: CommonStyle.padding,
child: Text(
Expand Down

0 comments on commit f480dc0

Please sign in to comment.