From fbd6450a26fa117f6b12f85ea6f1e31521fde11b Mon Sep 17 00:00:00 2001 From: ravindranag Date: Mon, 17 Apr 2023 22:04:27 +0530 Subject: [PATCH] feat: firebase login --- .../controller/login_controller.dart | 14 ++ ...Controller.dart => signup_controller.dart} | 0 .../screen/login/login_screen.dart | 2 +- .../screen/login/widgets/LoginForm.dart | 150 ------------------ .../screen/login/widgets/login_form.dart | 144 +++++++++++++++++ .../screen/signup/widgets/signup_form.dart | 2 +- .../widgets/dashboard_sliver_app_bar.dart | 20 ++- 7 files changed, 174 insertions(+), 158 deletions(-) create mode 100644 lib/src/features/authentication/controller/login_controller.dart rename lib/src/features/authentication/controller/{SignUpController.dart => signup_controller.dart} (100%) delete mode 100644 lib/src/features/authentication/screen/login/widgets/LoginForm.dart create mode 100644 lib/src/features/authentication/screen/login/widgets/login_form.dart diff --git a/lib/src/features/authentication/controller/login_controller.dart b/lib/src/features/authentication/controller/login_controller.dart new file mode 100644 index 0000000..7883a2a --- /dev/null +++ b/lib/src/features/authentication/controller/login_controller.dart @@ -0,0 +1,14 @@ +import 'package:courses_app/src/repository/auth_repository.dart'; +import 'package:flutter/cupertino.dart'; +import 'package:get/get.dart'; + +class LoginController extends GetxController { + static LoginController get instance => Get.find(); + + final email = TextEditingController(); + final password = TextEditingController(); + + void loginExistingUser(String email, String password) { + AuthRepository.instance.loginUserWithEmailAndPassword(email, password); + } +} \ No newline at end of file diff --git a/lib/src/features/authentication/controller/SignUpController.dart b/lib/src/features/authentication/controller/signup_controller.dart similarity index 100% rename from lib/src/features/authentication/controller/SignUpController.dart rename to lib/src/features/authentication/controller/signup_controller.dart diff --git a/lib/src/features/authentication/screen/login/login_screen.dart b/lib/src/features/authentication/screen/login/login_screen.dart index 083fdaf..95bfc30 100644 --- a/lib/src/features/authentication/screen/login/login_screen.dart +++ b/lib/src/features/authentication/screen/login/login_screen.dart @@ -1,6 +1,6 @@ import 'package:flutter/material.dart'; -import 'widgets/LoginForm.dart'; +import 'widgets/login_form.dart'; import 'widgets/login_screen_header.dart'; class LoginScreen extends StatelessWidget { diff --git a/lib/src/features/authentication/screen/login/widgets/LoginForm.dart b/lib/src/features/authentication/screen/login/widgets/LoginForm.dart deleted file mode 100644 index 784df27..0000000 --- a/lib/src/features/authentication/screen/login/widgets/LoginForm.dart +++ /dev/null @@ -1,150 +0,0 @@ -import 'package:courses_app/src/constants/images.dart'; -import 'package:courses_app/src/constants/text.dart'; -import 'package:courses_app/src/features/authentication/screen/signup/signup_screen.dart'; -import 'package:courses_app/src/features/dashboard/screen/dashboard/dashboard_screen.dart'; -import 'package:courses_app/src/widgets/common/outlined_password_text_field.dart'; -import 'package:courses_app/src/widgets/common/outlined_text_field.dart'; -import 'package:flutter/material.dart'; - -import 'forgot_password_bottom_sheet.dart'; - -class LoginForm extends StatefulWidget { - const LoginForm({ - super.key, - }); - - @override - State createState() => _LoginFormState(); -} - -class _LoginFormState extends State { - final TextEditingController _emailController = TextEditingController(); - - final TextEditingController _passwordController = TextEditingController(); - - void _handleLogin() { - print({_emailController.text, _passwordController.text}); - Navigator.pushAndRemoveUntil( - context, - MaterialPageRoute( - builder: (context) { - return const DashboardScreen(); - }, - ), - (route) => false, - ); - } - - @override - Widget build(BuildContext context) { - return Column( - children: [ - Padding( - padding: const EdgeInsets.symmetric(vertical: 30.0), - child: Column( - children: [ - OutlinedTextField( - label: const Text( - 'Email', - ), - controller: _emailController, - ), - const SizedBox( - height: 16.0, - ), - OutlinedPasswordTextField( - label: const Text('Password'), - controller: _passwordController, - ) - ], - ), - ), - Padding( - padding: const EdgeInsets.symmetric(vertical: 10.0), - child: Column( - children: [ - TextButton( - onPressed: () { - showModalBottomSheet( - context: context, - // enableDrag: true, - isScrollControlled: true, - useSafeArea: true, - builder: (context) { - return const ForgotPasswordBottomSheet(); - }); - }, - child: const Text( - 'Forgot password?', - ), - ), - SizedBox( - width: double.infinity, - child: FilledButton( - style: FilledButton.styleFrom(), - onPressed: _handleLogin, - child: const Text( - 'Login', - ), - ), - ), - ], - ), - ), - const Text('OR'), - Padding( - padding: const EdgeInsets.symmetric(vertical: 10.0), - child: OutlinedButton( - onPressed: () {}, - child: Row( - mainAxisAlignment: MainAxisAlignment.center, - children: [ - Container( - decoration: BoxDecoration( - borderRadius: BorderRadius.circular(50.0), - image: const DecorationImage( - image: AssetImage(googleLogo), fit: BoxFit.contain), - // shape: BoxShape.rectangle - ), - width: 24, - height: 24, - ), - const SizedBox( - width: 8.0, - ), - const Text('Login with Google') - ], - ), - ), - ), - Padding( - padding: const EdgeInsets.symmetric(vertical: 16.0), - child: TextButton( - onPressed: () { - Navigator.push( - context, - MaterialPageRoute( - builder: (context) { - return const SignUpScreen(); - }, - ), - ); - }, - child: Text.rich(TextSpan( - text: "Don't have an account?", - style: Theme.of(context).textTheme.bodyLarge, - children: [ - TextSpan( - text: ' Signup', - style: Theme.of(context) - .textTheme - .bodyLarge - ?.apply(color: Theme.of(context).colorScheme.primary), - ) - ])), - ), - ) - ], - ); - } -} diff --git a/lib/src/features/authentication/screen/login/widgets/login_form.dart b/lib/src/features/authentication/screen/login/widgets/login_form.dart new file mode 100644 index 0000000..073867d --- /dev/null +++ b/lib/src/features/authentication/screen/login/widgets/login_form.dart @@ -0,0 +1,144 @@ +import 'package:courses_app/src/constants/images.dart'; +import 'package:courses_app/src/constants/text.dart'; +import 'package:courses_app/src/features/authentication/controller/login_controller.dart'; +import 'package:courses_app/src/features/authentication/screen/signup/signup_screen.dart'; +import 'package:courses_app/src/features/dashboard/screen/dashboard/dashboard_screen.dart'; +import 'package:courses_app/src/widgets/common/outlined_password_text_field.dart'; +import 'package:courses_app/src/widgets/common/outlined_text_field.dart'; +import 'package:flutter/material.dart'; +import 'package:get/get.dart'; + +import 'forgot_password_bottom_sheet.dart'; + +class LoginForm extends StatelessWidget { + const LoginForm({ + super.key, + }); + + @override + Widget build(BuildContext context) { + final controller = Get.put(LoginController()); + + final formKey = GlobalKey(); + + void handleLogin() { + LoginController.instance.loginExistingUser( + controller.email.text.trim(), + controller.password.text.trim(), + ); + } + + return Form( + key: formKey, + child: Column( + children: [ + Padding( + padding: const EdgeInsets.symmetric(vertical: 30.0), + child: Column( + children: [ + OutlinedTextField( + label: const Text( + 'Email', + ), + controller: controller.email, + ), + const SizedBox( + height: 16.0, + ), + OutlinedPasswordTextField( + label: const Text('Password'), + controller: controller.password, + ) + ], + ), + ), + Padding( + padding: const EdgeInsets.symmetric(vertical: 10.0), + child: Column( + children: [ + TextButton( + onPressed: () { + showModalBottomSheet( + context: context, + // enableDrag: true, + isScrollControlled: true, + useSafeArea: true, + builder: (context) { + return const ForgotPasswordBottomSheet(); + }); + }, + child: const Text( + 'Forgot password?', + ), + ), + SizedBox( + width: double.infinity, + child: FilledButton( + style: FilledButton.styleFrom(), + onPressed: handleLogin, + child: const Text( + 'Login', + ), + ), + ), + ], + ), + ), + const Text('OR'), + Padding( + padding: const EdgeInsets.symmetric(vertical: 10.0), + child: OutlinedButton( + onPressed: () {}, + child: Row( + mainAxisAlignment: MainAxisAlignment.center, + children: [ + Container( + decoration: BoxDecoration( + borderRadius: BorderRadius.circular(50.0), + image: const DecorationImage( + image: AssetImage(googleLogo), fit: BoxFit.contain), + // shape: BoxShape.rectangle + ), + width: 24, + height: 24, + ), + const SizedBox( + width: 8.0, + ), + const Text('Login with Google') + ], + ), + ), + ), + Padding( + padding: const EdgeInsets.symmetric(vertical: 16.0), + child: TextButton( + onPressed: () { + Navigator.push( + context, + MaterialPageRoute( + builder: (context) { + return const SignUpScreen(); + }, + ), + ); + }, + child: Text.rich(TextSpan( + text: "Don't have an account?", + style: Theme.of(context).textTheme.bodyLarge, + children: [ + TextSpan( + text: ' Signup', + style: Theme.of(context) + .textTheme + .bodyLarge + ?.apply(color: Theme.of(context).colorScheme.primary), + ) + ])), + ), + ) + ], + ), + ); + } +} diff --git a/lib/src/features/authentication/screen/signup/widgets/signup_form.dart b/lib/src/features/authentication/screen/signup/widgets/signup_form.dart index 6a566a0..a2124da 100644 --- a/lib/src/features/authentication/screen/signup/widgets/signup_form.dart +++ b/lib/src/features/authentication/screen/signup/widgets/signup_form.dart @@ -1,5 +1,5 @@ import 'package:courses_app/src/constants/images.dart'; -import 'package:courses_app/src/features/authentication/controller/SignUpController.dart'; +import 'package:courses_app/src/features/authentication/controller/signup_controller.dart'; import 'package:courses_app/src/features/authentication/screen/login/login_screen.dart'; import 'package:courses_app/src/widgets/common/outlined_password_text_field.dart'; import 'package:courses_app/src/widgets/common/outlined_text_field.dart'; diff --git a/lib/src/features/dashboard/screen/dashboard/widgets/dashboard_sliver_app_bar.dart b/lib/src/features/dashboard/screen/dashboard/widgets/dashboard_sliver_app_bar.dart index 5b51ecd..8f03099 100644 --- a/lib/src/features/dashboard/screen/dashboard/widgets/dashboard_sliver_app_bar.dart +++ b/lib/src/features/dashboard/screen/dashboard/widgets/dashboard_sliver_app_bar.dart @@ -1,3 +1,4 @@ +import 'package:courses_app/src/repository/auth_repository.dart'; import 'package:flutter/material.dart'; class DashboardSliverAppBar extends StatelessWidget { @@ -13,9 +14,6 @@ class DashboardSliverAppBar extends StatelessWidget { return SliverAppBar( pinned: true, expandedHeight: 200.0, - // titleTextStyle: Theme.of(context).textTheme.headlineMedium?.apply( - // color: onPrimaryContainer - // ), flexibleSpace: FlexibleSpaceBar( title: Stack( // mainAxisSize: MainAxisSize.min, @@ -37,15 +35,25 @@ class DashboardSliverAppBar extends StatelessWidget { child: Text( 'Hi Ravindra', style: TextStyle( - color: Theme.of(context).colorScheme.inverseSurface - ), + color: Theme.of(context).colorScheme.inverseSurface), ), ), ], ), titlePadding: - const EdgeInsetsDirectional.only(start: 16, bottom: 16), + const EdgeInsetsDirectional.only(start: 16, bottom: 16), ), + actions: [ + IconButton( + onPressed: () { + AuthRepository.instance.logout(); + }, + icon: Icon( + Icons.power_settings_new, + color: Theme.of(context).colorScheme.error, + ), + ) + ], leading: null, automaticallyImplyLeading: false, );