-
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.
- Loading branch information
1 parent
b7d3ef2
commit 3398658
Showing
69 changed files
with
41,627 additions
and
1 deletion.
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
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.
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,2 @@ | ||
key/db.js | ||
node_modules |
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,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!') | ||
}) |
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 { 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() | ||
}) | ||
}) | ||
} | ||
|
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,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 |
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,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 |
Oops, something went wrong.