I am new to firebase cloud functions and been following a tutorial for setting up a function to authenticate users. I got a CORS problem when trying to send a request body to my /createAccount
endpoint. Anyone have any tips on how to begin to troubleshoot? Thank you
Access to XMLHttpRequest at 'https://us-central1-vue-photoapp-api.cloudfunctions.net/createAccount' from origin 'http://localhost:3000' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.
Here is snippet of code createAccount.js
that may be causing the error:
import * as functions from 'firebase-functions' // import everything from firebase-functions and reference it as 'functions' in code
import { createAuthUser } from './createAuthUser'
import { createTemporaryUser } from './createTemporaryUser'
export const createAccount = functions.https.onRequest(async (req, res) => {
try {
const { newUserInfo } = req.body // grab user info sent from front-end
// custom helper funcs to create new users in Firebase Auth
const authUid = await createAuthUser(newUserInfo)
// pre verification user
await createTemporaryUser(authUid, newUserInfo)
return res.status(200).send('Success!')
} catch (error) {
return res.status(500).send('Error!', error)
}
})
and createAuthUser.js
:
import * as admin from 'firebase-admin' // allows us to bypass security rules we set for client app
export const createAuthUser = async (newUserInfo) => {
console.log('show the info: ', newUserInfo)
const auth = admin.auth() // get access to Auth instance
const { emailAddress, password } = newUserInfo // get email and pass out of new user info payload
const newUser = await auth.createUser({
email: emailAddress,
password
})
return newUser.uid
}
Here are the detailed logs:
6:05:11.262 AM
createAccount
Function execution took 263 ms, finished with status: 'connection error'
6:05:11.251 AM
createAccount
}
6:05:11.251 AM
createAccount
code: 'ERR_HTTP_INVALID_STATUS_CODE'
6:05:11.251 AM
createAccount
at process._fatalException (internal/process/execution.js:165:25) {
6:05:11.251 AM
createAccount
at process.EventEmitter.emit (domain.js:483:12)
6:05:11.251 AM
createAccount
at process.emit (events.js:327:22)
6:05:11.251 AM
createAccount
at process.<anonymous> (/layers/google.nodejs.functions-framework/functions-framework/node_modules/@google-cloud/functions-framework/build/src/invoker.js:267:22)
6:05:11.251 AM
createAccount
at Object.sendCrashResponse (/layers/google.nodejs.functions-framework/functions-framework/node_modules/@google-cloud/functions-framework/build/src/logger.js:34:13)
6:05:11.251 AM
createAccount
at ServerResponse.send (/layers/google.nodejs.functions-framework/functions-framework/node_modules/express/lib/response.js:221:10)
6:05:11.251 AM
createAccount
at ServerResponse.end (_http_outgoing.js:753:5)
6:05:11.251 AM
createAccount
at write_ (_http_outgoing.js:642:9)
6:05:11.251 AM
createAccount
at ServerResponse._implicitHeader (_http_server.js:239:8)
6:05:11.251 AM
createAccount
at ServerResponse.writeHead (_http_server.js:248:11)
6:05:11.251 AM
createAccount
RangeError [ERR_HTTP_INVALID_STATUS_CODE]: Invalid status code: Error!
6:05:11.251 AM
createAccount
^
6:05:11.251 AM
createAccount
throw new ERR_HTTP_INVALID_STATUS_CODE(originalStatusCode);
6:05:11.251 AM
createAccount
_http_server.js:248
6:05:11.249 AM
createAccount
at processPromiseRejections (internal/process/promises.js:209:33)
6:05:11.249 AM
createAccount
at process.EventEmitter.emit (domain.js:483:12)
6:05:11.249 AM
createAccount
at process.emit (events.js:315:20)
6:05:11.249 AM
createAccount
at process.<anonymous> (/layers/google.nodejs.functions-framework/functions-framework/node_modules/@google-cloud/functions-framework/build/src/invoker.js:271:22)
6:05:11.249 AM
createAccount
at Object.sendCrashResponse (/layers/google.nodejs.functions-framework/functions-framework/node_modules/@google-cloud/functions-framework/build/src/logger.js:34:13)
6:05:11.249 AM
createAccount
at ServerResponse.send (/layers/google.nodejs.functions-framework/functions-framework/node_modules/express/lib/response.js:221:10)
6:05:11.249 AM
createAccount
at ServerResponse.end (_http_outgoing.js:753:5)
6:05:11.249 AM
createAccount
at write_ (_http_outgoing.js:642:9)
6:05:11.249 AM
createAccount
at ServerResponse._implicitHeader (_http_server.js:239:8)
6:05:11.249 AM
createAccount
at ServerResponse.writeHead (_http_server.js:248:11)
6:05:11.249 AM
createAccount
RangeError [ERR_HTTP_INVALID_STATUS_CODE]: Invalid status code: Error!
6:05:11.249 AM
createAccount
Uncaught exception
6:05:11.247 AM
createAccount
at Generator.invoke [as _invoke] (/workspace/node_modules/regenerator-runtime/runtime.js:293:22)
6:05:11.247 AM
createAccount
at tryCatch (/workspace/node_modules/regenerator-runtime/runtime.js:63:40)
6:05:11.247 AM
createAccount
at _callee$ (/workspace/build/users/createAccount.js:49:62)
6:05:11.247 AM
createAccount
at ServerResponse.send (/layers/google.nodejs.functions-framework/functions-framework/node_modules/express/lib/response.js:158:21)
6:05:11.247 AM
createAccount
at ServerResponse.json (/layers/google.nodejs.functions-framework/functions-framework/node_modules/express/lib/response.js:267:15)
6:05:11.247 AM
createAccount
at ServerResponse.send (/layers/google.nodejs.functions-framework/functions-framework/node_modules/express/lib/response.js:221:10)
6:05:11.247 AM
createAccount
at ServerResponse.end (_http_outgoing.js:753:5)
6:05:11.247 AM
createAccount
at write_ (_http_outgoing.js:642:9)
6:05:11.247 AM
createAccount
at ServerResponse._implicitHeader (_http_server.js:239:8)
6:05:11.247 AM
createAccount
at ServerResponse.writeHead (_http_server.js:248:11)
6:05:11.247 AM
createAccount
RangeError [ERR_HTTP_INVALID_STATUS_CODE]: Invalid status code: Error!
6:05:11.247 AM
createAccount
Unhandled rejection
6:05:11.243 AM
createAccount
Sat, 19 Dec 2020 12:05:11 GMT express deprecated res.send(status, body): Use res.status(status).send(body) instead at build/users/createAccount.js:49:62
6:05:11.120 AM
createAccount
show the info: undefined
6:05:11.000 AM
createAccount
Function execution started