Skip to content

Commit

Permalink
Task for Delta laterals done
Browse files Browse the repository at this point in the history
  • Loading branch information
nishihere19 committed Apr 18, 2021
1 parent b7d3ef2 commit 3398658
Show file tree
Hide file tree
Showing 69 changed files with 41,627 additions and 1 deletion.
21 changes: 20 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,20 @@
# text-editor
# text-editor

Steps to run on Locally:

>git clone https://github.com/nishihere19/text-editor.git
>cd text-editor/backend
>npm install
>npm start
Open a new terminal for frontend

>cd text-editor/client
>npm install
>npm start
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions backend/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
key/db.js
node_modules
109 changes: 109 additions & 0 deletions backend/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
const express = require('express');
const app = express();
const indexRouter = require("./routes/index");
const userRouter = require("./routes/user")
const documentRouter = require("./routes/document")
const mongoose = require("mongoose");
const cors = require('cors')
const http = require('http');
const jwt = require("jsonwebtoken");
//const io = require('socket.io')();
const cookieParser = require('cookie-parser')
require('dotenv').config()
const middleware= require('./middleware/auth')

const {MONGOURI, JWT_SECRET} = require('./key/db');


mongoose.connect(MONGOURI).then(() => console.log("Connected to database."));

let whitelist = ['http://localhost:3000']
let corsOptions = {
credentials: true,
origin: function(origin, callback) {
if (whitelist.indexOf(origin) !== -1) {
callback(null, true)
} else {
callback(new Error('Not allowed by CORS'))
}
}
}

let tokenVerifier = (middleware, req, res, next) => {
//console.log(req.user);
if (req.originalUrl.startsWith('/api/document')) {
const token = req.cookies['token']
try {
const decoded = jwt.verify(token,(req.user._id,JWT_SECRET));
req.user._id = decoded.id
req.user.email = decoded.email
} catch (e) {
res.send({
status: 200,
msg: 'Unauthorized'
})
return
}
}
next()
}

app.use(cookieParser())
app.use(cors(corsOptions));
// var handlecors=function(options){
// return (function(req, res,next) {

// res.header('Access-Control-Allow-Origin', '*');
// res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS');
// res.header("Access-Control-Allow-Headers", "Content-Type, Authorization");
// res.header("Access-Control-Allow-Credentials", true);

// if ('OPTIONS' == req.method) {
// res.status(200);
// }
// else {
// next();
// }
// })
// }
// app.use(handlecors(corsOptions))
// var resolveCrossDomain=(function(req, res,next) {

// res.header('Access-Control-Allow-Origin', '*');
// res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS');
// res.header("Access-Control-Allow-Headers", "Content-Type, Authorization");
// res.header("Access-Control-Allow-Credentials", true);

// if ('OPTIONS' == req.method) {
// res.status(200);
// }
// else {
// next();
// }
// })
// app.use(resolveCrossDomain);

app.use(tokenVerifier)
app.use('/', indexRouter);
app.use('/api/user', userRouter);
app.use('/api/document', documentRouter);



const server = http.createServer(app)
const io = require("socket.io")(server, {
cors: {
origin: "http://localhost:3000",
methods: ["GET", "POST"],
allowedHeaders: ["Authorization"],
credentials: true
},
allowEIO3: true
//transports: ["websocket"],
});
const socketService = require('./service/socketService')(io)
io.attach(server, {cookie: false})

server.listen(8000, function () {
console.log('App listening on port 8000!')
})
25 changes: 25 additions & 0 deletions backend/middleware/auth.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
const jwt = require('jsonwebtoken');
const { JWT_SECRET } = require('../key/db')
const User = require('../model/User');

module.exports = (req, res, next) => {
const { authorization } = req.headers

if (!authorization) {
return res.status(401).json({ error: "you must be logged in" })
}
const token = authorization.replace("Bearer ", "")
//console.log(token);
jwt.verify(token, JWT_SECRET, (err, payload) => {
if (err) {
return res.status(401).json({ error: "you must be logged in" })
}
const { _id } = payload
User.findById(_id).then(userdata => {
req.user = userdata
// console.log(userdata)
next()
})
})
}

50 changes: 50 additions & 0 deletions backend/model/Document.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
const mongoose = require('mongoose')

let DocumentSchema = mongoose.Schema({
created: {
type: Date,
required: true,
default: Date.now()
},
filename: {
type: String,
required: true,
default: "Untitled"
},
lastEdited: {
type: Date,
required: true,
default: Date.now()
},
createdBy: {
type: String,
required: true
},
lastEditedBy: {
type: String,
require: true
},
content: {
type: String,
required: true
},
type: {
type: String,
enum: ['markdown'],
required: true
},
accessType: {
type: String,
enum: ['public-edit', 'public-read', 'controlled'],
required: true,
default: 'controlled'
},
access: {
type: Map,
of: Object,
}
});

let Document = mongoose.model("Document", DocumentSchema)

module.exports = Document
36 changes: 36 additions & 0 deletions backend/model/User.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
const mongoose = require('mongoose')

let UserSchema = mongoose.Schema({
email: {
type: String,
required: true,
minlength: 5,
maxlength: 50,
unique: true,
},
firstName: {
type: String,
required: true,
minlength: 1,
},
lastName: {
type: String,
required: true,
minlength: 1
},
password: {
type: String,
required: true,
minlength: 8
},
files: {
type: Map,
of: Date,
required: true,
default: {}
}
})

let User = mongoose.model("User", UserSchema)

module.exports = User
Loading

0 comments on commit 3398658

Please sign in to comment.