From b8f59c4ebc93d3096f643af98fbec2e3bc3da81a Mon Sep 17 00:00:00 2001 From: shreyas1599 Date: Wed, 3 Feb 2021 11:39:25 +0530 Subject: [PATCH 1/3] fix(github): allow multiple accounts --- lib/models/auth.dart | 4 +-- lib/screens/login.dart | 63 +++++++++++++++++++++++++++++++++++------- 2 files changed, 55 insertions(+), 12 deletions(-) diff --git a/lib/models/auth.dart b/lib/models/auth.dart index 4bd751f3..709794b6 100644 --- a/lib/models/auth.dart +++ b/lib/models/auth.dart @@ -728,13 +728,13 @@ class AuthModel with ChangeNotifier { } String _oauthState; - void redirectToGithubOauth([publicOnly = false]) { + void redirectToGithubOauth(String login, [publicOnly = false]) { _oauthState = nanoid(); final repoScope = publicOnly ? 'public_repo' : 'repo'; final scope = Uri.encodeComponent( ['user', repoScope, 'read:org', 'notifications'].join(',')); launchUrl( - 'https://github.com/login/oauth/authorize?client_id=$clientId&redirect_uri=gittouch://login&scope=$scope&state=$_oauthState', + 'https://github.com/login/oauth/authorize?client_id=$clientId&redirect_uri=gittouch://login&scope=$scope&state=$_oauthState&login=$login', ); } diff --git a/lib/screens/login.dart b/lib/screens/login.dart index e7e2f689..23e85b05 100644 --- a/lib/screens/login.dart +++ b/lib/screens/login.dart @@ -111,17 +111,16 @@ class _LoginScreenState extends State { ); } - Widget _buildPopup( - BuildContext context, { - List notes, - bool showDomain = false, - }) { + Widget _buildPopup(BuildContext context, + {List notes, + bool showDomain = false, + String placeholder = 'Access token'}) { return Column( children: [ if (showDomain) MyTextField(controller: _domainController, placeholder: 'Domain'), SizedBox(height: 8), - MyTextField(placeholder: 'Access token', controller: _tokenController), + MyTextField(placeholder: placeholder, controller: _tokenController), SizedBox(height: 8), if (notes != null) ...notes, ], @@ -133,6 +132,14 @@ class _LoginScreenState extends State { Text(AppLocalizations.of(context).somethingBadHappens + '$err')); } + // TODO: handle email + bool _checkAccountExists(BuildContext context, String domain, String login) { + final auth = context.read(); + final accountExists = auth.accounts + .where((account) => account.domain == domain && account.login == login); + return accountExists.isNotEmpty; + } + @override Widget build(BuildContext context) { final auth = Provider.of(context); @@ -152,14 +159,50 @@ class _LoginScreenState extends State { theme.showActions(context, [ ActionItem( text: 'via OAuth', - onTap: (_) { - auth.redirectToGithubOauth(); + onTap: (_) async { + await theme.showConfirm( + context, + _buildPopup( + context, + placeholder: 'Username or email', + ), + ); + + bool accountExists = _checkAccountExists(context, + 'https://github.com', _tokenController.text); + + if (accountExists) { + await theme.showWarning( + context, "Account already exists"); + } else { + auth.redirectToGithubOauth(_tokenController.text); + } + + _tokenController.clear(); }, ), ActionItem( text: 'via OAuth (Public repos only)', - onTap: (_) { - auth.redirectToGithubOauth(true); + onTap: (_) async { + await theme.showConfirm( + context, + _buildPopup( + context, + placeholder: 'Username or email', + )); + + bool accountExists = _checkAccountExists(context, + 'https://github.com', _tokenController.text); + + if (accountExists) { + await theme.showWarning( + context, "Account already exists"); + } else { + auth.redirectToGithubOauth( + _tokenController.text, true); + } + + _tokenController.clear(); }, ), ActionItem( From 79791c53b99f33fd1e7f807444820deda9ac64a7 Mon Sep 17 00:00:00 2001 From: shreyas1599 Date: Sun, 14 Feb 2021 23:30:52 +0530 Subject: [PATCH 2/3] fix: remove email, use any() --- lib/screens/login.dart | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/screens/login.dart b/lib/screens/login.dart index 23e85b05..f43bb0a7 100644 --- a/lib/screens/login.dart +++ b/lib/screens/login.dart @@ -136,8 +136,8 @@ class _LoginScreenState extends State { bool _checkAccountExists(BuildContext context, String domain, String login) { final auth = context.read(); final accountExists = auth.accounts - .where((account) => account.domain == domain && account.login == login); - return accountExists.isNotEmpty; + .any((account) => account.domain == domain && account.login == login); + return accountExists; } @override @@ -164,7 +164,7 @@ class _LoginScreenState extends State { context, _buildPopup( context, - placeholder: 'Username or email', + placeholder: 'Username', ), ); From bc3dc4823305afadb1ccc19c64d840d7be15667e Mon Sep 17 00:00:00 2001 From: shreyas1599 Date: Mon, 21 Jun 2021 03:01:17 +0530 Subject: [PATCH 3/3] fix: any() null check error --- lib/screens/login.dart | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/screens/login.dart b/lib/screens/login.dart index c0c4dcc7..23751095 100644 --- a/lib/screens/login.dart +++ b/lib/screens/login.dart @@ -128,8 +128,9 @@ class _LoginScreenState extends State { // TODO: handle email bool _checkAccountExists(BuildContext context, String domain, String login) { final auth = context.read(); - final accountExists = auth.accounts - .any((account) => account.domain == domain && account.login == login); + final accountExists = auth.accounts?.any( + (account) => account.domain == domain && account.login == login) ?? + false; return accountExists; }