I have an app in railway that has 2 parts, the front and the backend
The backend tells me to use that to communicate internally
This is my code in frontend:
const handleSubmit = async (event) => {
event.preventDefault()
try {
const result = await axios.post(
'http://localhost:3001/auth/login',
{
username,
password,
}
)
console.log(result.status)
setCookie('access_token', result.data.token)
window.localStorage.setItem('token', result.data.token)
window.localStorage.setItem('userID', result.data.userID)
console.log(result.data.userID)
if (result.data.userID === undefined) {
logout()
alert('Acceso denegado')
} else {
navigate('/home')
}
} catch (error) {
console.error(error)
alert('Acceso denegado')
}
}
And this my backend :
router.post('/login', async(req,res) =>{
const { username,password } = req.body;
const user = await UserModel.findOne({ username });
console.log(req)
if (!user){
return res.json({ message: "user doesn't exist"});
}
const isPasswordValid = await bcrypt.compare(password,user.password);
if (!isPasswordValid){
return res.json({message: "user or password is incorrect"});
}
const token = jwt.sign({ id : user._id }, "secret");
res.json({ token, userID: user._id});
});
An this the userschema in the mongodb
import mongoose from "mongoose";
const UserSchema = new mongoose.Schema({
username: { type:String, required:true, unique:true},
password: { type:String, required:true}
});
export const UserModel = mongoose.model('users', UserSchema);
What do I have to use to communicate with the backend from the front? I try with:
'http://backend.railway.internal:3001/auth/login',
'https://backend.railway.internal:3001/auth/login',
'http://backend.railway.internal1/auth/login',
And nothing, what am I doing wrong?