0% found this document useful (0 votes)
26 views5 pages

Firebase User Management API Design

Uploaded by

thomasriley35420
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views5 pages

Firebase User Management API Design

Uploaded by

thomasriley35420
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Name: Nrupal Wakode

Class: IT-C
Roll No. 76
PRN. 12210035
Assignment No. 6

Problem Statement: Design an Assignment to retrieve, verify, and store


user credentials using Firebase Authentication, the Google App Engine
standard environment, and Google Cloud Data store.

Code
App.js

const express = require('express');


const bodyParser = require('body-parser');
const admin = require('firebase-admin');
const { Datastore } = require('@google-cloud/datastore');
const firebaseClient = require('./firebaseClient');

const serviceAccount =
require('C:/Users/LENOVO/Desktop/assi6/private.json');

admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
});

const datastore = new Datastore();

const app = express();


const port = process.env.PORT || 8080;

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));

app.get('/', (req, res) => {


res.send('Welcome to the User Management API');
});

app.post('/register', async (req, res) => {


const { email, password, displayName } = req.body;

try {

const existingUser = await admin.auth().getUserByEmail(email);

return res.status(400).send('Error: This email is already


registered.');

} catch (error) {

if (error.code === 'auth/user-not-found') {


try {

const userRecord = await admin.auth().createUser({


email,
password,
displayName,
});

const userKey = datastore.key('User');


const user = {
key: userKey,
data: {
uid: userRecord.uid,
email,
displayName,
},
};

await datastore.save(user);

return res.status(201).send(`User created: $


{userRecord.uid}`);

} catch (createError) {
return res.status(400).send(`Error: $
{createError.message}`);
}
} else {

return res.status(400).send(`Error: ${error.message}`);


}
}
});

app.post('/login', async (req, res) => {


const { email, password } = req.body;

try {

await firebaseClient.auth().signInWithEmailAndPassword(email,
password);

const userRecord = await admin.auth().getUserByEmail(email);

res.status(200).send(`User logged in: ${userRecord.uid}`);


} catch (error) {
res.status(400).send(`Error: ${error.message}`);
}
});

app.listen(port, () => {
console.log(`Server running at http://localhost:${port}`);
});

FirebaseClient.js
const firebase = require('firebase/app');
require('firebase/auth');

const firebaseConfig = {
apiKey: "AIzaSyC_K9GYxeXbzUjcnhqfWcBOhmoRlHLUPHg",
authDomain: "study-f4713.firebaseapp.com",
projectId: "study-f4713",
storageBucket: "study-f4713.appspot.com",
messagingSenderId: "858128562059",
appId: "1:858128562059:web:8b8601dc4b5dc23a6c618e",
measurementId: "G-717JMERV8Q"
};

// Initialize Firebase
firebase.initializeApp(firebaseConfig);

module.exports = firebase;

app.yaml
runtime: nodejs14
entrypoint: node app.js

handlers:
- url: /.*
script: auto

You might also like