Skip to content

Commit

Permalink
Created index.js
Browse files Browse the repository at this point in the history
Backend Server
  • Loading branch information
Shubh0507 authored Oct 25, 2024
1 parent 0172c34 commit a68cb8f
Showing 1 changed file with 87 additions and 0 deletions.
87 changes: 87 additions & 0 deletions backend/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
// Importing libraries and declaring global variables
const express = require("express");
const jwt = require("jsonwebtoken");
const cors = require('cors');
const JWT_SECRET = "hellohidarlingkkrh";
const app = express();
const port = 3000;
let users = [];


// Middlewares
app.use(express.json());
app.use(cors());

const auth = (req,res,next) => {
const token = req.headers.token;
const decodedInfo = jwt.verify(token, JWT_SECRET);
if (decodedInfo.username) {
req.username = decodedInfo.username;
next();
} else {
res.json({
message : "You are not logged in"
})
}
}


// Route Handlers
// app.get("/", (req,res) => {
// res.sendFile(__dirname + "/public/index.html")
// })

app.post("/signup", (req, res) => {
const username = req.body.username;
const password = req.body.password;

users.push({
username: username,
password: password,
});

res.json({
Message: "Welcome Aboard",
});
});

app.post("/signin", (req, res) => {
const username = req.body.username;
const password = req.body.password;

const user = users.find(
(user) => user.username === username && user.password === password
);

if (user) {
const token = jwt.sign(
{
username: username,
},
JWT_SECRET
);
user.token = token;
res.json({
token: token,
});
} else {
res.status(404).json({
Message: "Invalid username or password",
});
}
});

app.get("/me", auth, (req, res) => {
const token = req.headers.token;
const decodedInfo = jwt.verify(token, JWT_SECRET);
const username = decodedInfo.username;

res.json({
username: username,
});
});

// Listening port
app.listen(port, () => {
console.log("Server is running!")
});

0 comments on commit a68cb8f

Please sign in to comment.