-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
10 changed files
with
122 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
const jwt = require('jsonwebtoken'); | ||
const User = require('../models/userModel'); | ||
const asyncHandler = require('express-async-handler'); | ||
|
||
const protect = asyncHandler(async (req, res, next) => { | ||
let token; | ||
|
||
if (req.headers.authorization && req.headers.authorization.startsWith('Bearer')) { | ||
try { | ||
token = req.headers.authorization.split(' ')[1]; | ||
const decoded = jwt.verify(token, 'sankha'); // process.env.JWT_SECRET later | ||
req.user = await User.findById(decoded.id).select('-password'); | ||
next(); | ||
} catch (error) { | ||
console.error(error); | ||
res.status(401); | ||
throw new Error('Not authorized, token failed'); | ||
} | ||
} | ||
if (!token) { | ||
res.status(401); | ||
throw new Error('Not authorized, no token'); | ||
} | ||
}); | ||
module.exports = { protect }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,10 @@ | ||
const express = require('express'); | ||
const Router = express.Router(); | ||
const { loginController, registerController } = require("../controllers/userController"); | ||
const { loginController, registerController, fetchAllUsersController } = require("../controllers/userController"); | ||
const { protect } = require('../middleware/authMiddleware'); | ||
|
||
Router.post("/login", loginController); | ||
Router.post("/register", registerController); | ||
Router.get("/fetchusers", protect, fetchAllUsersController); | ||
|
||
module.exports = Router; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,7 +12,7 @@ app.use(cors()); | |
|
||
const connectDb = async () => { | ||
try { | ||
mongoose.connect("mongodb+srv://sankhadiproy23:[email protected]/?retryWrites=true&w=majority"); //will use env variable later | ||
await mongoose.connect("mongodb+srv://sankhadiproy23:[email protected]/?retryWrites=true&w=majority"); //will use env variable later | ||
console.log("Database connected"); //log | ||
} | ||
catch (err) { | ||
|
@@ -46,7 +46,7 @@ const onlineUsers = new Set(); | |
io.on("connection", (socket) => { | ||
onlineUsers.add(socket.id); | ||
const usersList = Array.from(onlineUsers); | ||
console.log(`✔ ${socket.id} :` + usersList); //log | ||
// console.log(`✔ ${socket.id} :` + usersList); //log | ||
io.emit('online-users', usersList); | ||
socket.on('send_message', (data) => { | ||
// console.log("Data:", data); //log | ||
|
@@ -56,7 +56,7 @@ io.on("connection", (socket) => { | |
socket.on('disconnect', () => { | ||
onlineUsers.delete(socket.id); | ||
const usersList = Array.from(onlineUsers); | ||
console.log(`✘ ${socket.id} :` + usersList) //log | ||
// console.log(`✘ ${socket.id} :` + usersList) //log | ||
io.emit('online-users', usersList); | ||
}); | ||
}); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import { useEffect, useState } from "react"; | ||
import { useNavigate } from "react-router-dom"; | ||
import axios from 'axios'; | ||
|
||
function Users() { | ||
const [users, setUsers] = useState([]); | ||
const navigate = useNavigate(); | ||
|
||
useEffect(() => { | ||
const userData = JSON.parse(localStorage.getItem('userData')); | ||
console.log("User data from local storage", userData);//log | ||
if (!userData) { | ||
navigate(-1); | ||
return; | ||
} | ||
|
||
const fetchUsers = async () => { | ||
try { | ||
const config = { | ||
headers: { | ||
Authorization: `Bearer ${userData.token}`, | ||
}, | ||
}; | ||
const response = await axios.get('http://localhost:3001/fetchusers', config); | ||
console.log("User data from API", response.data); | ||
setUsers(response.data); | ||
} catch (error) { | ||
console.error("Error fetching users:", error); | ||
} | ||
}; | ||
|
||
fetchUsers(); | ||
}, []); | ||
|
||
return ( | ||
<> | ||
<div className="flex items-center justify-center min-h-screen bg-gray-100"> | ||
<div className="px-8 py-6 mt-4 text-left bg-white shadow-lg rounded-lg w-full max-w-md"> | ||
<h3 className="text-2xl font-bold text-center text-gray-800 mb-4">Users</h3> | ||
<div className="mt-4"> | ||
{users.map(user => ( | ||
<div key={user._id} className="mb-4"> | ||
<p className="text-sm font-medium text-gray-700">Name: {user.name}</p> | ||
<p className="text-sm font-medium text-gray-700">Email: {user.email}</p> | ||
</div> | ||
))} | ||
</div> | ||
</div> | ||
</div> | ||
</> | ||
); | ||
} | ||
export default Users; |