Reputation: 837
I was following a tutorial for writing cloud functions, i tried to import firebase and use firebase.auth() as used in tutorial, but i am getting the below error.
⚠ Error [ERR_PACKAGE_PATH_NOT_EXPORTED]: No "exports" main defined in /home/sankethbk7777/Desktop/React/Projects/social-ape/my-code/social-ape/functions/node_modules/firebase/package.json
at throwExportsNotFound (internal/modules/esm/resolve.js:299:9)
at packageExportsResolve (internal/modules/esm/resolve.js:522:3)
at resolveExports (internal/modules/cjs/loader.js:449:36)
at Function.Module._findPath (internal/modules/cjs/loader.js:489:31)
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:875:27)
at Function.Module._load (internal/modules/cjs/loader.js:745:27)
at Module.require (internal/modules/cjs/loader.js:961:19)
at require (internal/modules/cjs/helpers.js:92:18)
at Object.<anonymous> (/home/sankethbk7777/Desktop/React/Projects/social-ape/my-code/social-ape/functions/index.js:19:18)
at Module._compile (internal/modules/cjs/loader.js:1072:14)
⚠ We were unable to load your functions code. (see above)
code
functions/index.js
const functions = require('firebase-functions');
const admin = require('firebase-admin');
const app = require('express')();
admin.initializeApp();
const config = {
apiKey: 'AIzaSyDMFe1IwnLoui-Meue-FMwNhc1k-MB8vc8',
authDomain: 'socialape-d306c.firebaseapp.com',
projectId: 'socialape-d306c',
storageBucket: 'socialape-d306c.appspot.com',
messagingSenderId: '705972174784',
appId: '1:705972174784:web:1ed87302a774bd1cef1225',
};
const firebase = require('firebase');
firebase.initializeApp(config);
// Signup route
app.post('/signup', (req, res) => {
const newUser = {
email: req.body.email,
password: req.body.password,
confirmPassword: req.body.confirmPassword,
handle: req.body.handle,
};
// TODO: validate date
firebase
.auth()
.createUserWithEmailAndPassword(newUser.email, newUser.password)
.then((data) => {
return res
.status(201)
.json({ message: `user ${data.user.uid} signed up successfully` });
})
.catch((err) => {
console.log(err);
return res.status(500).json({ error: err.code });
});
});
// To tell firebase cloud functions to use routes on express app
// we have written api because we want all our API URL's to start with /api.
exports.api = functions.https.onRequest(app);
I know import could be little different because of version change (the tutorial is from 2019) but i am not able to fix it. Please help me
Upvotes: 2
Views: 1656
Reputation: 50830
You should use the Admin SDK in a Cloud function and not the client. That being said you can remove the const firebase = require("firebase")
and firebase.initializeApp(config);
along with the client configuration. To create users, you can use the createUser()
method:
app.post('/signup', (req, res) => {
const newUser = {
email: req.body.email,
password: req.body.password,
confirmPassword: req.body.confirmPassword,
handle: req.body.handle,
};
return admin.auth().createUser({ email, password }).then((userRecord) => {
return res.send(`Created user ${userRecord.uid}`)
})
}
I am not sure what the 'handle' is but you can use Custom Claims or any database to store it.
Do note that creating users in a Cloud function or a server environment won't log the user in automatically. You must redirect users to your login page after returning the response.
Upvotes: 1