Untitled
unknown
plain_text
2 years ago
1.4 kB
11
Indexable
const express = require('express'); const jwt = require('jsonwebtoken'); const jwtPassword = "1234"; const app = express(); const port = 3000; app.use(express.json()); const allUsers = [{ username : "kushalatolia@gmail.com", password : "Katolia", name : "Kushal Atolia" }, { username : "tavishiseth@gmail.com", password : "Tseth", name : "Tavishi Seth" }]; function userExist (username, password) { let result = allUsers.find((x) => x.username === username && x.password === password); if(!result) { return false; } else { return true; } } app.post("/signin", (req, res) => { const username = req.body.username; const password = req.body.password; if(!userExist(username, password)) { res.status(403).json({ msg : "User does not Exist" }) return; } var token = jwt.sign({username : username}, jwtPassword); console.log(token); res.json({ token, }); }); app.get("/users", (req, res) => { const token = req.headers.authorization; try { const decode = jwt.verify(token, jwtPassword); const username = decode.username; res.json({ username, }) } catch { res.status(403).json({ msg : "Error decoding" }) } }) app.listen(port, () => { console.log(`Example app listening on port ${port}`) })
Editor is loading...
Leave a Comment