-
Notifications
You must be signed in to change notification settings - Fork 0
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
base app setup #1
Open
OmkarArora
wants to merge
11
commits into
master
Choose a base branch
from
dev
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
3ac26f8
base app setup
OmkarArora 15ae640
feat: Password encryption
OmkarArora edb673a
feat: jwt auth
OmkarArora 5f439bc
rm authVerify
OmkarArora 70a3899
fix: user object in req
OmkarArora ed4fb8e
refactor: removed sensitive console logs
OmkarArora 3cacc52
feat: url userId cross verification with auth token
OmkarArora 1adbdf6
fix: auth with playlist fixed
OmkarArora 5919f00
refactor: removed console lg
OmkarArora 9b2d518
refactor: removed unneeded code
OmkarArora 1d939f7
feat: watch later and history added for users
OmkarArora File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 |
---|---|---|
@@ -0,0 +1,4 @@ | ||
/node_modules | ||
.env | ||
npm-debug.log | ||
.DS_Store |
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 @@ | ||
# Game Labs |
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,20 @@ | ||
const mongoose = require("mongoose"); | ||
|
||
const uri = process.env.GAME_LABS_DB_URI; | ||
|
||
const initDBConnection = async () => { | ||
try{ | ||
await mongoose.connect(uri, { | ||
useNewUrlParser: true, | ||
useUnifiedTopology: true, | ||
useCreateIndex: true | ||
}); | ||
console.log("DB connection successful"); | ||
} | ||
catch(error){ | ||
console.log("Error connecting to DB\nLogs - "); | ||
console.error(error); | ||
} | ||
} | ||
|
||
module.exports = { initDBConnection }; |
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,91 @@ | ||
require("dotenv").config(); | ||
|
||
const jwt = require("jsonwebtoken"); | ||
const bcrypt = require("bcrypt"); | ||
const express = require("express"); | ||
const bodyParser = require("body-parser"); | ||
const cors = require("cors"); | ||
const { initDBConnection } = require("./db.connect"); | ||
const { requestInfo, errorHandler } = require("./middleware/middleware"); | ||
|
||
const { User } = require("./models/user.model"); | ||
|
||
const app = express(); | ||
app.use(bodyParser.json()); | ||
app.use(cors()); | ||
app.use(requestInfo); | ||
|
||
initDBConnection(); | ||
|
||
const usersRouter = require("./routers/users.router"); | ||
app.use("/users", usersRouter); | ||
|
||
const userFixedPlaylists = require("./routers/user-fixed-playlists.router"); | ||
app.use("/user-fixed-playlists", userFixedPlaylists); | ||
|
||
const playlistsRouter = require("./routers/playlists.router"); | ||
app.use("/playlists", playlistsRouter); | ||
|
||
const videosRouter = require("./routers/videos.router"); | ||
app.use("/videos", videosRouter); | ||
|
||
const categoriesRouter = require("./routers/categories.router"); | ||
app.use("/categories", categoriesRouter); | ||
|
||
app.get("/", (req, res) => { | ||
res.send("Connected to Game LABS server"); | ||
}); | ||
|
||
app.post("/login", async (req, res) => { | ||
const { email, password } = req.body; | ||
try { | ||
const user = await User.findOne({ email }); | ||
if (!user) { | ||
return res.json({ | ||
success: false, | ||
message: "Email not found", | ||
errorMessage: "Email not found", | ||
}); | ||
} | ||
const validPassword = await bcrypt.compare(password, user.password); | ||
|
||
if (validPassword) { | ||
const token = jwt.sign( | ||
{ userId: user._id, email: user.email }, | ||
process.env.JWT_SECRET | ||
); | ||
res.json({ | ||
success: true, | ||
message: "Login success", | ||
user: { | ||
id: user._id, | ||
name: user.name, | ||
email: user.email, | ||
role: user.role, | ||
}, | ||
token, | ||
}); | ||
} else { | ||
res.json({ success: false, message: "Invalid password" }); | ||
} | ||
} catch (error) { | ||
res.status(400).json({ | ||
success: false, | ||
message: "User not found", | ||
errorMessage: error.message, | ||
}); | ||
} | ||
}); | ||
|
||
// catching errors | ||
app.use(errorHandler); | ||
|
||
// 404 Handler | ||
app.use((req, res) => { | ||
res.status(404).json({ success: false, message: "Route not found" }); | ||
}); | ||
|
||
const PORT = process.env.PORT || 5050; | ||
app.listen(PORT, () => { | ||
console.log("SERVER STARTED on port: ", PORT); | ||
}); |
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,48 @@ | ||
const jwt = require("jsonwebtoken"); | ||
|
||
const requestInfo = (req, res, next) => { | ||
console.log("\nREQUEST", req.path); | ||
console.log("METHOD", req.method); | ||
next(); | ||
}; | ||
|
||
const paramLogger = (req, res, next) => { | ||
if (req.params) { | ||
console.log("\nPARAMS"); | ||
console.table(req.params); | ||
req.paramsChecked = true; | ||
} else { | ||
req.paramsChecked = false; | ||
} | ||
next(); | ||
}; | ||
|
||
const authVerify = (req, res, next) => { | ||
const token = req.headers.authorization; | ||
try { | ||
const decoded = jwt.verify(token, process.env.JWT_SECRET); | ||
req.userAuth = { userId: decoded.userId, email: decoded.email }; | ||
if(req.user && decoded.userId !== String(req.user._id)){ | ||
return res.status(401).json({ | ||
success: false, | ||
message: "User authentication failed", | ||
}); | ||
} | ||
return next(); | ||
} catch (error) { | ||
return res.status(401).json({ | ||
success: false, | ||
message: "Unauthorised access, put valid token", | ||
}); | ||
} | ||
}; | ||
|
||
const errorHandler = (err, req, res, next) => { | ||
console.error(err.stack); | ||
res.status(500).json({ | ||
success: false, | ||
message: "error occurred, see the error message for more details", | ||
}); | ||
}; | ||
|
||
module.exports = { requestInfo, paramLogger, errorHandler, authVerify }; |
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,57 @@ | ||
const mongoose = require("mongoose"); | ||
require('mongoose-type-url'); | ||
|
||
const CategorySchema = new mongoose.Schema( | ||
{ | ||
name: { | ||
type: String, | ||
required: "Cannot create a category without a `name` field" | ||
}, | ||
developers: [ | ||
{ | ||
type: String, | ||
required: "Provide atleat one developer, expected an array" | ||
} | ||
], | ||
publishers: [ | ||
{ | ||
type: String, | ||
required: "Provide atleat one publisher, expected an array" | ||
} | ||
], | ||
release: { | ||
type: Date | ||
}, | ||
genre: [ | ||
{type: String} | ||
], | ||
thumbnail: { | ||
type: mongoose.SchemaTypes.Url, | ||
required: "Category needs a thumbnail image" | ||
}, | ||
icon: { | ||
type: mongoose.SchemaTypes.Url, | ||
required: "Category needs an icon" | ||
}, | ||
gallery: [ | ||
{type: mongoose.SchemaTypes.Url} | ||
], | ||
description: { | ||
type: String, | ||
minLength: [100, "Description should be atleast 100 characters long"] | ||
}, | ||
platforms: [ | ||
{ | ||
type: String, | ||
enum: ["PS4", "PS5", "Xbox One | X", "Xbox Series X | S", "Windows 10", "macOS", "iOS", "Android", "Nintendo Switch"], | ||
} | ||
] | ||
}, | ||
{ | ||
timestamps: true | ||
} | ||
); | ||
|
||
const Category = mongoose.model("Category", CategorySchema); | ||
|
||
module.exports = { Category }; |
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,22 @@ | ||
const mongoose = require("mongoose"); | ||
|
||
const PlaylistSchema = new mongoose.Schema( | ||
{ | ||
title: { | ||
type: String, | ||
required: "Cannot create a playlist without a `title`" | ||
}, | ||
videos: [{ | ||
type: mongoose.Schema.Types.ObjectId, | ||
ref: "Video", | ||
required: true | ||
}] | ||
}, | ||
{ | ||
timestamps: true | ||
} | ||
); | ||
|
||
const Playlist = mongoose.model("Playlist", PlaylistSchema); | ||
|
||
module.exports = { Playlist }; |
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,64 @@ | ||
const mongoose = require("mongoose"); | ||
require("mongoose-type-url"); | ||
|
||
const UserSchema = new mongoose.Schema( | ||
{ | ||
name: { | ||
type: String, | ||
required: "Cannot create a user without a `name`", | ||
}, | ||
email: { | ||
type: String, | ||
required: "Email address is required", | ||
unique: true, | ||
index: true, | ||
validate: [ | ||
(email) => { | ||
let re = new RegExp(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/); | ||
return re.test(email); | ||
}, | ||
"Provide a valid email address", | ||
], | ||
}, | ||
password: { | ||
type: String, | ||
required: true, | ||
minLength: [8, "Password must be atleast 8 characters long"], | ||
}, | ||
role: { | ||
type: String, | ||
enum: ["user", "admin"], | ||
default: "user", | ||
}, | ||
profileImage: { | ||
type: mongoose.SchemaTypes.Url, | ||
}, | ||
categorySubscriptions: [ | ||
{ | ||
type: mongoose.Schema.Types.ObjectId, | ||
ref: "Category", | ||
}, | ||
], | ||
playlists: [ | ||
{ | ||
type: mongoose.Schema.Types.ObjectId, | ||
ref: "Playlist", | ||
}, | ||
], | ||
history: { | ||
type: mongoose.Schema.Types.ObjectId, | ||
ref: "Playlist", | ||
}, | ||
watchLater: { | ||
type: mongoose.Schema.Types.ObjectId, | ||
ref: "Playlist", | ||
}, | ||
}, | ||
{ | ||
timestamps: true, | ||
} | ||
); | ||
|
||
const User = mongoose.model("User", UserSchema); | ||
|
||
module.exports = { User }; |
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,44 @@ | ||
const mongoose = require("mongoose"); | ||
require('mongoose-type-url'); | ||
|
||
const VideoSchema = new mongoose.Schema( | ||
{ | ||
title: { | ||
type: String, | ||
required: "Cannot create a playlist without a `name`" | ||
}, | ||
category: { | ||
type: mongoose.Schema.Types.ObjectId, | ||
ref: "Category", | ||
required: true | ||
}, | ||
thumbnail: { | ||
type: mongoose.SchemaTypes.Url, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Correct me if I'm wrong, but There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I want thumbnail to be specifically a url for the image, hence used that type |
||
required: "Video needs a thumbnail image url" | ||
}, | ||
video: { | ||
type: mongoose.SchemaTypes.Url, | ||
required: "Video needs a url" | ||
}, | ||
description: { | ||
type: String | ||
}, | ||
runtime: { | ||
minutes: { | ||
type: Number, | ||
required: "Video runtime - minutes required" | ||
}, | ||
seconds: { | ||
type: Number, | ||
required: "Video runtime - seconds required" | ||
} | ||
} | ||
}, | ||
{ | ||
timestamps: true | ||
} | ||
); | ||
|
||
const Video = mongoose.model("Video", VideoSchema); | ||
|
||
module.exports = { Video }; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can make a separate route for login along with others.