最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin4浏览0评论

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: PostMan02 print with token updated:

Login prints:

Login prints:

Login prints:

Login prints:

Firebase Emulator:

发布评论

评论列表(0)

  1. 暂无评论