Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

auth.currentUser?.email still contains old email after email change #986

Open
Mr-Pepe opened this issue Jul 24, 2024 · 0 comments
Open

auth.currentUser?.email still contains old email after email change #986

Mr-Pepe opened this issue Jul 24, 2024 · 0 comments
Labels
auth This issue or pull request is related to authentication bug Something isn't working

Comments

@Mr-Pepe
Copy link

Mr-Pepe commented Jul 24, 2024

Describe the bug

auth.currentUser?.email does not get set to the new email address after calling auth.updateUser with a new email address and clicking the link in both confirmation emails.

To Reproduce

  1. flutter create supabase_auth

  2. flutter pub add supabase_flutter

  3. Paste the following into main.dart

    main.dart

    import 'dart:async';
    
    import 'package:flutter/material.dart';
    import 'package:flutter_web_plugins/url_strategy.dart';
    import 'package:supabase_flutter/supabase_flutter.dart';
    
    final supabase = Supabase.instance.client;
    
    Future<void> main() async {
      usePathUrlStrategy();
      await Supabase.initialize(
        url: const String.fromEnvironment("SUPABASE_URL"),
        anonKey: const String.fromEnvironment("SUPABASE_ANON_KEY"),
      );
    
      runApp(const MyApp());
    }
    
    class MyApp extends StatelessWidget {
      const MyApp({super.key});
    
      @override
      Widget build(BuildContext context) {
        return const MaterialApp(
          home: HomePage(),
        );
      }
    }
    
    class HomePage extends StatefulWidget {
      const HomePage({super.key});
    
      @override
      State<HomePage> createState() => _HomePageState();
    }
    
    class _HomePageState extends State<HomePage> {
      final _signUpEmailController = TextEditingController();
      final _signUpPasswordController = TextEditingController();
    
      final _signInEmailController = TextEditingController();
      final _signInPasswordController = TextEditingController();
    
      final _oldEmailController = TextEditingController();
      final _newEmailController = TextEditingController();
    
      StreamSubscription? _authChangeListener;
    
      @override
      void initState() {
        super.initState();
        _authChangeListener = supabase.auth.onAuthStateChange.listen((_) {
          setState(() {});
        });
      }
    
      @override
      void dispose() {
        _authChangeListener?.cancel();
        _signUpEmailController.dispose();
        _signUpPasswordController.dispose();
        _signInEmailController.dispose();
        _signInPasswordController.dispose();
        _oldEmailController.dispose();
        _newEmailController.dispose();
        super.dispose();
      }
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
            appBar: AppBar(title: const Text("Supabase Auth")),
            body: Center(
                child: ConstrainedBox(
              constraints: const BoxConstraints(maxWidth: 400),
              child: Column(
                children: [
                  if (supabase.auth.currentSession != null)
                    Text("Logged in as ${supabase.auth.currentUser!.email}")
                  else
                    const Text("Not logged in"),
                  const SizedBox(height: 50),
                  const Text("Sign up"),
                  TextFormField(
                    controller: _signUpEmailController,
                    decoration: const InputDecoration(label: Text("Email")),
                  ),
                  TextFormField(
                    controller: _signUpPasswordController,
                    decoration: const InputDecoration(label: Text("Password")),
                  ),
                  const SizedBox(height: 20),
                  ElevatedButton(
                    onPressed: () async {
                      await supabase.auth.signUp(
                          email: _signUpEmailController.text.trim(),
                          password: _signUpPasswordController.text.trim(),
                          emailRedirectTo: "http://localhost:8000/");
                    },
                    child: const Text("Sign up"),
                  ),
                  const SizedBox(height: 50),
                  const Text("Sign in"),
                  TextFormField(
                    controller: _signInEmailController,
                    decoration: const InputDecoration(label: Text("Email")),
                  ),
                  TextFormField(
                    controller: _signInPasswordController,
                    decoration: const InputDecoration(label: Text("Password")),
                  ),
                  const SizedBox(height: 20),
                  ElevatedButton(
                    onPressed: () async {
                      await supabase.auth.signInWithPassword(
                          email: _signInEmailController.text.trim(),
                          password: _signInPasswordController.text.trim());
                    },
                    child: const Text("Sign in"),
                  ),
                  const SizedBox(height: 50),
                  const Text("Change email"),
                  TextFormField(
                    controller: _oldEmailController,
                    decoration: const InputDecoration(label: Text("Old email")),
                  ),
                  TextFormField(
                    controller: _newEmailController,
                    decoration: const InputDecoration(label: Text("New email")),
                  ),
                  const SizedBox(height: 20),
                  ElevatedButton(
                      onPressed: () async {
                        await supabase.auth.updateUser(
                            UserAttributes(email: _newEmailController.text.trim()),
                            emailRedirectTo: "http://localhost:8000/");
                      },
                      child: const Text("Change email"))
                ],
              ),
            )));
      }
    }
    

  4. flutter run -d web-server --web-hostname localhost --web-port 8000 --dart-define=SUPABASE_URL=<your-supabase-url> --dart-define=SUPABASE_ANON_KEY=<your-anon-key>

  5. Go to http://localhost:8000

  6. Sign up with some email address xxx

  7. Click the link in the confirmation email

  8. Get redirected -> It says "Logged in as xxx" at the top

  9. Change email from xxx to yyy

  10. Click the link in the confirmation email sent to xxx

  11. Click the link in the confirmation email sent to yyy

  12. Get redirected -> It still says "Logged in as xxx" at the top although the email has changed in auth.users

Expected behavior

The session after clicking the second confirmation link should contain the new email.

Version

supabase_auth 1.0.0+1
└── supabase_flutter 2.5.9
├── supabase 2.2.5
│ ├── functions_client 2.2.0
│ ├── gotrue 2.8.3
│ ├── postgrest 2.1.2
│ ├── realtime_client 2.2.0
│ ├── storage_client 2.0.2

@Mr-Pepe Mr-Pepe added the bug Something isn't working label Jul 24, 2024
@dshukertjr dshukertjr added the auth This issue or pull request is related to authentication label Jul 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
auth This issue or pull request is related to authentication bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants