Firebase Authentication is a service provided by Firebase to authenticate and manage users in your applications. In a Node.js application, you can use the Firebase Admin SDK to interact with Firebase Authentication. Here's a step-by-step guide on how to set up Firebase Authentication in a Node.js application:
firebase-admin
package:bashnpm install firebase-admin
javascriptconst admin = require('firebase-admin');
// Replace 'path/to/your/serviceAccountKey.json' with the actual path to your service account key file
const serviceAccount = require('path/to/your/serviceAccountKey.json');
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
databaseURL: 'https://your-project-id.firebaseio.com', // Replace with your Firebase project URL
});
javascriptconst admin = require('firebase-admin');
// ... Initialize Firebase Admin SDK ...
// Register a new user with email and password
admin.auth().createUser({
email: 'user@example.com',
password: 'password123',
})
.then((userRecord) => {
console.log('Successfully created new user:', userRecord.uid);
})
.catch((error) => {
console.error('Error creating new user:', error);
});
javascriptconst admin = require('firebase-admin');
// ... Initialize Firebase Admin SDK ...
// Sign in an existing user with email and password
admin.auth().signInWithEmailAndPassword('user@example.com', 'password123')
.then((userCredential) => {
console.log('Successfully signed in:', userCredential.user.uid);
})
.catch((error) => {
console.error('Error signing in:', error);
});
These are just basic examples. Firebase Authentication supports various authentication providers, and you can customize the authentication flow based on your application's needs. Refer to the Firebase Authentication documentation for more details and options.