Skip to content
Snippets Groups Projects
Unverified Commit 7164c5d8 authored by Fazal's avatar Fazal Committed by GitHub
Browse files

Add files via upload

parents
No related branches found
No related tags found
No related merge requests found
const { Client } = require("pg");
const client = new Client(process.env.DB_URL); //Configuring PostgresSQL Database
module.exports = client;
\ No newline at end of file
if (process.env.NODE_ENV !== 'production') {
require('dotenv').config() //Configuring dotenv during development stage
}
\ No newline at end of file
const bcrypt = require("bcrypt");
const client = require("../configs/database");
const jwt = require("jsonwebtoken");
//Login Function
exports.login = async (req, res) => {
//Checking if User registered to our application or not
const {
email,
password
} = req.body;
try {
const data = await client.query(`SELECT * FROM users WHERE email= $1;`, [email]) //Verifying if the user exists in the database
const user = data.rows;
if (user.length === 0) {
res.status(400).json({
error: "User is not registered, Sign Up first",
});
}
//Comparing Hashed Password with User's Password
else {
bcrypt.compare(password, user[0].password, (err, result) => { //Comparing the hashed password
if (err) {
res.status(500).json({
error: "Server error",
});
} else if (result === true) { //Checking if credentials match
const token = jwt.sign({
email: email,
},
process.env.SECRET_KEY
);
res.status(200).json({
message: "User signed in!",
token: token,
});
}
//Handling Errors While Signing in the Users
else {
//Declaring the errors
if (result != true)
res.status(400).json({
error: "Enter correct password!",
});
}
})
}
} catch (err) {
console.log(err);
res.status(500).json({
error: "Database error occurred while signing in!", //Database connection error
});
};
};
\ No newline at end of file
const bcrypt = require("bcrypt");
const client = require("../configs/database");
const jwt = require("jsonwebtoken");
//Registration Function
//Checking if user is already present in our database
exports.register = async (req, res) => {
const {
name,
email,
role,
password
} = req.body;
try {
const data = await client.query(`SELECT * FROM users WHERE email= $1;`, [email]); //Checking if user already exists
const arr = data.rows;
if (arr.length != 0) {
return res.status(400).json({
error: "Email already there, No need to register again.",
});
} else {
//Hashing user's Password
bcrypt.hash(password, 10, (err, hash) => {
if (err)
res.status(err).json({
error: "Server error",
});
const user = {
name,
email,
role,
password: hash,
};
//Inserting User's Information in our Database
var flag = 1; //Declaring a flag
//Inserting data into the database
client
.query(`INSERT INTO users (name, email, role, password) VALUES ($1,$2,$3,$4);`, [user.name, user.email, user.role, user.password], (err) => {
if (err) {
flag = 0; //If user is not inserted is not inserted to database assigning flag as 0/false.
console.error(err);
return res.status(500).json({
error: "Database error"
})
} else {
flag = 1;
res.status(200).send({
message: 'User added to database, not verified'
});
}
})
//Signing JSON Web Token for each User
if (flag) {
const token = jwt.sign( //Signing a jwt token
{
email: user.email
},
process.env.SECRET_KEY
);
};
});
}
} catch (err) {
console.log(err);
res.status(500).json({
error: "Database error while registring user!", //Database connection error
});
};
}
\ No newline at end of file
This diff is collapsed.
{
"name": "ems",
"version": "1.0.0",
"description": "",
"main": "server.js",
"dependencies": {
"cors": "^2.8.5",
"dotenv": "^10.0.0",
"express": "^4.17.2",
"pg": "^8.7.1"
},
"devDependencies": {
"bcrypt": "^5.0.1",
"jsonwebtoken": "^8.5.1"
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "nodemon server.js"
},
"keywords": [],
"author": "",
"license": "ISC"
}
const express = require('express');
const router = express.Router();
const {register} = require("../controllers/register");
const {login} = require("../controllers/login");
router.post('/register' , register); //POST request to register the user
router.post('/login' , login); // POST request to login the user
module.exports = router;
\ No newline at end of file
const express = require("express");
const cors = require('cors')
const app = express();
app.use(express.json());
app.use(cors());
const port = process.env.PORT || 5000;
app.get("/", (req, res) => {
res.status(200).send("Engine Started, Ready to take off!");
})
//configuring dotenv file
require("./configs/dotenv");
const client = require("./configs/database");
client.connect((err) => { //Connected Database
if (err) {
console.log(err);
} else {
console.log("Data logging initiated!");
}
});
//routes to user.js
const api = require("./routes/user");
app.use("/api", api); //Route for /api endpoint of API
//express....
app.listen(port, () => {
console.log(`Here we go, Engines started at ${port}.`);
})
\ No newline at end of file
Server/users-db.jpeg

13 KiB

0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment