How to create custom log in actions in Flutterflow

In this video we will look at how we direct users to different areas within our Flutterflow apps on login depending on what type of user they are. We do this by creating custom log in actions.

There are many use cases for directing users to different parts of a an app when they log in. The standard single logged in page does not work in these situations so we need to create custom log in actions in Flutterflow to channel our user to where they need to be. In the video we look a a specific use case where we have three different options for our user on log in and look at what we need to do if their logged in authentication token is still valid.

Here is the video and the custom code can be downloaded below.

https://youtu.be/Ko-pFgjlSpM

Custom code for creating Flutterflow custom log in actions

This is the custom code for checking if the user is logging in for the first time. Remember to change the tables, variables etc for your individual use case.

//The custom action code for Flutterflow:

Future<bool?> checkFirst(String userid) async {
  try {
    final supabase = SupaFlow.client;
    final response = await supabase.rpc('checkfirst', params: {
      'user_id': userid,
    });
    // Print the raw response for debugging purposes
    print('Check  First Log In: $response');

    // Return the raw response
    return response;
  } catch (e) {
    print('Exception:$e');
  }
}

//The Postgres function we are calling:

CREATE OR REPLACE FUNCTION checkfirst(user_id uuid)
RETURNS BOOLEAN
AS $$

BEGIN
 
  IF user_id IS NOT NULL THEN
    RETURN (SELECT first_login FROM public.users WHERE "userid" = user_id);
  ELSE
    -- If the user_id is not found, return false or handle it as needed
    RETURN FALSE;
  END IF;
END;
$$ LANGUAGE plpgsql;

These are the two code blocks we need to check what type of user we are dealing with to direct them to the correct location.

//The custom action code for Flutterflow:

Future<String?> checkPurpose(String userid) async {
  try {
    final supabase = SupaFlow.client;
    final response = await supabase.rpc('checkpurpose', params: {
      'user_id': userid,
    });
    // Print the raw response for debugging purposes
    print('Check  First Log In: $response');

    // Return the raw response
    return response;
  } catch (e) {
    print('Exception:$e');
  }
}

//The Postgres function we are calling:

CREATE OR REPLACE FUNCTION checkpurpose(user_id uuid)
RETURNS text AS $$
DECLARE
    response text;
BEGIN
    SELECT purpose INTO response
    FROM users
    WHERE "userid" = user_id;
    
    RETURN response;
END;
$$ LANGUAGE plpgsql;