Flutter web Firebase Authentication URL redirect

Viewed 547

I have a question about Flutter navigation in combination with Firebase Auth. In my code a have a AuthenticationWrapper that checks if a user is logged or not. If the user is logged in then it navigates to the home page and when a user is not logged in it navigates to the sign in page.

The problem is when I change the URL of my web page

from: http://localhost/

To:http://localhost/home.

It's goes to the home page without logging in. What I want is that is redirects to the Sign in page.

How can I make this? I would love to hear your input on this issue. Thanks in advance!

Main.dart:

import 'package:firebase_auth/firebase_auth.dart';
import 'package:rsm/Screens/screen2.dart';
import 'package:rsm/Screens/sign_in_page.dart';
import 'package:rsm/Screens/home_page.dart';
import 'package:rsm/Auth/authentication_service.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:flutter/widgets.dart';

Future<void> main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await Firebase.initializeApp();
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MultiProvider(
        providers: [
          Provider<AuthenticationService>(
            create: (_) => AuthenticationService(FirebaseAuth.instance),
          ),
          StreamProvider(
            create: (context) =>
                context.read<AuthenticationService>().authStateChanges,
            initialData: null,
          ),
        ],
        child: MaterialApp(
          title: 'name',
          theme: ThemeData(
            primarySwatch: Colors.blue,
            visualDensity: VisualDensity.adaptivePlatformDensity,
          ),
          routes: {
            // When navigating to the "/" route, build the FirstScreen widget.
            '/signin': (context) => SignInPage(),
            // When navigating to the "/second" route, build the SecondScreen widget.
            '/home': (context) => HomePage(),
            '/screen2': (context) => Screen2(),
          },
          home: AuthenticationWrapper(),
        ));
  }
}

class AuthenticationWrapper extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    final firebaseUser = context.watch<User?>();
    if (firebaseUser != null) {
      //print(firebaseUser);
      // Page after loggin in
      return HomePage();
    }
    // Sign in page
    return SignInPage();
  }
}

AuthenticationService.dart:

import 'package:firebase_auth/firebase_auth.dart';

class AuthenticationService {
  final FirebaseAuth _firebaseAuth;

  AuthenticationService(this._firebaseAuth);

  Stream<User?> get authStateChanges => _firebaseAuth.idTokenChanges();

  Future<void> signOut() async {
    await _firebaseAuth.signOut();
  }

  Future<String?> signIn(
      {required String email, required String password}) async {
    try {
      await _firebaseAuth.signInWithEmailAndPassword(
          email: email, password: password);
      return "Signed in";
    } on FirebaseAuthException catch (e) {
      return e.message;
    }
  }

  Future<String?> signUp(
      {required String email, required String password}) async {
    try {
      await _firebaseAuth.createUserWithEmailAndPassword(
          email: email, password: password);
      return "Signed up";
    } on FirebaseAuthException catch (e) {
      return e.message;
    }
  }

  Future<void> sendPasswordResetMail(String email) async {
    await _firebaseAuth.sendPasswordResetEmail(email: email);
    return null;
  }
}
0 Answers
Related