I followed some guides and wrote a cloud function. Now I need to call it from within my app. I did the following:
Kotlin code:
class ActivitySignup : AppCompatActivity() {
private lateinit var functions: FirebaseFunctions
private lateinit var user: String
override fun onCreate(savedInstanceState: Bundle?) {
...
functions = Firebase.functions
...
submitbutton.setOnClickListener() {
Log.e(tag,"Clicked submit")
userEditTxt = findViewById(R.id.et_user)
user = userEditTxt.text.toString().trim()
auth.createUserWithEmailAndPassword(email, password)
.addOnCompleteListener(this) { task ->
if (task.isSuccessful) {
// Sign in success, update UI with the signed-in user's information
Log.i(tag, "User created")
functions.getHttpsCallable("addUser")
.call(user)
.continueWith { task ->
// This continuation runs on either success or failure, but if the task
// has failed then result will throw an Exception which will be
// propagated down.
val result = task.result?.data as String
Log.e("result", result)
result
}
val intent = Intent(this, ActivityGroups::class.java)
startActivity(intent)
finish()
} else {
// If sign in fails, display a message to the user.
Log.w(tag, "failure", task.exception)
Toast.makeText(baseContext, "Authentication failed.",
Toast.LENGTH_SHORT).show()
}
}
}
The cloud function in index.ts:
import * as functions from 'firebase-functions';
import * as admin from 'firebase-admin';
admin.initializeApp();
exports.addUser = functions.https.onCall((data, context) => {
console.log('addUser: ', data.username);
const username = data.username
functions.auth.user().onCreate(user => {
const doc = admin.firestore().collection('users').doc();
return doc.set({
createDate: admin.firestore.FieldValue.serverTimestamp(),
modifiedDate: admin.firestore.FieldValue.serverTimestamp(),
username: username,
email: user.email,
stat: 1, //0 = banned, 1 = normal
uid: user.uid,
rowpointer: doc.id,
});
});
However, there are 2 problems:
Android studio is highlighting the "functions" part of
Firebase.functions
in red. The error isUnresolved reference: functions
When I did
firebase serve
in Visual Studio, I got the following:
- functions[addUser]: http function initialized (http://localhost:5000/APPNAME-cf4da/us-central1/addUser).
i functions: Beginning execution of "addUser"
{"severity":"WARNING","message":"Request has invalid method. GET"}
{"severity":"ERROR","message":"Invalid request, unable to process."}
i functions: Finished "addUser" in ~1s
I'm pretty new to Android dev/cloud functions, so I feel like I'm just making a rookie mistake somewhere...