I am getting firebase Internal error and I am unable to figure out why, here is my client side code and could function and Thanks for your help and time in advance. –
Client Side Code
Future<bool> forgotPassword(String email) async {
final functions = FirebaseFunctions.instanceFor(region: "australia-southeast1");
// final HttpsCallableResult result =
Map<String, dynamic> data = {
"Email": email,
};
try {
final json = jsonEncode(data);
print(json.toString());
await functions.httpsCallable("resetPasswordNew").call(json);
// await auth.sendPasswordResetEmail(email: email);
return true;
} on FirebaseFunctionsException catch (e) {
print(e.message);
return false;
}
}
I tried without encoding it do json and directly sending the map and also tried formatting it like according to this answer and also added region in the function instance as per this
Map<String, dynamic> data = {
"data": {
"Email": email,
},
};
This is my cloud function which is meant to 1. Generate a temporary password, 2. Update the user password using auth, 3. Update the firestore collection with that temporary password and 4. finally send email to the user about that password.
const functions = require("firebase-functions");
const admin = require("firebase-admin");
const logger = require("firebase-functions/logger");
const emailHelper = require("./email.helper");
const fs = require('fs');
const { HttpsError } = require("firebase-functions/v1/auth");
const app = admin.initializeApp();
const auth = admin.auth(app);
const userCollection = admin.firestore().collection('User_Info');
exports.resetPasswordNew = functions.https.onCall(async (data) => {
const userData = data;
const dest = userData["Email"];
const subject = `Password Reset Email`;
function generatePassword() {
// Define a list of characters to choose from
const characters = "ABCDEFGHJKLMNPQRSTUVWXYZ";
const numbers = "23456789"
// Initialize the password string
let password = '';
// Generate the password by randomly selecting characters from the list
for (let i = 0; i < 3; i++) {
const randomIndex = Math.floor(Math.random() * characters.length);
password += characters[randomIndex];
}
for (let i = 0; i < 5; i++) {
const randomIndex = Math.floor(Math.random() * numbers.length);
password += numbers[randomIndex];
}
return password;
}
const tempPassword = generatePassword()
try {
const userRecord = await auth.getUserByEmail(userData["Email"]);
await auth.updateUser(userRecord.uid, {
password: tempPassword,
});
// Remove after Emails are streamlined.
userData["Is_Password_Temporary"] = true;
userData["Temporary_Password"] = tempPassword;
await userCollection.doc(userRecord.uid).set(userData, { merge: true });
// await userCollection.doc(user.uid).create(userData);
// userData["userID"] = user.uid;
// userData["Temporary_Password"] = null;
} catch (error) {
logger.error(error.code);
logger.error(error.message);
throw new HttpsError('unknown', error.message);
}
fs.readFile('./invitation.html', 'utf8', (err, template) => {
if (err) {
logger.error(err);
} else {
const invitationData = {
"%Name%": userData["FirstName"] || "",
"%Username%": userData["Email"],
"%Password%": tempPassword,
"%Institute Name%": userData["institutionName"] || "",
};
Object.keys(invitationData).forEach(key => {
template = template.replace(key, invitationData[key]);
});
emailHelper.sendEmail(dest, subject, template, (err, res) => {
if (err) {
logger.error(err);
logger.error(`Error sending Email to ${dest}`);
} else {
logger.log(`Welcome Email sent to ${dest}`);
}
});
}
});
return userData;
});
Update: There are no errors shown in Cloud Functions Metrices, and I tried the function by removing the email part and still I am receiving the Internal error. I also copied the function to uscentral1 from Australia-southeast but that doesn’t seem to make any difference too.
3