Skip to content
Open
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
107 changes: 107 additions & 0 deletions server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@ require("dotenv").config();

const express = require("express");
const app = express();
const path = require('path');
const bodyParser = require('body-parser');
const User = require('./model/user');
const bcrypt = require('bcryptjs');
const jwt = require('jsonwebtoken');

const JWT_SECRET = ""

const dbUrl = process.env.DB_URL;

Expand All @@ -17,6 +24,106 @@ connection.once("open", () => {
console.log("Database connected");
});

app.use('/', express.static(path.join(__dirname, 'static')))
app.use(bodyParser.json())

app.post('/api/change-password', async (req, res) => {
const { token, newpassword: plainTextPassword } = req.body

if (!plainTextPassword || typeof plainTextPassword !== 'string') {
return res.json({ status: 'error', error: 'Invalid password' })
}

if (plainTextPassword.length < 5) {
return res.json({
status: 'error',
error: 'Password too small. Should be atleast 6 characters'
})
}

try {
const user = jwt.verify(token, JWT_SECRET)

const _id = user.id

const password = await bcrypt.hash(plainTextPassword, 10)

await User.updateOne(
{ _id },
{
$set: { password }
}
)
res.json({ status: 'ok' })
} catch (error) {
console.log(error)
res.json({ status: 'error', error: ';))' })
}
})

app.post('/api/login', async (req, res) => {
const { username, password } = req.body
const user = await User.findOne({ username }).lean()

if (!user) {
return res.json({ status: 'error', error: 'Invalid username/password' })
}

if (await bcrypt.compare(password, user.password)) {
// the username, password combination is successful

const token = jwt.sign(
{
id: user._id,
username: user.username
},
JWT_SECRET
)

return res.json({ status: 'ok', data: token })
}

res.json({ status: 'error', error: 'Invalid username/password' })
})

app.post('/api/register', async (req, res) => {
const { username, password: plainTextPassword } = req.body

if (!username || typeof username !== 'string') {
return res.json({ status: 'error', error: 'Invalid username' })
}

if (!plainTextPassword || typeof plainTextPassword !== 'string') {
return res.json({ status: 'error', error: 'Invalid password' })
}

if (plainTextPassword.length < 5) {
return res.json({
status: 'error',
error: 'Password too small. Should be atleast 6 characters'
})
}

const password = await bcrypt.hash(plainTextPassword, 10)

try {
const response = await User.create({
username,
password
})
console.log('User created successfully: ', response)
} catch (error) {
if (error.code === 11000) {
// duplicate key
return res.json({ status: 'error', error: 'Username already in use' })
}
throw error
}

res.json({ status: 'ok' })
})


app.listen(3000, () => {
console.log("APP IS LISTENING ON PORT 3000!");
});
Loading