From fd074e96eefd79b28461b542e65317ad78e0dc04 Mon Sep 17 00:00:00 2001 From: Vishnu Mohandas Date: Wed, 26 Aug 2020 04:32:43 +0530 Subject: [PATCH] Simplify EmailEntryPage --- lib/main.dart | 4 ++++ lib/ui/email_entry_page.dart | 21 +++++++++------------ lib/ui/ott_verification_page.dart | 12 ++++++------ lib/user_authenticator.dart | 16 +++++++--------- 4 files changed, 26 insertions(+), 27 deletions(-) diff --git a/lib/main.dart b/lib/main.dart index c392738d5..611872547 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -11,6 +11,7 @@ import 'package:photos/folder_service.dart'; import 'package:photos/memories_service.dart'; import 'package:photos/photo_sync_manager.dart'; import 'package:photos/ui/home_widget.dart'; +import 'package:photos/user_authenticator.dart'; import 'package:sentry/sentry.dart'; import 'package:super_logging/super_logging.dart'; import 'package:logging/logging.dart'; @@ -96,6 +97,9 @@ Future initDeepLinks() async { // Attach a listener to the stream getLinksStream().listen((String link) { _logger.info("Link received: " + link); + final ott = Uri.parse(link).queryParameters["ott"]; + _logger.info("Ott: " + ott); + // UserAuthenticator.instance.getCredentials(context, ott); }, onError: (err) { _logger.severe(err); }); diff --git a/lib/ui/email_entry_page.dart b/lib/ui/email_entry_page.dart index 5aa59ded0..723fbb0cd 100644 --- a/lib/ui/email_entry_page.dart +++ b/lib/ui/email_entry_page.dart @@ -2,23 +2,23 @@ import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import 'package:flutter/widgets.dart'; import 'package:flutter_svg/svg.dart'; +import 'package:photos/core/configuration.dart'; import 'package:photos/user_authenticator.dart'; class EmailEntryPage extends StatefulWidget { - final String email; - - EmailEntryPage({this.email, Key key}) : super(key: key); + EmailEntryPage({Key key}) : super(key: key); @override _EmailEntryPageState createState() => _EmailEntryPageState(); } class _EmailEntryPageState extends State { - String _email; + TextEditingController _emailController; @override void initState() { - _email = widget.email; + _emailController = + TextEditingController(text: Configuration.instance.getEmail()); super.initState(); } @@ -48,22 +48,19 @@ class _EmailEntryPageState extends State { hintText: 'email@domain.com', contentPadding: EdgeInsets.all(20), ), - initialValue: widget.email == null ? "" : widget.email, + controller: _emailController, autofocus: true, autocorrect: false, keyboardType: TextInputType.emailAddress, - onChanged: (email) { - setState(() { - _email = email; - }); - }, ), Padding(padding: EdgeInsets.all(8)), SizedBox( width: double.infinity, child: RaisedButton( onPressed: () { - UserAuthenticator.instance.getOtt(context, _email); + final email = _emailController.text; + Configuration.instance.setEmail(email); + UserAuthenticator.instance.getOtt(context, email); }, padding: const EdgeInsets.fromLTRB(8, 12, 8, 12), child: Text("Sign In"), diff --git a/lib/ui/ott_verification_page.dart b/lib/ui/ott_verification_page.dart index 22b91cd83..c211bc6a0 100644 --- a/lib/ui/ott_verification_page.dart +++ b/lib/ui/ott_verification_page.dart @@ -3,12 +3,12 @@ import 'dart:ui'; import 'package:flutter/material.dart'; import 'package:flutter/widgets.dart'; import 'package:flutter_svg/flutter_svg.dart'; +import 'package:photos/core/configuration.dart'; import 'package:photos/ui/email_entry_page.dart'; import 'package:photos/user_authenticator.dart'; class OTTVerificationPage extends StatefulWidget { - final String email; - OTTVerificationPage(this.email, {Key key}) : super(key: key); + OTTVerificationPage({Key key}) : super(key: key); @override _OTTVerificationPageState createState() => _OTTVerificationPageState(); @@ -48,7 +48,7 @@ class _OTTVerificationPageState extends State { children: [ TextSpan(text: "We've sent a mail to "), TextSpan( - text: widget.email, + text: Configuration.instance.getEmail(), style: TextStyle( color: Theme.of(context).accentColor, )), @@ -85,8 +85,8 @@ class _OTTVerificationPageState extends State { _verificationCodeController.text.isEmpty ? null : () { - UserAuthenticator.instance.getCredentials(context, - widget.email, _verificationCodeController.text); + UserAuthenticator.instance.getCredentials( + context, _verificationCodeController.text); }, padding: const EdgeInsets.fromLTRB(8, 12, 8, 12), child: Text( @@ -103,7 +103,7 @@ class _OTTVerificationPageState extends State { Navigator.of(context).push( MaterialPageRoute( builder: (BuildContext context) { - return EmailEntryPage(email: widget.email); + return EmailEntryPage(); }, ), ); diff --git a/lib/user_authenticator.dart b/lib/user_authenticator.dart index a67971a2a..6977e163a 100644 --- a/lib/user_authenticator.dart +++ b/lib/user_authenticator.dart @@ -36,7 +36,7 @@ class UserAuthenticator { Navigator.of(context).push( MaterialPageRoute( builder: (BuildContext context) { - return OTTVerificationPage(email); + return OTTVerificationPage(); }, ), ); @@ -46,14 +46,13 @@ class UserAuthenticator { }); } - Future getCredentials( - BuildContext context, String email, String ott) async { + Future getCredentials(BuildContext context, String ott) async { final dialog = createProgressDialog(context, "Please wait..."); await dialog.show(); await Dio().get( Configuration.instance.getHttpEndpoint() + "/users/credentials", queryParameters: { - "email": email, + "email": Configuration.instance.getEmail(), "ott": ott, }, ).catchError((e) async { @@ -63,7 +62,7 @@ class UserAuthenticator { }).then((response) async { await dialog.hide(); if (response.statusCode == 200) { - _saveConfiguration(email, response); + _saveConfiguration(response); Navigator.of(context).pop(); } else { showErrorDialog( @@ -80,7 +79,7 @@ class UserAuthenticator { "password": password, }).then((response) { if (response.statusCode == 200 && response.data != null) { - _saveConfiguration(username, response); + _saveConfiguration(response); Bus.instance.fire(UserAuthenticatedEvent()); return true; } else { @@ -99,7 +98,7 @@ class UserAuthenticator { "password": password, }).then((response) { if (response.statusCode == 200 && response.data != null) { - _saveConfiguration(username, response); + _saveConfiguration(response); return true; } else { if (response.data != null && response.data["message"] != null) { @@ -126,8 +125,7 @@ class UserAuthenticator { ); } - void _saveConfiguration(String email, Response response) { - Configuration.instance.setEmail(email); + void _saveConfiguration(Response response) { Configuration.instance.setUserID(response.data["id"]); Configuration.instance.setToken(response.data["token"]); final String encryptedKey = response.data["encryptedKey"];