Skip to content

Commit

Permalink
Login by pasting magic link
Browse files Browse the repository at this point in the history
  • Loading branch information
felipebueno committed Nov 15, 2023
1 parent 347d898 commit 6ad4c7c
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 51 deletions.
73 changes: 68 additions & 5 deletions lib/views/pages/auth/check_email_page.dart
Original file line number Diff line number Diff line change
@@ -1,16 +1,29 @@
import 'package:flutter/material.dart';
import 'package:stacker_news/data/api.dart';
import 'package:stacker_news/main.dart';
import 'package:stacker_news/utils.dart';
import 'package:stacker_news/views/pages/home_page.dart';
import 'package:stacker_news/views/widgets/generic_page_scaffold.dart';

class CheckEmailPage extends StatelessWidget {
class CheckEmailPage extends StatefulWidget {
static const String id = 'check_email';

const CheckEmailPage({super.key});

@override
State<CheckEmailPage> createState() => _CheckEmailPageState();
}

class _CheckEmailPageState extends State<CheckEmailPage> {
final _busy = ValueNotifier<bool>(false);
final _tokenController = TextEditingController();

@override
Widget build(BuildContext context) {
return GenericPageScaffold(
body: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
const Text(
'Check your email',
Expand All @@ -20,13 +33,63 @@ class CheckEmailPage extends StatelessWidget {
),
const SizedBox(height: 8),
const Text(
'A sign in link has been sent to your email address',
style: TextStyle(
fontSize: 16,
),
'A sign in link has been sent to your email address.\nYou can click it to open directly in the app or, if it doesn\'t work, you can copy the link and paste below.',
style: TextStyle(fontSize: 16),
textAlign: TextAlign.center,
),
const SizedBox(height: 16),
Image.asset('assets/hello.gif'),
TextField(
controller: _tokenController,
decoration: const InputDecoration(
labelText: 'Paste Magic Link Here',
hintText: 'https://stacker.news/api/auth/callback/email/...',
),
),
const SizedBox(height: 24),
ElevatedButton(
onPressed: () async {
if (_tokenController.text.isEmpty) {
return;
}

try {
_busy.value = true;
final session =
await locator<Api>().login(_tokenController.text);

if (session != null) {
if (context.mounted) {
Navigator.pushReplacementNamed(context, HomePage.id);
} else {
Utils.showError(
'Error going to home page. Context is not mounted',
);
}
}
} catch (e) {
Utils.showError('Error logging in: ${e.toString()}');
} finally {
_busy.value = false;
}
},
child: const Text('Login'),
),
ValueListenableBuilder<bool>(
valueListenable: _busy,
builder: (context, busy, child) {
if (!busy) {
return const SizedBox.shrink();
}

return Container(
color: Colors.black.withOpacity(0.5),
child: const Center(
child: CircularProgressIndicator(),
),
);
},
),
],
),
);
Expand Down
47 changes: 1 addition & 46 deletions lib/views/pages/auth/sign_in_page.dart
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
import 'dart:io';

import 'package:flutter/material.dart';
import 'package:stacker_news/data/api.dart';
import 'package:stacker_news/main.dart';
import 'package:stacker_news/utils.dart';
import 'package:stacker_news/views/pages/auth/check_email_page.dart';
import 'package:stacker_news/views/pages/home_page.dart';
import 'package:stacker_news/views/widgets/generic_page_scaffold.dart';

class SignInPage extends StatelessWidget {
Expand Down Expand Up @@ -76,9 +72,7 @@ class _SignInFormState extends State<SignInForm> {
.requestMagicLink(_emailController.text);
_busy.value = false;

if (ret &&
(Platform.isAndroid || Platform.isIOS) &&
context.mounted) {
if (ret && context.mounted) {
Navigator.pushNamed(context, CheckEmailPage.id);
}
},
Expand All @@ -96,45 +90,6 @@ class _SignInFormState extends State<SignInForm> {
// icon: const Icon(Icons.qr_code),
// label: const Text('Read QRCode'),
// ),
if (!Platform.isAndroid && !Platform.isIOS)
TextField(
controller: _tokenController,
decoration: const InputDecoration(
labelText: 'Paste Magic Link',
hintText: 'https://stacker.news/api/auth/callback/email/...',
),
),
if (!Platform.isAndroid && !Platform.isIOS)
const SizedBox(height: 24),
if (!Platform.isAndroid && !Platform.isIOS)
ElevatedButton(
onPressed: () async {
if (_tokenController.text.isEmpty) {
return;
}

try {
_busy.value = true;
final session =
await locator<Api>().login(_tokenController.text);

if (session != null) {
if (context.mounted) {
Navigator.pushReplacementNamed(context, HomePage.id);
} else {
Utils.showError(
'Error going to home page. Context is not mounted',
);
}
}
} catch (e) {
Utils.showError('Error logging in: ${e.toString()}');
} finally {
_busy.value = false;
}
},
child: const Text('Login'),
),
],
),
ValueListenableBuilder<bool>(
Expand Down

0 comments on commit 6ad4c7c

Please sign in to comment.