Redirecting to a React Component - Express - reactjs

I have a Login component on the front in which I make a POST to a rout on the server:
const handleSubmit = async (e) => {
e.preventDefault();
try {
fetch("http://localhost:3000/login", {
method: "POST",
mode: "cors",
headers: {
"Content-Type": "application/json",
"Access-Control-Allow-Origin": "*",
},
body: JSON.stringify({
email,
password,
}),
});
} catch (error) {
console.log(error.message);
}
};
On the server side, I validate the information sended from the front:
app.post("/login", async (req, res) => {
const user = await Users.findOne({
where: { email: req.body.email, password: req.body.password },
});
if (user) {
console.log(user);
console.log("usuario logeado");
res.status(200).send("LOGGED");
} else {
console.log("Usuario no Registrado");
}
});
I want to redirect to a component Home on the front once I validate the user and idk how to do it.

You need to read the response from the server and figure out what you want to do with it. I'd recommend not sending just a string from the server, so this solution will restructure a bit to hopefully help you understand and expand for your own needs.
// From your server - we're sending some json that we can
// read in the front end code. Maybe you also want to send
// the user object or other data, and json is great for that.
app.post("/login", async (req, res) => {
const user = await Users.findOne({
where: { email: req.body.email, password: req.body.password },
});
if (user) {
console.log(user);
console.log("usuario logeado");
res.status(200).send({
isLoggedIn: true,
});
} else {
console.log("Usuario no Registrado");
res.status(200).send({
isLoggedIn: false,
});
}
});
const handleSubmit = async (e) => {
e.preventDefault();
try {
const response = await fetch("http://localhost:3000/login", {
method: "POST",
mode: "cors",
headers: {
"Content-Type": "application/json",
"Access-Control-Allow-Origin": "*",
},
body: JSON.stringify({
email,
password,
}),
});
// Get the server response as json
const data = await response.json()
if(data.isLoggedIn) {
// Redirect to the account
window.location.href = "/account"
} else {
// Show some error message
window.alert("Invalid login")
}
} catch (error) {
console.log(error.message);
}
};

Related

retrieving data from an api call

I have successfully retrieved data from a login API call and I return the data variable which logs user information eg. id, token, email and this is successfully printed to the console.
async function login(email: string, password: string, rememberMe: boolean) {
const requestOptions = {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ email, password, rememberMe }),
};
await fetch(`${API_URL}/auth/login`, requestOptions).then((response) => {
if (response.ok === true) {
response.json().then((data) => {
console.log(data);
if (data.success === true) {
localStorage.setItem("USER_ID", data.id);
localStorage.setItem("EMAIL", data.email);
localStorage.setItem("ACCESS_TOKEN_KEY", data.token);
return data;
} else {
return Promise.reject(new Error("toast.user.general_error"));
}
});
} else {
return Promise.reject(new Error(response.statusText));
}
});
}
however I get user = undefined when logging to the console suggesting that my data variable is undefined
function login(email: string, password: string, rememberMe: boolean) {
return (dispatch: ThunkDispatch<{}, void, AnyAction>) => {
authService.login(email, password, rememberMe).then(
(user) => {
history.push("/student/dashboard");
console.log("user = ", user);
},
(error) => {
dispatch(failure(error.toString()));
}
);
};
}
why am I not retrieving the user variable from my fetch request? Should I be wrapping the data variable with a promise before returning it?
Login must return something at the top-level, not from within a then block.
Since you're already using async/await, try it like this.
async function login(email: string, password: string, rememberMe: boolean) {
const requestOptions = {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ email, password, rememberMe }),
};
const response = await fetch(`${API_URL}/auth/login`, requestOptions);
if (!response.ok) return new Error(response.statusText);
const data = await response.json();
console.log(data);
if (data.success !== true) return new Error("toast.user.general_error");
return data;
}

React.js POST request- "TypeError: res.status is not a function"

I am trying to use HTTP POST to send a request to a server to change account settings for an app I am making. This is the code I am using:
Server route:
app.post('/api/account/changeSettings', function(req, res) {
authJwt.verifyToken(req, res, function(req, res) {
accountModule.changeSettings(req, res, function(req, res1) {
console.log(res);
return res.status(200).json({success: true, statusCode: 200, message:
messages.accountSettingsChanged});
});
});
})
Middleware function:
changeSettings = (req, res, next) => {
checkForWarning(req, res, function(req, res) {
var accountQuery = ( { username: req.headers["username"] } );
var newData = { $set: {
display_name: req.body.newDisplayName,
profile_bio: req.body.profileBio,
gender: req.body.gender,
genderPronoun: req.body.gender_pronoun,
notify_achievement_received: req.body.notifyAchievementReceived,
notify_submission_featured: req.body.notifySubmissionFeatured,
notify_submission_comment: req.body.notifySubmissionComment,
notify_submission_upvote: req.body.notifySubmissionUpvote,
notify_pm_received: req.body.notifyPMReceived,
profile_image_url: req.body.profileImageURL,
uses_gravatar: req.body.usesGravatar
}};
Account.updateOne(accountQuery, newData, function(err, res) {
if(err) throw err;
});
next(req, res);
});
}
Code for sending the request:
handleSubmit(e) {
const token = localStorage.jwtToken;
setAuthToken(token);
const decoded = jwt_decode(token);
const currentUsername = decoded.name;
fetch(`${Globals.API_URL}/account/changeSettings`, {
method: 'POST',
headers: {
"x-access-token": token,
'username': currentUsername,
'Content-Type': 'application/json'
},
body: JSON.stringify({
displayName: this.state.displayName,
profileBio: this.state.profileBio,
password: this.state.password,
gender: this.state.gender,
genderPronoun: this.state.genderPronoun,
notifyAchievementReceived: this.state.notifyAchievementReceived,
notifySubmissionFeatured: this.state.notifySubmissionFeatured,
notifySubmissionComment: this.state.notifySubmissionComment,
notifySubmissionUpvote: this.state.notifySubmissionUpvote,
notifyPMReceived: this.state.notifyPMReceived,
profileImageURL: this.state.profileImageURL,
usesGravatar: this.state.usesGravatar
})}).then((res) => res.json())
.then(res => {
if(res.success) {
window.location.reload(false);
} else {
console.log(res.message);
}
});
}
"verifyToken" and "checkForWarning" work just fine. However, when I get to sending the response back to the client, I get this error on the server:
TypeError: res.status is not a function.
How do I make sending a 200 response work?

NEXT.js API route wont accept POST requests

I have a Next.js API route set up at "pages/api/contact". When I process a GET request or navigate to localhost:3000/api/contact I can see the api is working. However, whenever I try to process a get request to this API route nothing happens. I have tried using axios and fetch but nothing seems to work for post request. See below. Any help would be greatly appreciated.
called from a component when a button is clicked
const handleClick = async (e) => {
e.preventDefault();
console.log("in handleSubmit");
try {
const res = await axios.post(
"http://localhost:3000/api/contact",
{
firstName,
lastName,
email,
},
{
headers: {
"Content-Type": "application/json",
},
},
console.log(res) //this comes back undefined
);
} catch (e) {}
};
in pages/api/contact
export default async function sendEmail(req, res) {
const { firstName, lastName, email } = req.body;
console.log(req.method);
if (req.method === "POST") {
return res.status(200).json({
message: "This is in post",
});
} else {
return res.status(200).json({
message: "This is not a post",
});
}
}
I think it syntax error
try {
const res = await axios.post(
"http://localhost:3000/api/contact",
{
firstName,
lastName,
email,
},
{
headers: {
"Content-Type": "application/json",
},
},
);
console.log(res) //check now
} catch (e) {}

How to send token through headers by using axios post method in react

In my react app i am using axios to perform the REST api requests.
But it's unable to send the Authorization header with the request.
Here is my code:
This is authentication.js
async login(data) {
try {
const res = await axios.post(`'http://localhost:5000'/api/login`, data);
this.subject.next(true);
return res;
} catch (error) {
throw error;
}
}
This is login.js
async handleSubmit(e) {
e.preventDefault();
try {
const res = await auth.login(this.state.data);
tokenService.saveToken(res.data.token);
this.setState({});
swal({
title: "Good job!",
text: "Login successfully!",
icon: "success",
});
}
catch (error) {
swal({
title: "incorrect or password!",
text: "Login failed!",
icon: "error",
});
}
}
You can use Axios to create an instance of it with the headers passed to it save in local storage. Then, use that instance to further make requests. In this way, you don't to include it in every request.
const instance = axios.create({
baseURL: 'https://some-domain.com/api/',
headers: {'Authorization': bearer <TOKEN_FROM_LOCALSTORAGE>}
});
Use the instance to make request
instance.get("users")
.then(res => {
console.log(res);
console.log(res.data);
})
You can use this instance and customize it according to your instance so that code won't repeat. For further reference
Store it in localstorage and then concatenate it with 'Bearer'
let bearer = 'Bearer ' + JSON.parse(localStorage.getItem('token'));
//payload is the data which you're trying to send to the api endpoint
axios({
method: 'post',
url: '/api-endpoint',
headers: {
Authorization: bearer
},
data: payload
})
.then(response => response.json())
.then(json => json)
.catch(error => {
throw error;
});
check if the user is authenticated to use the Get or Post requests made by them
isAuthenticated() {
const token = localStorage.getItem('token');
}
Use the token to make the post request
axios({
method: 'post',
url: ''http://localhost:5000'/api/login',
{ headers: {"authorization" : token} }
data: payload
}),
.then(response => response.json())
.then(json => json)
.catch(error => {
throw error;
});
Handle your login
async handleSubmit(e) {
e.preventDefault();
try {
const res = await auth.login(this.state.data);
tokenService.saveToken(res.data.token);
this.setState({});
swal({
title: "Good job!",
text: "Login successfully!",
icon: "success",
});
}
catch (error) {
swal({
title: "incorrect or password!",
text: "Login failed!",
icon: "error",
});
}
}
Why you don't use axios interceptors like this:
axiosInstance.interceptors.request.use(
config => {
config.headers.authorization = localStorage.getItem("token");
return config;
},
error => Promise.reject(error)
);
Or declared on https://github.com/axios/axios/issues/1383

Proxy to express - 500-timeout - Server code is executed more then once

I don't know what is going on. When i try to send request to the backend to add follower(my route bellow), I get server tiemout error instead of sucess, but in my database the follower is added correctly(and removed), buuuut not always. Sometimes it saves 3 times the same result(follower to db), or sometimes doesn't delete the follower.
And the problem is that i have no idea what's is going on.
In my console i have this error sometimes i see this:
[HPM] Error occurred while trying to proxy request /api/users/user/follow from 127.0.0.1:8080 to http://[::1]:1648 (ECONNRESET) (https://nodejs.org/api/errors.html#errors_common_system_errors)
setFollower route:
const setFollowing = async (req, res, next) => {
try {
const userId = req.body.userId;
const followId = req.body.followId;
await User.findByIdAndUpdate(
userId,
{
$push: {
following: followId,
},
},
);
next();
} catch (err) {
res.status(400).json({
error: err,
});
}
};
const setFollower = async (req: Request, res: Response) => {
try {
const followId = req.body.followId;
const userId = req.body.userId;
const result = await User.findByIdAndUpdate(
followId,
{
$push: {
followers: userId,
},
},
{ new: true },
)
.populate('following', '_id name')
.populate('followers', '_id name')
const followerResult = { ...result._doc };
const { photo, salt, passwordHash, ...rest } = followerResult;
return res.status({ ...rest });
} catch (err) {
res.status(400).json({
error: err,
});
}
};
router.put(
'/user/follow',
isUserSignIn,
setFollowing,
setFollower,
);
sending request on button click
try {
setLoading(true);
const response = await fetch('/api/users/user/follow', {
body: JSON.stringify({
followId: params.userId,
userId: loggedInUser._id,
}),
headers: {
'Accept': 'application/json',
'Authorization': `Bearer ${token}`,
'Content-Type': 'application/json',
},
method: 'PUT',
});
const data = await response.json();
setLoading(false);
setFollowing(true);
} catch (err) {
if (err.message) {
setServerError(err.message);
} else {
setServerError(JSON.stringify(err));
}
}
my repo: https://github.com/bartek-fecko/fullstackapp
for my assumption, you're using express, given the logs you have in your question. The
key is to set the timeout property on server (the following sets the timeout to one
second, use whatever value you want):
var server = app.listen(app.get('port'), function() {
debug('Express server listening on port ' + server.address().port);
});
server.timeout = 1000;

Resources