Check User Authentication

Actions & Logic

New Flutterflow developer here so go easy one me ๐Ÿ™‚

Stack is Flutterflow + Firebase.

I want to make it so that when someone navigates to my application, or returns to an existing session, that it checks to see if the user is authenticated or not, if they are not authenticated I want it to force them back to the login page.

I tried checking the "Requires Authentication" button on the page, but it does not seem to work. I also tried writing a Custom Action and then integrating it into a to check on the On Load page actions, but it also does not work. Every time I open my app it goes straight to the page, even though the user is not authenticated.

I'm kind of stuck here and could use some help.

What have you tried so far?
  • Requires Authentication on page

  • Custom Action, here is the code

  • // Automatic FlutterFlow imports

    import '/backend/backend.dart';

    import '/actions/actions.dart' as action_blocks;

    import '/flutter_flow/flutter_flow_theme.dart';

    import '/flutter_flow/flutter_flow_util.dart';

    import '/custom_code/actions/index.dart'; // Imports other custom actions

    import '/flutter_flow/custom_functions.dart'; // Imports custom functions

    import 'package:flutter/material.dart';

    // Begin custom action code

    // DO NOT REMOVE OR MODIFY THE CODE ABOVE!

    import 'package:firebase_auth/firebase_auth.dart';

    // Set your action name, define your arguments and return parameter,

    // and then add the boilerplate code using the green button on the right!

    import 'package:firebase_auth/firebase_auth.dart';

    Future<bool> isUserAuthenticated() async {

    // Get the current user

    User? user = FirebaseAuth.instance.currentUser;

    // Check if user is authenticated

    if (user != null) {

    return true; // User is authenticated

    } else {

    return false; // User is not authenticated

    }

    }

Did you check FlutterFlow's Documentation for this topic?
Yes
1
1 reply