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

Video library #94

Open
wants to merge 3 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
3 changes: 1 addition & 2 deletions apps/cra-template-mockbee-video-library/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
{
"name": "cra-template-mockbee-video-library",
"version": "0.1.0",
"private": true,
"version": "1.0.0",
"main": "template.json",
"files": [
"template",
Expand Down
13 changes: 4 additions & 9 deletions apps/cra-template-mockbee-video-library/template.json
Original file line number Diff line number Diff line change
@@ -1,17 +1,12 @@
{
"package": {
"dependencies": {
"@testing-library/jest-dom": "^5.11.6",
"@testing-library/react": "^11.2.2",
"@testing-library/user-event": "^12.6.0",
"axios": "^0.21.4",
"bcryptjs": "^2.4.3",
"dayjs": "^1.10.7",
"jsonwebtoken": "^8.5.1",
"jwt-decode": "^3.1.2",
"jwt-encode": "^1.0.1",
"miragejs": "^0.1.41",
"uuid": "^8.3.2",
"web-vitals": "^0.2.4",
"yarn": "^1.22.10"
"mockman-js": "^1.0.5",
"uuid": "^8.3.2"
},
"scripts": {
"start": "react-scripts start",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import { v4 as uuid } from "uuid";
import { Response } from "miragejs";
import { formatDate } from "../utils/authUtils";
import bcrypt from "bcryptjs";
const jwt = require("jsonwebtoken");

const sign = require("jwt-encode");
/**
* All the routes related to Auth are present here.
* These are Publicly accessible routes.
Expand All @@ -30,11 +28,10 @@ export const signupHandler = function (schema, request) {
);
}
const _id = uuid();
const encryptedPassword = bcrypt.hashSync(password, 5);
const newUser = {
_id,
email,
password: encryptedPassword,
password,
createdAt: formatDate(),
updatedAt: formatDate(),
...rest,
Expand All @@ -43,10 +40,7 @@ export const signupHandler = function (schema, request) {
playlists: [],
};
const createdUser = schema.users.create(newUser);
const encodedToken = jwt.sign(
{ _id, email },
process.env.REACT_APP_JWT_SECRET
);
const encodedToken = sign({ _id, email }, process.env.REACT_APP_JWT_SECRET);
return new Response(201, {}, { createdUser, encodedToken });
} catch (error) {
return new Response(
Expand Down Expand Up @@ -76,8 +70,8 @@ export const loginHandler = function (schema, request) {
{ errors: ["The email you entered is not Registered. Not Found error"] }
);
}
if (bcrypt.compareSync(password, foundUser.password)) {
const encodedToken = jwt.sign(
if (password === foundUser.password) {
const encodedToken = sign(
{ _id: foundUser._id, email },
process.env.REACT_APP_JWT_SECRET
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ export const getVideosFromPlaylistHandler = function (schema, request) {
const user = requiresAuth.call(this, request);
if (user) {
const playlistId = request.params.playlistId;
const playlist = user.playlists.find((item) => item._id !== playlistId);
const playlist = user.playlists.find((item) => item._id === playlistId);
return new Response(200, {}, { playlist });
}
return new Response(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { v4 as uuid } from "uuid";
import bcyrpt from "bcryptjs";
import { formatDate } from "../utils/authUtils";
/**
* User Database can be added here.
Expand All @@ -13,25 +12,7 @@ export const users = [
firstName: "Adarsh",
lastName: "Balika",
email: "[email protected]",
password: bcyrpt.hashSync("adarshBalika123", 5),
createdAt: formatDate(),
updatedAt: formatDate(),
},
{
_id: uuid(),
firstName: "Dhruvi",
lastName: "Shah",
email: "[email protected]",
password: bcyrpt.hashSync("dhruviShah123", 5),
createdAt: formatDate(),
updatedAt: formatDate(),
},
{
_id: uuid(),
firstName: "Shubham",
lastName: "Soni",
email: "[email protected]",
password: bcyrpt.hashSync("shubhamSoni123", 5),
password: "adarshBalika123",
createdAt: formatDate(),
updatedAt: formatDate(),
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { Response } from "miragejs";
const jwt = require("jsonwebtoken");
import dayjs from "dayjs";
import jwt_decode from "jwt-decode";

export const requiresAuth = function (request) {
const encodedToken = request.requestHeaders.authorization;
const decodedToken = jwt.verify(
const decodedToken = jwt_decode(
encodedToken,
process.env.REACT_APP_JWT_SECRET
);
Expand Down
6 changes: 3 additions & 3 deletions apps/video-library/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
"@testing-library/react": "^11.1.0",
"@testing-library/user-event": "^12.1.10",
"axios": "^0.21.4",
"bcryptjs": "^2.4.3",
"dayjs": "^1.10.7",
"jsonwebtoken": "^8.5.1",
"jwt-decode": "^3.1.2",
"jwt-encode": "^1.0.1",
"miragejs": "^0.1.41",
"mockman-js": "^1.0.5",
"react": "^17.0.2",
"react-dom": "^17.0.2",
"mockman-js": "^1.0.5",
"react-scripts": "4.0.3",
"uuid": "^8.3.2",
"web-vitals": "^1.0.1"
Expand Down
16 changes: 5 additions & 11 deletions apps/video-library/src/backend/controllers/AuthController.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import { v4 as uuid } from "uuid";
import { Response } from "miragejs";
import { formatDate } from "../utils/authUtils";
import bcrypt from "bcryptjs";
const jwt = require("jsonwebtoken");

const sign = require("jwt-encode");
/**
* All the routes related to Auth are present here.
* These are Publicly accessible routes.
Expand All @@ -30,11 +28,10 @@ export const signupHandler = function (schema, request) {
);
}
const _id = uuid();
const encryptedPassword = bcrypt.hashSync(password, 5);
const newUser = {
_id,
email,
password: encryptedPassword,
password,
createdAt: formatDate(),
updatedAt: formatDate(),
...rest,
Expand All @@ -43,10 +40,7 @@ export const signupHandler = function (schema, request) {
playlists: [],
};
const createdUser = schema.users.create(newUser);
const encodedToken = jwt.sign(
{ _id, email },
process.env.REACT_APP_JWT_SECRET
);
const encodedToken = sign({ _id, email }, process.env.REACT_APP_JWT_SECRET);
return new Response(201, {}, { createdUser, encodedToken });
} catch (error) {
return new Response(
Expand Down Expand Up @@ -76,8 +70,8 @@ export const loginHandler = function (schema, request) {
{ errors: ["The email you entered is not Registered. Not Found error"] }
);
}
if (bcrypt.compareSync(password, foundUser.password)) {
const encodedToken = jwt.sign(
if (password === foundUser.password) {
const encodedToken = sign(
{ _id: foundUser._id, email },
process.env.REACT_APP_JWT_SECRET
);
Expand Down
21 changes: 1 addition & 20 deletions apps/video-library/src/backend/db/users.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { v4 as uuid } from "uuid";
import bcyrpt from "bcryptjs";
import { formatDate } from "../utils/authUtils";
/**
* User Database can be added here.
Expand All @@ -13,25 +12,7 @@ export const users = [
firstName: "Adarsh",
lastName: "Balika",
email: "[email protected]",
password: bcyrpt.hashSync("adarshBalika123", 5),
createdAt: formatDate(),
updatedAt: formatDate(),
},
{
_id: uuid(),
firstName: "John",
lastName: "Doe",
email: "[email protected]",
password: bcyrpt.hashSync("johnDoe123", 5),
createdAt: formatDate(),
updatedAt: formatDate(),
},
{
_id: uuid(),
firstName: "Adarsh",
lastName: "Balak",
email: "[email protected]",
password: bcyrpt.hashSync("adarshBalaki123", 5),
password: "adarshBalika123",
createdAt: formatDate(),
updatedAt: formatDate(),
},
Expand Down
4 changes: 2 additions & 2 deletions apps/video-library/src/backend/utils/authUtils.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { Response } from "miragejs";
const jwt = require("jsonwebtoken");
import dayjs from "dayjs";
import jwt_decode from "jwt-decode";

export const requiresAuth = function (request) {
const encodedToken = request.requestHeaders.authorization;
const decodedToken = jwt.verify(
const decodedToken = jwt_decode(
encodedToken,
process.env.REACT_APP_JWT_SECRET
);
Expand Down
Loading