Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Database #46

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
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
56 changes: 39 additions & 17 deletions Collab-Chat-App/client/src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const socket = io.connect("http://localhost:3001/");
function App() {
const uniqueId = nanoid(4);
const [username, setUsername] = useState("");
const[password,setPassword]=useState("");
const [room, setRoom] = useState(uniqueId);
const [showChat, setShowChat] = useState(false);
const [roomBool, setRoomBool] = useState(true);
Expand All @@ -21,7 +22,13 @@ function App() {
if (username !== "" && room !== "") {
const joinData = { room, username };
socket.emit("join_room", joinData);
setShowChat(true);
socket.on("database",(data)=>
{
setShowChat(data);

}
);

}
};

Expand All @@ -30,6 +37,16 @@ function App() {
setRoom(uniqueId);
setRoomBool(false);
};

const signup=() =>
{
if(username !=="" && password !=="")
{
socket.emit("database",{username,password});
}

}


const notify = () => toast.success('Copied to Clipboard', { position: "top-right", autoClose: 5000, hideProgressBar: false, closeOnClick: true, pauseOnHover: true, draggable: true, progress: undefined, theme: "light", });
const handleClick = event => {
Expand Down Expand Up @@ -72,23 +89,28 @@ function App() {
<div className="box2">
<h3>Join A Room</h3>

<input
type="text"
placeholder="John..."
onChange={(event) => {
setUsername(event.target.value);
}}
/>
<input
type="text"
placeholder="John..."
onChange={(event) => {
setUsername(event.target.value);
}}
/>
<input type="text" placeholder="password" onChange={(event) => {
setPassword(event.target.value);
}} />


<input
type="text"
placeholder="Room ID..."
onChange={(event) => {
setRoom(event.target.value);
}}
/>
<button onClick={joinRoom}>Enter Room</button>
<button onClick={signup}>Signup</button>

<input
type="text"
placeholder="Room ID..."
onChange={(event) => {
setRoom(event.target.value);
}}
/>
<button onClick={joinRoom}>Enter Room</button>
</div>
</div>
</div>
) : (
Expand Down
43 changes: 43 additions & 0 deletions Collab-Chat-App/server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ const app = express();
const http = require("http");
const cors = require("cors");
const { Server } = require("socket.io");
const User=require("./model.js");


app.use(cors());

const server = http.createServer(app);
Expand All @@ -25,8 +28,27 @@ io.on("connection", (socket) => {
//adding this new connection to connections set
connections.add(socket);
console.log(`User Connected: ${socket.id}`);

socket.on("database",(data)=>{
User.create({username:data.username,password:data.password});

})

socket.on("join_room", async (data) => {

const user=await User.findOne({username:username});
if(user)
{
if(user.password==data.password)
{
const data=true
socket.emit("database",data);






console.log(`User with ID: ${socket.id} joined room: ${data}`);
let roomId = data.room;
socket.join(roomId);
Expand All @@ -52,6 +74,27 @@ io.on("connection", (socket) => {
new Date(Date.now()).getMinutes(),
};
socket.to(data.room).emit("receive_message", messageData);
}
else
{
const data=false;
socket.emit("database",data);
}
}

else
{
const data=false;
socket.emit("database",data);


}






});

socket.on("send_message", (data) => {
Expand Down
20 changes: 20 additions & 0 deletions Collab-Chat-App/server/model.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
const mongoose=require("mongoose");

const UserSchema=new mongoose.Schema({

username:
{
type:String
},
password:
{
type:String
}




});

const user=mongoose.model("user",UserSchema);
module.exports=user;