I am trying to make a cloud function.
Whenever i try to hit the endpoint I get 500 Internal Server Error
Postman Response Image here
I've checked logs for firebase functions and i don't see any information there too.
It just says "Function Crashed" without any further information.
I've also checked for any typos and mismatch in the Firestore database structure but it all looks fine to me.
This is the code for firebase function which i uploaded on my Firebase Project.
const functions = require('firebase-functions');
const admin = require('firebase-admin');
const { error } = require('firebase-functions/lib/logger');
admin.initializeApp(functions.config().firebase);
exports.addEvent = functions.region('asia-east2').https.onRequest(async (req, res) => {
if (req.method === 'POST') {
var db = admin.firestore();
var write = db.collection("Colleges")
.doc(req.body.college)
.collection("events")
.doc(req.body.event.id)
.set({
id: req.body.event.id,
admin: req.body.event.admin,
event_state: req.body.event.event_state,
name: req.body.event.name,
poster_url: req.body.event.poster_url,
start_date_time: req.body.event.start,
end_date_time: req.body.event.end,
location: req.body.event.location,
short_desc: req.body.event.shortDesc,
long_desc: req.body.event.longDesc,
contacts: req.body.event.contacts,
links: req.body.event.links,
});
return res.send(write);
}
else
return res.sendStatus(403);
});
This is the body of the POST Request which i sent from Postman
{
"college": "college_name",
"event": {
"id": 1234,
"admin": "admin",
"event_state": 2,
"name": "Event Name",
"poster_url": "test",
"start": "Date Time",
"end": "Date Time",
"location": "auditorium",
"shortDesc": "lorem ipsum short",
"longDesc": "lorem ipsum long",
"contatcs": [
{
"tag": "Name Tag",
"contact": 12345678
}
],
"links": [
{
"tag": "Link Tag",
"link": 123456784
}
]
}
}
The Firestore Structure is Something like
-Colleges (Collection)
|
|
-Document
|
-events(Collection)
|
-Event Documents (Document which i want to write to ,from the firebase function)
The problem is that your event id in your payload is a number and Firestore documents ids must be strings. So you either, use .doc(req.body.event.id.toString()) or you send your event id as string in your payload id: "1234".
Also, consider refactoring your code following Firebase guidelines to handle the POST method.
Related
I'm building an email marketing automation tool using NextJS, next-auth and Microsoft Graph API. I'm using next-auth's Azure AD B2C provider to authenticate users, and I've been following their docs.
Within the Configuration (Advanced) section of the docs, I've followed the steps to setup an Azure AD api app to communicate with the Microsoft Graph API (to send email on our user's behalf). Now, when a user signs up, an access_token (jwt) is added to my accounts db table. Here it is decoded:
{
"iss": "https://something.b2clogin.com/b03...f94/v2.0/",
"exp": 1664588154,
"nbf": 1664584554,
"aud": "6eb...c5b",
"idp_access_token": "EwB...QI=",
"idp": "live.com",
"name": "Will Despard",
"sub": "1f7...d6c",
"emails": [
"willdespard#outlook.com"
],
"tfp": "B2C_1_signupsignin",
"scp": "mail.send",
"azp": "ff8...f5d",
"ver": "1.0",
"iat": 1664584554
}
The problem is, there is no example of how to setup the Microsoft Graph JS Client with next-auth. For example, according to Microsoft, to create a Microsoft Graph API client, you must do the following:
import { Client } from '#microsoft/microsoft-graph-client';
const client = Client.init({
authProvider: (done) =>
done(
null,
accessToken // WHERE DO WE GET THIS FROM?
),
});
const sendMail = {
message: {
subject: 'Meet for lunch?',
body: { contentType: 'Text', content: 'The new cafeteria is open.' },
toRecipients: [
{ emailAddress: { address: 'william.cm.despard#gmail.com' } },
],
},
};
const userDetails = await client.api('/me/sendMail').post(sendMail);
However, the following is unclear:
Where are we meant to get the accessToken used in this example from? I've tried using the idp_access_token in the decoded accessToken on my accounts db table (above), but this doesn't seem to work.
I'm assuming the accessToken we use to communicate with Microsoft Graph API is going to expire after a short amount of time. How do we handle getting a new token?
Help/code examples would be much appreciated!
I would try it like this. First, it looks that for graph access you should be looking for Azure AD provider, not Azure AD B2C that is a service that provides identity providers. I.e. looks like you need this one: https://next-auth.js.org/providers/azure-ad
To use Microsoft Graph to send mail you'll also need to request a non-default scope with "Send Mail" grant from your user. Means, when authorizing your app the user will be asked to consent that your app will send emails on behalf of him. Also you'll need to save the graph access token you get from the authentication flow. Something like this:
import AzureADProvider from "next-auth/providers/azure-ad"
export const authOptions: NextAuthOptions = {
providers: [
....
AzureADProvider({
clientId: process.env.AZURE_AD_CLIENT_ID,
clientSecret: process.env.AZURE_AD_CLIENT_SECRET,
authorization: {
params: {
scope:
"openid email profile Mail.Send",
},
},
// tenantId: process.env.AZURE_AD_TENANT_ID,
}),
],
callbacks: {
async jwt({ token, account }) {
if (account) {
token.accessToken = account.access_token;
}
return token
},
Please note that if you do not specify tenantId that would mean that your application will be available for users from any tenant, but that in turn would mean that you must be a verified publisher (i.e. must have a valid MPN ID associated with your app). If you do specify a tenantId, then your app will only work for users from that specified tenant.
Later on, you could just use the token from the API:
import { getToken } from 'next-auth/jwt';
import { Client } from '#microsoft/microsoft-graph-client';
// some API function
export default async function handler(req, res) {
const token = await getToken({ req })
if (token) {
const accessToken = token.accessToken;
const client = Client.init({
authProvider: (done) =>
done(null, accessToken)
});
const sendMail = {
message: {
subject: 'Meet for lunch?',
body: { contentType: 'Text', content: 'The new cafeteria is open.' },
toRecipients: [
{ emailAddress: { address: 'william.cm.despard#gmail.com' } },
],
},
};
const userDetails = await client.api('/me/sendMail').post(sendMail);
...
First of all I know NOSQL systems do not have JOIN. But I know there is a way for getting a datas with another table values.
About my app:
This app is basic social network app. People can share photos. These posts saving firebase database 'realtime database' not cloud firestore than it has a timeline page. this page shows shared posts. I need to show posts with publisher information.
Users login with firebase authentication and I have users table called kullanici like this.
I have Posts table like this.
I need to delete displayName in Posts table than get isim in kullanici table.
my needed data is: Post with kullanici->{posts.{postid}.userid}->isim.
my working code is below:
return (dispatch) => {
firebase.database().ref(`/posts`).endAt("tarih").limitToLast(2)
.on('value', snapshot => {
dispatch({type: STUDENT_LIST_DATA_SUCCESS, payload: snapshot.val()});
});
};
my returned data is below:
Object {
"-L9tnfvm6jQiKLc378c6": Object {
"aciklama": "",
"baslik": "Ensar mı Ahmet mi",
"displayName": "HasanRiza",
"image": "https://hasan-riza-uzuner.s3.amazonaws.com/1523535677033.png",
"like": 244,
"tarih": 1523535757133,
"userid": "fD7IfKAhXogFwHtfKYiF7LMtXNp1",
},
"-LYHPJgR4sywTzpcxX7A": Object {
"aciklama": "Ev",
"baslik": "Nasıl",
"displayName": "HasanRiza",
"image": "https://hasan-riza-uzuner.s3.us-east-2.amazonaws.com/1549718296409.png",
"like": 1,
"tarih": 1549718342522,
"userid": "fD7IfKAhXogFwHtfKYiF7LMtXNp1",
},
}
var promises = [];
group_ids.forEach(function (group_id) {
promises.push(firebase.firestore().collection('Group').doc(group_id).get())
});
Promise.all(promises).then(function(docs) {
docs.forEach((groupDoc) => {
group.name = groupDoc._data['name'];
group.city = groupDoc._data['city'];
group.state = groupDoc._data['state'];
groups.push(group);
});
console.log(groups);
});
I am trying to send a Firebase Cloud Message from Google Apps Script. I have followed the following questions & links & assembled the following code:
Get 403 response with the "new" Firebase Cloud Messaging API
The res|error I am receiving is:
{
"error": {
"code": 403,
"message": "Request had insufficient authentication scopes.",
"status": "PERMISSION_DENIED"
}
}
My Code in Google Apps Script is:
function sendFCM(text){
text = {
"message": {
"topic": "news",
"notification": {
"title": "Breaking News",
"body": "New news story available."
},
"data": {
"story_id": "story_12345"
}
}
}
var projectId = "xxxxxxx";
var apiUrl = "https://fcm.googleapis.com/v1/projects/"+projectId+"/messages:send";
var authKey = "AAAU.......Cs";
var token = ScriptApp.getOAuthToken();
var options = {
'method' : 'post',
'contentType': 'application/json',
'muteHttpExceptions' : true,
'payload' : JSON.stringify(text),
headers:{Authorization: "Bearer "+ token},
};
var res = UrlFetchApp.fetch(apiUrl, options);
Logger.log(res);
}
Script's properties > User Scopes list follwoing:
https://www.googleapis.com/auth/firebase.database
https://www.googleapis.com/auth/script.external_request
https://www.googleapis.com/auth/spreadsheets
https://www.googleapis.com/auth/userinfo.email
FCM & GCM Api is enabled in project's developer console.
Edit:
From Ref's comment, I added the following scope to app manifest:
https://www.googleapis.com/auth/firebase.messaging
Upon Run, it asked me to authorize & now the response is an instance of a Message. But still I do not know how to define a to device i.e. to which device the message should go.
{
"name": "projects/send-xxxx3/messages/866xxxxx92"
}
I found a strange error while I developing system using Firebase with service url contains user data.
User data is below.
{
"uid": "kt9Hcp2FbYbBvvIeSHHa1RbvHcv2",
"displayName": "Anonymous 901",
"photoURL": null,
"email": null,
"emailVerified": false,
"identifierNumber": null,
"isAnonymous": true,
"providerData": [
],
"apiKey": "MyApiKeyString",
"appName": "MyAppName",
"authDomain": "my.auth.domain",
"stsTokenManager": {
"apiKey": "MyApiKeyString",
"refreshToken": "refreshTokenString",
"accessToken": "accessTokenString",
"expirationTime": 1532451863076
},
"redirectEventId": null
}
I encode the above anonymous user data and include it in the service url.
( http://myserviceurl?userdata=encodedUserData )
Inside the system receives that url, firebase creates a user object with that user data contained in the url.
The purpose of this url is to use specific user's information in any browser.
However, when I call that service url, sometimes system creates user object well, sometimes got error -
400 Bad request errors with
https://www.googleapis.com/identitytoolkit/v3/relyingparty/setAccountInfo?key=MyApiKeyString
And error data is below,
{
"error": {
"code": 400,
"message": "TOKEN_EXPIRED",
"errors": [
{
"message": "TOKEN_EXPIRED",
"domain": "global",
"reason": "invalid"
}
]
}
}
Few hours later it works well, I changed nothing though.
I could not find the exact error point, but I suspect error occurs while observing authentication state or before this step.
Here is code snipets
#bind
private makeUserLoadingPromise(): Promise<void> {
let unSubscribe: () => void;
return new Promise<void>((resolve, _reject) => {
const onInitialized = this.makeOnInitializedAuthStateChanged(resolve);
unSubscribe = this.auth.onAuthStateChanged(onInitialized);
}).then(() => {
unSubscribe();
this.auth.onAuthStateChanged(this.onAuthStateChanged);
});
}
#bind
private makeOnInitializedAuthStateChanged(resolve: () => void) {
return (user: firebase.User | null) => {
this.user = user;
resolve();
};
}
#bind
private onAuthStateChanged(user: firebase.User | null) {
this.user = user;
}
Or maybe it relates with expirationTime?
I couldn't find any hints about this situation.
Any advice would be appreciated.
It is not clear what you are doing, but it appears that you are using the API incorrectly and insecurely. The plain user object contains a refresh token that is indefinite. Passing it around via URL is a really bad idea.
First don't rely on internal implementations, it is subject to change.
To get the user's information on your backend, the right way to do it, is to get the user's ID token using officially supported API, eg user.getIdToken(), then pass it to your server.
On your server, you verify it via the Firebase Admin SDK: admin.auth().verifyIdToken(idToken). Then you know this is a real authenticated user. If you need the full user info, you can then look it up using the decoded user id in the token: admin.auth().getUser(decodedIdToken.sub).
I'm trying to call functions from app but it doesn't work and I'm getting the following error from the console:
index.esm.js:402 OPTIONS https://us-central1-undefined.cloudfunctions.net/addMessage 404 ()
Failed to load https://us-central1-undefined.cloudfunctions.net/addMessage: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'https://MYWEBADDRESS' is therefore not allowed access. The response had HTTP status code 404. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.
firebase.json:
{
"database": {
"rules": "database.rules.json"
},
"hosting": {
"public": "public",
"rewrites": [
{
"source": "**",
"function": "addMessage"
}
]
},
"functions": {
"predeploy": [
"npm --prefix $RESOURCE_DIR run lint"
],
"source": "functions"
}
}
index.js
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();
exports.addMessage = functions.https.onCall((data, context) => {
// Message text passed from the client.
const text = data.text;
// Checking attribute.
if (!(typeof text === 'string') || text.length === 0) {
// Throwing an HttpsError so that the client gets the error details.
throw new functions.https.HttpsError('invalid-argument', 'The function must be called with ' +
'one arguments "text" containing the message text to add.');
}
// Checking that the user is authenticated.
if (!context.auth) {
// Throwing an HttpsError so that the client gets the error details.
throw new functions.https.HttpsError('failed-precondition', 'The function must be called ' +
'while authenticated.');
}
// Saving the new message to the Realtime Database.
return admin.database().ref('/messages').push({
text: text
}).then(() => {
console.log('New Message written');
// Returning the sanitized message to the client.
return { text: sanitizedMessage };
}).catch((error) => {
// Re-throwing the error as an HttpsError so that the client gets the error details.
throw new functions.https.HttpsError('unknown', error.message, error);
});
});
my script in index.html
var addMessage = firebase.functions().httpsCallable('addMessage');
addMessage({text: "messageText"}).then(function(result) {
var message = result.data.text;
console.log(message);
});
How I initialize Firebase:
<script src="https://www.gstatic.com/firebasejs/5.0.4/firebase-app.js"></script>
<script src="https://www.gstatic.com/firebasejs/5.0.4/firebase-auth.js"></script>
<script src="https://www.gstatic.com/firebasejs/5.0.4/firebase-database.js"></script>
<script src="https://www.gstatic.com/firebasejs/5.0.4/firebase-functions.js"></script>
<script>
// Initialize Firebase
var config = {
apiKey: "**",
authDomain: "***",
databaseURL: "***",
storageBucket: "***",
};
firebase.initializeApp(config);
var functions = firebase.functions();
</script>
I was having the same exact problem, found your question without an answer, but managed to figure it out in the end.
As #Doug Stevenson mentioned above in the comments, the problem is that the Cloud Functions URL you are seeing has undefined instead of your project ID as the last piece of the url subdomain.
The reason it is undefined is because your project ID is not part of your initial Firebase config object. Like me, you likely copied and pasted the starter snippet from Firebase for the JS SDK, but you did it before they started including the Project ID as part of it. For some reason, even though the project ID is now needed to construct the cloud functions URL, the SDK doesn't error / warn if you don't include it.
All you need to do is add the following field to your config object:
projectId: <YOUR_PROJECT_ID_HERE>
Then you should see the requests no longer 404.