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

added #12

Open
wants to merge 2 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
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
The diff you're trying to view is too large. We only load the first 3000 changed files.
2 changes: 1 addition & 1 deletion backend/.env
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
mongoUrl = mongodb+srv://jackayron:[email protected].mongodb.net/streamspace?retryWrites=true&w=majority
mongoUrl = mongodb+srv://khidki:[email protected].mongodb.net/StreamSpace?retryWrites=true&w=majority
port = 8080
accessKey = superman
refreshKey = batman
Expand Down
18 changes: 9 additions & 9 deletions backend/chat/chat.js → backend/chatting.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,29 @@ const app=require("express")()
const server=require("http").createServer(app)
const io=require("socket.io")(server)
let arr=[]
io.on("connect",(socket)=>{
io.on("connection",(socket)=>{
console.log(socket.id)
socket.on("jroom",(data)=>{
const user=jroom(socket.id,data.name,data.roomno)
socket.on("joinroom",(data)=>{
const user=joinroom(socket.id,data.name,data.roomno)
let name=`${user.name} has joined chat `
let room=user.room
socket.join(user.room)
socket.broadcast.emit("welcome_msg",({
socket.broadcast.emit("welcomemsg",({
name,
room
}))
io.to(room).emit("room_no",room)
io.to(room).emit("roomno",room)
socket.on("msg",(data)=>{
let room=data.room
io.to(room).emit("incoming_msg",data)
io.to(room).emit("incomingmsg",data)
})
socket.on("exit",(data)=>{
socket.broadcast.emit("exit_msg",`${data.name} has leaved the Chat`)
socket.broadcast.emit("exitmsg",`${data.name} has leaved the Chat`)
})
})
})

function jroom(id,name,room) {
function joinroom(id,name,room) {
let obj={
id,
name,
Expand All @@ -34,4 +34,4 @@ function jroom(id,name,room) {
return obj
}

server.listen(5050,()=>console.log("server running..."))
server.listen(4500,()=>console.log("server running..."))
5 changes: 0 additions & 5 deletions backend/config/db.js

This file was deleted.

5 changes: 5 additions & 0 deletions backend/db.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const mongoose=require("mongoose")
require("dotenv").config()
const connection=mongoose.connect(process.env.mongoUrl);

module.exports={connection}
2 changes: 2 additions & 0 deletions backend/dummy.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
hello backend
hi backend
101 changes: 58 additions & 43 deletions backend/index.js
Original file line number Diff line number Diff line change
@@ -1,52 +1,67 @@
const express = require("express");
const { connection } = require("./config/db");
const { userRoute } = require("./routes/userRoute.routes");
// const { auth } = require("./midleware/auth.middleware");
const cookieParser = require("cookie-parser");
const cors = require("cors");
const session = require("express-session");
const { otpRouter } = require("./routes/otproutes.routes");
const app = express();
app.use(express.json());
app.use(cors());
app.use(cookieParser());
const port = process.env.port;
require("dotenv").config();
const express=require("express");
const { connection } = require("./db");
const { authentication } = require("./middleware/authentication");
const { userRoute } = require("./routes/userRoute");
const cookieParser = require('cookie-parser')
const cors=require("cors");
const app=express();
app.use(express.json())
app.use(cors())
app.use(cookieParser())
const port=process.env.port;
require('dotenv').config();
const expressWinston= require("express-winston")
const winston= require("winston");
require("winston-mongodb")

app.use(expressWinston.logger({
transports: [
// new winston.transports.File({
// level:"info",
// json:true,
// filename:"alllogs.json"
// }),
new winston.transports.MongoDB({
level:"silly",
db:process.env.mongoUrl,
json:true

})
],
format: winston.format.combine(
winston.format.colorize(),
winston.format.json()
),
}));

// this is the home route---
app.get("/", (req, res) => {
res.send("home page");
});

// attaching the user login and register routes----
app.use("/users", userRoute);
app.use(
session({
resave: true,
secret: "your secret",
saveUninitialized: true,
})
);
// this is the home route---
app.get("/",(req,res)=>{
res.send("home page");
})


// app.use(auth)
// app.get("/check",async(req,res)=>{
// try {
// res.status(200).send({success:"successful"})
// } catch (error) {
// res.status(404).send({err:error})
// }

// })
// attaching the user login and register routes----
app.use("/users",userRoute)
// user authentication----
app.use(authentication)

// listening and creating the mongosse connection
app.listen(port, async () => {
app.get("/check",async(req,res)=>{
try {
await connection;
console.log("Db is connected");
res.status(200).send({success:"successful"})
} catch (error) {
console.log({ err: error });
res.status(404).send({err:error})
}
console.log(`server is running at port ${port}`);
});

})
// listening and creating the mongosse connection
app.listen(8998,async()=>{
try {
await connection
console.log(`server is running at port ${port}`);
} catch (error) {
console.log({"err":error});

}

})
68 changes: 68 additions & 0 deletions backend/middleware/authentication.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
const jwt = require("jsonwebtoken");
const { BlockModel } = require("../models/blockUser");
const { UserModel } = require("../models/userModel");
require("dotenv").config()
const fetch = (...args) => import('node-fetch').then(({ default: fetch }) => fetch(...args));
const authentication = async (req, res, next) => {

const token = req.cookies.token
const refreshToken = req.cookies.reftoken
const isBlacklisted = await BlockModel.find({token})

if (isBlacklisted) {
return res.status(401).send('Token is blacklisted');

}


if (!token) {
res.send("Login again")
return;
}
if (token) {
jwt.verify(token, process.env.accessKey, async (err, decode) => {
if (decode) {
req.body.userId = decode.userId;

const userData= await UserModel.findById({_id:decode.userId})
req.user=userData
if(!userData){
return res.status(401).json({"err":"unauthorized"})
}
next()
} else {
const fetchdata = await fetch("https://wild-gold-betta-fez.cyclic.app/users/refresh", {
headers: {
cookie: `reftoken=${refreshToken}`
}
}).then((res) => res.json())
if (fetchdata.err) {
res.send("login first")
return;
}
jwt.verify(fetchdata.token, process.env.accessKey, async (err, decode) => {
if (decode) {
res.cookie("token", fetchdata.token);
req.body.userId = decode.userId;
const userData= await UserModel.findById({_id:decode.userId})
req.user=userData
if(!userData){
return res.status(401).json({"err":"unauthorized"})
}
next()
} else {
res.send("login first")
return;
}

})
}
})
} else {
res.send({ "Msg": "Please login" })
}
}

module.exports = {
authentication
}
55 changes: 0 additions & 55 deletions backend/midleware/auth.middleware.js

This file was deleted.

12 changes: 6 additions & 6 deletions backend/models/blockUser.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
const mongoose = require("mongoose");
const mongoose=require("mongoose")

const blockSchema = mongoose.Schema({
token: { type: String, required: true },
});
const blockSchema=mongoose.Schema({
token:{type:String,required:true}
})

const BlockModel = mongoose.model("blacklist", blockSchema);
module.exports = { BlockModel };
const BlockModel=mongoose.model("blog",blockSchema)
module.exports={BlockModel}
17 changes: 8 additions & 9 deletions backend/models/userModel.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
const mongoose=require('mongoose')

const mongoose = require("mongoose");
const userSchema=mongoose.Schema({
name:{type:String,required:true},
email:{type:String,required:true},
password:{type:String,required:true}
})

const userSchema = mongoose.Schema({
name: { type: String, required: true },
email: { type: String, required: true },
password: { type: String, required: true },
});

const UserModel = mongoose.model("user", userSchema);
module.exports = { UserModel };
const UserModel=mongoose.model("user",userSchema);
module.exports={UserModel}
12 changes: 0 additions & 12 deletions backend/node_modules/.bin/color-support

This file was deleted.

17 changes: 0 additions & 17 deletions backend/node_modules/.bin/color-support.cmd

This file was deleted.

Loading