aboutsummaryrefslogtreecommitdiffstats
path: root/server
diff options
context:
space:
mode:
Diffstat (limited to 'server')
-rw-r--r--server/.env0
-rw-r--r--server/config/db.js14
-rw-r--r--server/models/User.js7
-rw-r--r--server/routes/auth.js61
-rw-r--r--server/server.js14
5 files changed, 96 insertions, 0 deletions
diff --git a/server/.env b/server/.env
deleted file mode 100644
index e69de29..0000000
--- a/server/.env
+++ /dev/null
diff --git a/server/config/db.js b/server/config/db.js
new file mode 100644
index 0000000..effbe5e
--- /dev/null
+++ b/server/config/db.js
@@ -0,0 +1,14 @@
+const mongoose = require('mongoose')
+const connecttDB = async () => {
+ try {
+ await mongoose.connect(process.env.MONGO_URI, {
+ useNewUrlParser: true,
+ useUnifiedTopology: true,
+ });
+ console.log('MongoDB Connected...');
+ } catch (err) {
+ console.error(err.message);
+ process.exit(1);
+ }
+};
+module.exports = connectDB;
diff --git a/server/models/User.js b/server/models/User.js
new file mode 100644
index 0000000..6773c64
--- /dev/null
+++ b/server/models/User.js
@@ -0,0 +1,7 @@
+const mongoose = require('mongoose');
+const UserSchema = new mongoose.Schema({
+ username: { type: String, reauired: true, unique: true },
+ password: { type: String, required: true },
+ role: { type: String, default: 'admin' },
+});
+module.exports = mongoose.model('User', UserSchema);
diff --git a/server/routes/auth.js b/server/routes/auth.js
new file mode 100644
index 0000000..3fb0986
--- /dev/null
+++ b/server/routes/auth.js
@@ -0,0 +1,61 @@
+const express = require('express');
+const bcrypt = require('bcryptjs');
+const jwt = require('jsonwebtoken');
+const User = require('../models/User');
+const router = express.Router();
+
+// Register
+router.post('/register', async (req, res) => {
+ const { username, password } = req.body;
+ try {
+ let user = await User.findOne({ username });
+ if (user) {
+ return res.status(400).json({ msg: 'User already exists' });
+ }
+ user = new User({
+ username,
+ password,
+ });
+ const salt = await bcrypt.genSalt(10);
+ user.password = await bcrypt.hash(password, salt);
+ await user.save();
+ res.status(200).send('User registered');
+ } catch (err) {
+ console.error(err.message);
+ res.status(500).send('Server error');
+ }
+});
+
+// Login
+router.post('/login', async (req, res) => {
+ const { username, password } = req.body;
+ try {
+ const user = await User.findOne({ username });
+ if (!user) {
+ return res.status(400).json({ msg: 'Invalid credentials' });
+ }
+ const isMatch = await bcrypt.compare(password, user.password);
+ if (!isMatch) {
+ return res.status(400).json({ msg: 'Invalid credentials' });
+ }
+ const payload = {
+ user: {
+ id: user.id,
+ },
+ };
+ jwt.sign(
+ payload,
+ process.env.JWT_SECRET,
+ { expiresIn: '1h' },
+ (err, token) => {
+ if (err) throw err;
+ res.json({ token });
+ }
+ );
+ } catch (err) {
+ console.error(err.message);
+ res.status(500).send('Server error');
+ }
+});
+
+module.exports = router;
diff --git a/server/server.js b/server/server.js
new file mode 100644
index 0000000..8836667
--- /dev/null
+++ b/server/server.js
@@ -0,0 +1,14 @@
+const express = require('express');
+const connectDB = require('./config/db');
+const cors = require('cors');
+const app = express();
+require('dotenv').config();
+
+connectDB();
+app.use(cors());
+app.use(express.json());
+
+app.use('/api/auth', require('./routes/auth'));
+
+const PORT = process.env.PORT || 5000;
+app.listen(PORT, () => console.log(`Server started on port ${PORT}`));