Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

180
Views
Firebase: vuelva a autenticar al usuario actual dentro de una función en la nube

Estoy implementando una función en la nube para actualizar la contraseña del usuario actual.

Básicamente, la lógica que quiero seguir es:

 (Client side) 0. Complete form and submit the data (current password and new password). (Backend) 1. Get the current user email from the callable function context. 2. Re-authenticate the current user using the provided current password. 2.1. If success, change the password and send a notification email. 2.2. Else, throw an error.

Aquí está mi código actual:

 const { auth, functions } = require("../../services/firebase"); ... exports.updatePassword = functions .region("us-central1") .runWith({ memory: "1GB", timeoutSeconds: 120 }) .https.onCall(async (data, context) => { const { currentPassowrd, newPassword } = data; const { email, uid: userId } = context.auth.token; if (!userId) { // throw ... } try { // // Problem: `firebase-admin` authentication doesn't include // the `signInWithEmailAndPassword()` method... // await auth.signInWithEmailAndPassword(email, currentPassowrd); await auth.updateUser(userId, { password: newPassword, }); sendPasswordUpdateEmail(email); } catch (err) { // ... throw AuthErrors.cannotUpdatePassword(); } });

Mi problema es que el paquete firebase-admin no incluye signInWithEmailAndPassword , y necesito una forma de manejar esto, para verificar que "currentPassword" sea correcta, dentro de mi función.

Mi otra opción, si la que he descrito no es posible, es actualizar la contraseña usando firebase sdk en el lado del cliente y luego llamar a una función de firebase para enviar el correo electrónico de notificación.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Estrictamente hablando, no necesita volver a autenticar al usuario en Cloud Function: si obtiene un valor para context.auth.uid en su Callable Cloud Function, significa que el usuario está autenticado en el front-end y puede por lo tanto, llame de forma segura al método updateUser() .

Si desea lidiar con el caso en que el usuario dejó su dispositivo abierto y alguien actualiza su contraseña, como se explica en los comentarios debajo de su pregunta, le sugiero que use el método reauthenticateWithCredential() en el front-end, que re- autentica a un usuario usando una nueva credencial.

Haz lo siguiente:

 import { EmailAuthProvider, getAuth, reauthenticateWithCredential, } from 'firebase/auth' const email = auth.currentUser.email; // Capture the password value // eg via a pop-up window const password = ...; const auth = getAuth(); const credential = EmailAuthProvider.credential( email, password ); await reauthenticateWithCredential( auth.currentUser, credential ); // If no error is thrown, you can call the Callable Cloud Function, knowing the user has just re-signed-in.
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!