Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Signup gautam #6

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
SECRET_KEY = "HACKTOBERFEST"
72 changes: 72 additions & 0 deletions backend/controllers/auth.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
const express = require('express');
const router = express.Router();


const bcrypt = require("bcrypt");
const saltRounds = 10;
const jwt = require("jsonwebtoken");
require('dotenv').config();

const bypass = require('../middleware/bypass');
const knex = require('../db/db')

router.get('/',bypass,(req,res )=> {
res.send("Working User API")
});


router.get('/get',bypass, async (req,res)=>{
const response = await knex.select().table('users');
res.json({response})
});

const generateToken = (data) =>{
const KEY = "HACKTOBERFEST"
const token = jwt.sign(data, KEY);
return token;
}

const encryptPass = async (pass)=>{
const salt = await bcrypt.genSalt(saltRounds);
const secPass = await bcrypt.hash(pass, salt);
return secPass;
}
const validateMail = require('../middleware/validateemail')

router.post('/signup',validateMail,async(req,res)=>{
const {name, email,password} = req.body;

const data = {
'email' : email
}


const token = await generateToken(data);
const pass = await encryptPass(password);
let success = 0
try {

const d = await knex('users')
.where({'email' : email}).limit(1);
console.log(d[0])

if(d[0]){
return res.status(203).json({success : 0, message : "Email already in use"})
}
await knex('users').insert({
'name' : name,
'email': email,
'password' : pass,
'token' : token
}).then(()=>{
success = 1
return res.status(200).json({success, message : "User Created"})
});
} catch (error) {
success = 0;
console.log(error);
return res.json({success, message : "Error"})
}
})

module.exports = router
2 changes: 2 additions & 0 deletions backend/controllers/example.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ const post = (req,res) => {
res.status(200).json(`Recieved via test/post:${req.body}`)
}



module.exports = {
get, post
}
6 changes: 6 additions & 0 deletions backend/db/db.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const knex = require('knex')
const knexfile = require('./knexfile')

const db = knex(knexfile);

module.exports = db;
21 changes: 21 additions & 0 deletions backend/db/knexfile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Update with your config settings.


module.exports = {


client: 'postgresql',
connection: {
database: 'thepeopleboard',
user: 'postgres',
password: '0000'
},
pool: {
min: 2,
max: 10
},
migrations: {
tableName: 'knex_migrations'
}

};
15 changes: 15 additions & 0 deletions backend/db/migrations/20231014071544_create_user.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
exports.up = function (knex) {
return knex.schema.createTable("users", (table) => {
table.increments("id");
table.string("name");
table.string("email");
table.string("password");
table.string("token");
table.timestamps(true, true);
});
};

exports.down = function (knex) {

return knex.schema.dropTable("users");
};
7 changes: 7 additions & 0 deletions backend/middleware/bypass.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
//BYPASS MIDDLEWARE
const bypass = (req,res,next) =>
{
next();
}

module.exports = bypass
14 changes: 14 additions & 0 deletions backend/middleware/validateemail.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
var validator = require("email-validator");

const validateMail = (req,res,next) =>{
const {email} = req.body;
const v = validator.validate(email);

if (v){
next();
}else{
let success = 0
res.json({success,message : "Invalid Mail!"})
}
}
module.exports = validateMail
114 changes: 112 additions & 2 deletions backend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@
"dependencies": {
"bcrypt": "^5.1.1",
"cors": "^2.8.5",
"dotenv": "^16.3.1",
"email-validator": "^2.0.4",
"express": "^4.18.2",
"jsonwebtoken": "^9.0.2",
"knex": "^2.5.1",
"pg": "^8.11.3"
}
Expand Down
23 changes: 13 additions & 10 deletions backend/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,18 @@ const express = require('express');
const cors = require('cors');
const bcrypt = require('bcrypt');

const db = require('knex')({
client: 'pg',
connection: {
host : '127.0.0.1', //Localhost
port : 5432,
user : '',
password : '',
database : 'thepeopleboard'
}
});
// const db = require('knex')({
// client: 'pg',
// connection: {
// host : '127.0.0.1', //Localhost
// port : 5432,
// user : 'postgres',
// password : '0000',
// database : 'thepeopleboard'
// }
// });

// db.migrate.latest();

// Connecting our controllers
const example = require('./controllers/example');
Expand All @@ -28,6 +30,7 @@ app.get('/example/get', (req, res) => { example.get(req,res) });

app.post('/example/post', (req, res) => { example.post(req,res) });

app.use('/auth',require('./controllers/auth'))

// Running our backend API on port 5000
app.listen(5000, () => {
Expand Down