1
0
mirror of https://github.com/git-touch/git-touch synced 2025-01-31 16:14:49 +01:00

feat: login by access token

This commit is contained in:
Rongjian Zhang 2019-10-03 12:55:17 +08:00
parent 206e533463
commit 8869c6b456
5 changed files with 58 additions and 15 deletions

View File

@ -75,10 +75,10 @@ class AuthModel with ChangeNotifier {
}), }),
); );
final token = json.decode(res.body)['access_token'] as String; final token = json.decode(res.body)['access_token'] as String;
await _loginWithToken(token); await loginWithToken(token);
} }
Future<void> _loginWithToken(String token) async { Future<void> loginWithToken(String token) async {
// Get login and avatar url // Get login and avatar url
final queryData = await query(''' final queryData = await query('''
{ {

View File

@ -117,14 +117,14 @@ class ThemeModel with ChangeNotifier {
return Navigator.of(context).pushReplacement(StaticRoute(builder: builder)); return Navigator.of(context).pushReplacement(StaticRoute(builder: builder));
} }
Future<bool> showConfirm(BuildContext context, String text) { Future<bool> showConfirm(BuildContext context, Widget content) {
switch (theme) { switch (theme) {
case AppThemeType.cupertino: case AppThemeType.cupertino:
return showCupertinoDialog( return showCupertinoDialog(
context: context, context: context,
builder: (context) { builder: (context) {
return CupertinoAlertDialog( return CupertinoAlertDialog(
title: Text(text), title: content,
actions: <Widget>[ actions: <Widget>[
CupertinoDialogAction( CupertinoDialogAction(
child: const Text('cancel'), child: const Text('cancel'),
@ -148,10 +148,7 @@ class ThemeModel with ChangeNotifier {
context: context, context: context,
builder: (BuildContext context) { builder: (BuildContext context) {
return AlertDialog( return AlertDialog(
content: Text( content: content,
text,
// style: dialogTextStyle
),
actions: <Widget>[ actions: <Widget>[
FlatButton( FlatButton(
child: const Text('CANCEL'), child: const Text('CANCEL'),

View File

@ -66,7 +66,7 @@ class _IssueFormScreenState extends State<IssueFormScreen> {
throw 'Create fail, response: ${res.body}'; throw 'Create fail, response: ${res.body}';
} }
await Provider.of<ThemeModel>(context) await Provider.of<ThemeModel>(context)
.showConfirm(context, 'Issue created'); .showConfirm(context, Text('Issue created'));
Navigator.of(context).pop(); Navigator.of(context).pop();
} catch (err) { } catch (err) {
print(err); // TODO: print(err); // TODO:

View File

@ -1,8 +1,11 @@
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:git_touch/models/auth.dart'; import 'package:git_touch/models/auth.dart';
import 'package:git_touch/models/theme.dart';
import 'package:git_touch/scaffolds/single.dart'; import 'package:git_touch/scaffolds/single.dart';
import 'package:git_touch/utils/utils.dart'; import 'package:git_touch/utils/utils.dart';
import 'package:git_touch/widgets/app_bar_title.dart'; import 'package:git_touch/widgets/app_bar_title.dart';
import 'package:primer/primer.dart';
import 'package:provider/provider.dart'; import 'package:provider/provider.dart';
import '../widgets/link.dart'; import '../widgets/link.dart';
import '../widgets/loading.dart'; import '../widgets/loading.dart';
@ -15,6 +18,8 @@ class LoginScreen extends StatefulWidget {
} }
class _LoginScreenState extends State<LoginScreen> { class _LoginScreenState extends State<LoginScreen> {
String _token = '';
Widget _buildAccountItem(int index) { Widget _buildAccountItem(int index) {
final settings = Provider.of<AuthModel>(context); final settings = Provider.of<AuthModel>(context);
final account = settings.accounts[index]; final account = settings.accounts[index];
@ -73,20 +78,61 @@ class _LoginScreenState extends State<LoginScreen> {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
final settings = Provider.of<AuthModel>(context); final auth = Provider.of<AuthModel>(context);
return SingleScaffold( return SingleScaffold(
title: AppBarTitle('Select account'), title: AppBarTitle('Select account'),
body: settings.loading body: auth.loading
? Center(child: Loading()) ? Center(child: Loading())
: Container( : Container(
child: Column( child: Column(
children: [ children: [
...List.generate(settings.accounts.length, _buildAccountItem), ...List.generate(auth.accounts.length, _buildAccountItem),
_buildAddItem( _buildAddItem(
text: 'GitHub Account', text: 'GitHub Account by OAuth',
onTap: settings.redirectToGithubOauth, onTap: auth.redirectToGithubOauth,
), ),
_buildAddItem(
text: 'GitHub Account by Token',
onTap: () async {
var result =
await Provider.of<ThemeModel>(context).showConfirm(
context,
Column(
children: <Widget>[
CupertinoTextField(
placeholder: 'Access token',
onChanged: (v) {
setState(() {
_token = v;
});
},
),
SizedBox(height: 8),
Text(
'GitTouch needs these permissions',
style: TextStyle(
fontSize: 14, fontWeight: FontWeight.w400),
),
SizedBox(height: 8),
Text(
'user, repo, read:org',
style: TextStyle(
fontSize: 16, color: PrimerColors.blue500),
)
],
),
);
if (result == true) {
try {
await auth.loginWithToken(_token);
} catch (err) {
Provider.of<ThemeModel>(context).showConfirm(
context, Text('Token invalid: $err'));
}
}
},
)
// _buildAddItem( // _buildAddItem(
// text: 'GitLab Account', // text: 'GitLab Account',
// screenBuilder: (_) => LoginGitlabScreen(), // screenBuilder: (_) => LoginGitlabScreen(),

View File

@ -162,7 +162,7 @@ $key: pullRequest(number: ${item.number}) {
iconData: Icons.done_all, iconData: Icons.done_all,
onTap: () async { onTap: () async {
var value = await Provider.of<ThemeModel>(context) var value = await Provider.of<ThemeModel>(context)
.showConfirm(context, 'Mark all as read?'); .showConfirm(context, Text('Mark all as read?'));
if (value) { if (value) {
await Provider.of<AuthModel>(context) await Provider.of<AuthModel>(context)
.putWithCredentials('/notifications'); .putWithCredentials('/notifications');