Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/models/auth.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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',
);
}

Expand Down
63 changes: 53 additions & 10 deletions lib/screens/login.dart
Original file line number Diff line number Diff line change
Expand Up @@ -111,17 +111,16 @@ class _LoginScreenState extends State<LoginScreen> {
);
}

Widget _buildPopup(
BuildContext context, {
List<Widget> notes,
bool showDomain = false,
}) {
Widget _buildPopup(BuildContext context,
{List<Widget> notes,
bool showDomain = false,
String placeholder = 'Access token'}) {
return Column(
children: <Widget>[
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,
],
Expand All @@ -133,6 +132,14 @@ class _LoginScreenState extends State<LoginScreen> {
Text(AppLocalizations.of(context).somethingBadHappens + '$err'));
}

// TODO: handle email
bool _checkAccountExists(BuildContext context, String domain, String login) {
final auth = context.read<AuthModel>();
final accountExists = auth.accounts
.where((account) => account.domain == domain && account.login == login);
return accountExists.isNotEmpty;
}

@override
Widget build(BuildContext context) {
final auth = Provider.of<AuthModel>(context);
Expand All @@ -152,14 +159,50 @@ class _LoginScreenState extends State<LoginScreen> {
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(
Expand Down