I want to compare the values entered by the user with the fetched data for authentication and then proceed to next page.
I’m unable to access the values of the retreived response from the MySQL database. The response is in the form of an object.
I’m able to console log the entire response, but cannot access individual values like the value of email and password.
I’ve tried Object.values as well as .find(), but none of them work.
the Object.values(arr) returns ‘undefined’ when i tried console.log(Object.values.email);
import {
LOGIN_URL,
ME_URL,
REGISTER_URL,
REQUEST_PASSWORD_URL
} from "../../app/crud/auth.crud";
import userTableMock from "./userTableMock";
const fetch = require("node-fetch");
export default function mockAuth(mock) {
mock.onPost(LOGIN_URL).reply(({ data }) => {
const { email, password } = JSON.parse(data);
if (email && password) {
console.log(email);
console.log(password);
(async () => {
var arr = {};
await fetch("http://localhost:4000/adminlogin", {
method: "get"
})
.then(res => {
return res.json();
})
.then(response => {
arr = response;
})
.catch(err => console.error(err));
console.log(typeof arr);
console.log(arr);
const user = arr.find(
x =>
x.email.toLowerCase() === email.toLowerCase() &&
x.password === password
);
console.log(Object.values(arr));
if (user) {
return [200, { ...user, password: undefined }];
}
})();
}
return [400];
});
mock.onPost(REGISTER_URL).reply(({ data }) => {
const { email, fullname, username, password } = JSON.parse(data);
if (email && fullname && username && password) {
const user = {
email,
fullname,
username,
password,
roles: [2], // Manager
accessToken: "access-token-" + Math.random(),
refreshToken: "access-token-" + Math.random(),
pic: process.env.PUBLIC_URL + "/media/users/default.jpg"
};
userTableMock.push(user);
return [200, { ...user, password: undefined }];
}
return [400];
});
mock.onPost(REQUEST_PASSWORD_URL).reply(({ data }) => {
const { email } = JSON.parse(data);
if (email) {
const user = userTableMock.find(
x => x.email.toLowerCase() === email.toLowerCase()
);
if (user) {
user.password = undefined;
return [200, { ...user, password: undefined }];
}
}
return [400];
});
mock.onGet(ME_URL).reply(({ headers: { Authorization } }) => {
const accessToken =
Authorization &&
Authorization.startsWith("Bearer ") &&
Authorization.slice("Bearer ".length);
if (accessToken) {
const user = userTableMock.find(x => x.accessToken === accessToken);
if (user) {
return [200, { ...user, password: undefined }];
}
}
return [401];
});
}
I’ve attached the screenshot of the console screen
Please help me with this I’am stuck here for a very long time now.