Skip to content

Commit c255dcc

Browse files
fixed - auth required typo and validate tocken expresion (#11)
* fixed - auth required typo and validate tocken expresion * fixed - version --------- Co-authored-by: ct-amit <[email protected]>
1 parent 655e7a3 commit c255dcc

File tree

9 files changed

+3781
-6791
lines changed

9 files changed

+3781
-6791
lines changed

package-lock.json

Lines changed: 3769 additions & 6776 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "mock-data-api-nextjs",
3-
"version": "1.2.0",
3+
"version": "1.3.0",
44
"private": false,
55
"scripts": {
66
"dev": "next dev -p 3010",

src/config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@ export const JWT_API = {
33
timeout: '1 days'
44
};
55

6-
export const NO_AUTHENTICATION_REQUIRED = true;
6+
export const AUTHENTICATION_REQUIRED = false;

src/pages/api/account/login.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import jwt from 'jsonwebtoken';
2-
import { JWT_API, NO_AUTHENTICATION_REQUIRED } from 'config';
2+
import { JWT_API } from 'config';
33
import cors from 'utils/cors';
44
import users from 'data/users.json';
55
import { messages } from 'utils';
@@ -9,7 +9,7 @@ const JWT_SECRET = JWT_API.secret;
99
const JWT_EXPIRES_TIME = JWT_API.timeout;
1010

1111
export default async function handler(req, res) {
12-
await cors(req, res, NO_AUTHENTICATION_REQUIRED);
12+
await cors(req, res, false);
1313
const { email, password } = req.body;
1414
const user = users.find((_user) => _user.email === email);
1515

src/pages/api/account/me.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// project imports
2-
import { JWT_API, NO_AUTHENTICATION_REQUIRED } from 'config';
2+
import { JWT_API } from 'config';
33
import cors from 'utils/cors';
44
import { verify } from 'jsonwebtoken';
55
import users from 'data/users.json';
@@ -8,7 +8,7 @@ import users from 'data/users.json';
88
const JWT_SECRET = JWT_API.secret;
99

1010
export default async function handler(req, res) {
11-
await cors(req, res, NO_AUTHENTICATION_REQUIRED);
11+
await cors(req, res);
1212
const { authorization } = req.headers;
1313
if (!authorization) {
1414
return res.status(401).json({ message: 'Token Missing' });

src/pages/api/account/register.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
import cors from 'utils/cors';
22
import users from 'data/users.json';
33
import { messages } from 'utils';
4-
import { NO_AUTHENTICATION_REQUIRED } from 'config';
54

65
export default async function handler(req, res) {
7-
await cors(req, res, NO_AUTHENTICATION_REQUIRED);
6+
await cors(req, res, false);
87
const { id, email, password, firstName, lastName } = req.body;
98
if (!email || !password) {
109
return res.status(400).json({ message: messages.errorMessages.enterEmailAndPassword });

src/pages/api/menu/dashboard.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import cors from 'utils/cors';
22
import dashboard from 'data/dashboard.json';
3-
import { NO_AUTHENTICATION_REQUIRED } from 'config';
43

54
export default async function handler(req, res) {
6-
await cors(req, res, NO_AUTHENTICATION_REQUIRED);
5+
await cors(req, res);
76
return res.status(200).json({ dashboard });
87
}

src/pages/api/menu/widget.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import cors from 'utils/cors';
22
import widget from 'data/widget.json';
3-
import { NO_AUTHENTICATION_REQUIRED } from 'config';
43

54
export default async function handler(req, res) {
6-
await cors(req, res, NO_AUTHENTICATION_REQUIRED);
5+
await cors(req, res);
76
return res.status(200).json({ widget });
87
}

src/utils/init-middleware.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// project imports
2-
import { JWT_API } from 'config';
2+
import { AUTHENTICATION_REQUIRED, JWT_API } from 'config';
33
import { verify } from 'jsonwebtoken';
44
import users from 'data/users.json';
55

@@ -9,13 +9,13 @@ const JWT_SECRET = JWT_API.secret;
99
// Helper method to wait for a middleware to execute before continuing
1010
// And to throw an error when an error happens in a middleware
1111
export default function initMiddleware(middleware) {
12-
return (req, res, NO_AUTHENTICATION_REQUIRED = false) =>
12+
return (req, res, validateToken = AUTHENTICATION_REQUIRED) =>
1313
new Promise((resolve, reject) => {
1414
middleware(req, res, (result) => {
1515
if (result instanceof Error) {
1616
return reject(result);
1717
}
18-
if (!NO_AUTHENTICATION_REQUIRED) {
18+
if (validateToken) {
1919
const { authorization } = req.headers;
2020
if (!authorization) {
2121
return res.status(401).json({ message: 'Token Missing' });

0 commit comments

Comments
 (0)