Asset-Borrowing-App / lib / Project / ProfilePage / EditProfilePage / confirm_password.dart
confirm_password.dart
Raw
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';

import 'edit_email.dart';
import 'edit_name.dart';
import '../widgets/appbar_widget.dart';

const Color cColor = Color.fromARGB(255, 58, 168, 50);

class ConfirmPasswordPage extends StatefulWidget {
  const ConfirmPasswordPage({Key? key}) : super(key: key);

  @override
  _ConfirmPasswordPageState createState() => _ConfirmPasswordPageState();
}

class _ConfirmPasswordPageState extends State<ConfirmPasswordPage> {
  final _formKey = GlobalKey<FormState>();
  final passwordController = TextEditingController();
  bool isValid = true;
  bool _obscureText = true;

  @override
  void initState() {
    super.initState();
    passwordController.addListener(() {});
  }

  @override
  void dispose() {
    passwordController.dispose();
    super.dispose();
  }

  void clearForm() {
    passwordController.clear();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: buildAppBar(context),
      body: Form(
        key: _formKey,
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.center,
          mainAxisAlignment: MainAxisAlignment.start,
          children: <Widget>[
            const SizedBox(
              width: 320,
              child: Text(
                'To change your email, please enter your password:',
                style: TextStyle(fontSize: 22, fontWeight: FontWeight.bold),
                textAlign: TextAlign.left,
              ),
            ),
            Padding(
              padding: const EdgeInsets.only(top: 40),
              child: SizedBox(
                height: 100,
                width: 320,
                child: TextFormField(
                  keyboardType: TextInputType.text,
                  obscureText: _obscureText,
                  // Handles Form Validation
                  validator: (value) {
                    if (value == null || value.isEmpty) {
                      return 'Please enter your password';
                    }
                    if (value.length < 8) {
                      return 'Password must be at least 8 characters long';
                    }
                    return null;
                  },
                  decoration: InputDecoration(
                    labelText: 'Password',
                    suffixIcon: IconButton(
                      icon: Icon(
                        _obscureText ? Icons.visibility : Icons.visibility_off,
                        color: Colors.grey,
                      ),
                      onPressed: () {
                        setState(() {
                          _obscureText = !_obscureText;
                        });
                      },
                    ),
                  ),
                  controller: passwordController,
                ),
              ),
            ),
            Padding(
              padding: const EdgeInsets.only(top: 150),
              child: Align(
                alignment: Alignment.bottomCenter,
                child: SizedBox(
                  width: 320,
                  height: 50,
                  child: ElevatedButton(
                    onPressed: () async {
                      setState(() {
                        isValid = _formKey.currentState!.validate();
                      });

                      if (isValid) {
                        final String password = passwordController.text.trim();

                        try {
                          // Reauthenticate the user with their password
                          final user = FirebaseAuth.instance.currentUser;
                          final credential = EmailAuthProvider.credential(
                              email: user!.email!, password: password);
                          await user.reauthenticateWithCredential(credential);

                          // Navigate to the page to change the email
                          // ignore: use_build_context_synchronously
                          Navigator.push(
                            context,
                            MaterialPageRoute(
                              builder: ((context) => const EditEmailFormPage()),
                            ),
                          );
                        } on FirebaseAuthException catch (e) {
                          // Handle the error
                          ScaffoldMessenger.of(context).showSnackBar(
                            SnackBar(
                              content: Text(e.message!),
                              duration: const Duration(seconds: 3),
                            ),
                          );
                        }
                      }
                    },
                    style: ButtonStyle(
                      backgroundColor: MaterialStateProperty.all<Color>(cColor),
                    ),
                    child: const Text(
                      'Authenticate',
                      style: TextStyle(fontSize: 15),
                    ),
                  ),
                ),
              ),
            )
          ],
        ),
      ),
    );
  }
}