Email Authentication and Verification using Node.js and Firebase | Engineering Education (EngEd) Program | Section

Adding firebase to the application

In the next phase, we will get to the core functions of the application. To use Firebase, we need to obtain authorization for Firebase to know who is accessing the Firebase functions. We need to generate a config object to use with the application.

To generate the config object, check out this guide.

Your final Firebase config object should look like this:

Authentication with email and password

Ensure you have enabled email-password authentication from your firebase console. From the console, go to Authentication (Sign in method tab) and enable Email/Password

Authentication with github

Ensure you have enabled Github authentication from your firebase console. From the console, go to Authentication (Sign in method tab) and enable Github.

Authentication with google (gmail) account

Ensure you have enabled authentication with Google from your firebase console. From the console, go to Authentication (Sign in method tab) and enable Google.

Building the server

The index.js file is the entry point of the application. It will also act as the server file for the project.

Creating the application routes

We need three routes for this project:

Errors

All errors have the following structure

Further readings

To find more about the topic, you can check the resources in the following links:

Id token authentication with firebase, node js (explained, github)

const express = require('express')
const app = express()
const port = 8080
const cors = require('cors');
app.use(cors());
app.use(express.json());
app.get('/', (req, res) => res.send('Hello World!'))app.listen(port, () => console.log(`Example app listening on port ${port}!`))
require("firebase/auth");
const firebase = require('firebase')
const admin = require('firebase-admin')
const serviceAccount = require("YOUR SERVICE ACCOUNT JSON PATH");
var firebaseConfig = {
//YOUR FIREBASE CONFIG
};
firebase.initializeApp(firebaseConfig);
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
databaseURL: "YOUR DATABASE URL"
});
module.exports = { firebase, admin };
Похожее:  Заключение договора поставки газа - ООО «Газпром межрегионгаз Липецк»
const { firebase, admin } = require('./fbConfig')
exports.login = (req, res) => {firebase.auth().signInWithEmailAndPassword(req.body.email, req.body.password)
.then(function () {
firebase.auth().currentUser.getIdToken(true).then(function (idToken){
res.send(idToken)
res.end()
}).catch(function (error) {
//Handle error
});
}).catch(function (error) {
//Handle error
});
}
admin.auth().createCustomToken(uid)
.then(function (customToken) {
res.send(customToken)
res.end()
})
.catch(function (error) {
//Handle error
});
const fbAuth = require('./fbAuth')const {
login,
userBasedFunc
} = require('./user')
app.post('/login', login);
app.get('/userBasedFunc', fbAuth, userBasedFunc);
const { firebase, admin } = require('./fbConfig');module.exports = (req, res, next) => {const token = req.header('Authorization').replace('Bearer', '').trim()var user = firebase.auth().currentUser;
if (user) {
admin.auth().verifyIdToken(token)
.then(function (decodedToken) {
if(decodedToken.uid === user.uid)
{
req.user = user.uid
return next()
}
}).catch(function (error) {
//Handle error
});
} else {
console.log("There is no current user.");
}
};
Похожее:  Интернет-банк Промсвязьбанк: вход в личный кабинет на

Prerequisites

To follow this artice along with me effectively:

Project setup

We will set up the project just like any other Node.js project. Execute the command below to get started.

Register a callback

You can register a callback to perform other operations after token verification or customize responses on token verification error.

When using a callback, remember to call next() or respond with an error after processing the information returned to the callback

const middlewareCallback = function(req, res, next, error, data) {
    if (error === 'ERROR_NO_TOKEN') {
        // token not supplied
        res.status(401).json({error: "No token provided"});
    }
    else if (error === 'ERROR_INVALID_TOKEN') {
        // token failed verification
        res.status(401).json({error: "Unauthorized access"});
    }
    else if (error) {
        // some other error occurred (this should never happen!)
        res.status(500).json({error: "Unexpected error"});
    }
    else if (data.error) {
        // there was no error with verifying the token, thus user id can be found in data.userId
        // there was however an error in getting user info from firebase using the id
        res.status(401).json({error: "An error occurred while trying to verify your credentials"});
    }
    else {
        // data contains user id and token (v0.2.0 and later) and full user information (id, displayName, email etc) for v0.1.1 and earlier
        req.user = data;
        next();
    }
};
const firebaseTokenMiddleware = FirebaseAuth.initTokenMiddleware(serviceAccount, middlewareCallback); // v1.0.0 and later
const firebaseTokenMiddleware = new FirebaseAuth.Guard(serviceAccount, middlewareCallback); // v0.2.0 to 0.9.9
const firebaseTokenMiddleware = firebase.protect(serviceAccount, middlewareCallback); // v0.1.1 and earlier

Starting the server

In this phase, we will test our application by running the command nodemon start in the terminal.

Похожее:  Интернет Банк Росгосстрах: регистрация и вход в личный кабинет

We need to add the below code in the index.js file:

Step 1 – get service account json from your firebase project console

From your console, go to Project Overview -> Settings icon -> Project settings (Service Accounts tab)

Import the dependencies

To bring in the dependencies, add the snippets below to your index.js file.

Step 2 – initialize token checking middleware for express

Setup default token middleware with following behaviour

1 Звезда2 Звезды3 Звезды4 Звезды5 Звезд (1 оценок, среднее: 5,00 из 5)
Загрузка...

Добавить комментарий

Ваш адрес email не будет опубликован.

Adblock
detector