Firebase cloud function with With Angular context serverless with 401 - Unauthorized

Discussão em 'Angular' iniciado por Tonyfilho, Fevereiro 19, 2025.

  1. Tonyfilho

    Tonyfilho Guest

    I just deployed my first Angular Serverless Project, now i need to apply claims like Admin, and Roles like User . If i put UID static, aparentely it work, but when i use data from Frontend I have 401, even though are being lognined.

    my Cloud Function

    admin.initializeApp();
    export const setAdminRole = functions.https.onCall(
    async (data: any, context: any) => {
    console.log('Context:', JSON.stringify(context, null, 2)); //
    console.log('Auth Context:', JSON.stringify(context.auth, null, 2)); // Log detalhado
    const localUid = String(data.uid);

    if (!context.auth) {
    throw new functions.https.HttpsError(
    'unauthenticated',
    'The Authentication must be necessary to allow change Permissions.'
    );
    }

    if (!localUid || !data) {
    throw new functions.https.HttpsError(
    'invalid-argument',
    'User UID is required and must be a string.'
    );
    }

    // Define o claim 'admin' para outro usuário
    return admin
    .auth()
    .setCustomUserClaims(localUid, { admin: true, role: 'admin' })
    .then(() => {
    return { message: `Admin role set for user ${localUid}` };
    })
    .catch((error) => {
    throw new functions.https.HttpsError(
    'internal',
    error + 'setAdminRole no index.js'
    );
    });
    }
    );


    Angular Service Claims Code:

    async creatAdminRoles(uid: string) {
    const setAdminRoleFn = httpsCallable(this.functions, 'setAdminRole' );
    try {
    if (uid) {
    const result = await setAdminRoleFn({ uid: uid });
    console.log("creatAdminRoles Success ",result);
    }

    } catch (error) {
    console.error('Erro ao definir admin:', error);
    }
    }



    PostMan print with token updated: [​IMG] PostMan02 print with token updated: [​IMG]

    Login prints: [​IMG]

    Login prints: [​IMG]

    Login prints: [​IMG]

    Login prints: [​IMG]

    Firebase Emulator: [​IMG]

    Continue reading...

Compartilhe esta Página