supabase: MFA example is missing RLS policy for mfa_factors

Improve documentation

Link

https://supabase.com/docs/guides/auth/auth-mfa

Describe the problem

I tried to implement MFA and enforce only for users that have opted-in. But when I try to fetch now I get this error:

permission denied for table mfa_factors

I tried a couple combinations to make mfa_factors readable but I can’t get it to work.

Describe the improvement

It would be great to have also an example RLS policy for the mfa_factors to make them accessible only for the MFA check.

Additional context

Add any other context or screenshots that help clarify your question.

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Comments: 18 (6 by maintainers)

Most upvoted comments

It’s still erroring! @Collin0810 's fix could be referenced in the docs.

Hi @KirioXX,

I encountered a similar issue and managed to resolve it by creating a separate check_mfa function set as a SECURITY DEFINER.

This function enforces MFA when the user has at least one MFA factor verified:

CREATE OR REPLACE FUNCTION public.check_mfa()
RETURNS boolean
LANGUAGE plpgsql
SECURITY DEFINER
AS $$
BEGIN
    RETURN (
        array[auth.jwt()->>'aal'] <@ (
            SELECT
                CASE
                    WHEN count(id) > 0 THEN array['aal2']
                    ELSE array['aal1', 'aal2']
                END
            FROM auth.mfa_factors
            WHERE auth.uid() = user_id AND status = 'verified'
        )
    );
END;
$$;

Once created, you can implement the checks in your RLS policies as follows:

check_mfa() AND auth.uid() = user_id

Hope this helps!