Answer the question
In order to leave comments, you need to log in
Why does it redirect back when I try to go to the registration page?
There is a registration logic and prescribed routes, when the site is first opened, the registration page opens, the account is registered, recorded in mongo. The main page opens, but when I try to go back to the registration page (/register), it throws me back to the main page, with the same login, I googled it, but I don’t know exactly what to look for.
I'll throw off the register logic and routes, if you need anything else, write. Models for mongo I think do not need to be thrown. Maybe the problem is on the React side?
Routes
//REGISTER
router.post("/register", async (req, res) => {
try {
//generate new password
const salt = await bcrypt.genSalt(10);
const hashedPassword = await bcrypt.hash(req.body.password, salt);
//create new user
const newUser = new User({
username: req.body.username,
email: req.body.email,
password: hashedPassword,
});
//save user and respond
const user = await newUser.save();
res.status(200).json(user);
} catch (err) {
res.status(500).json(err)
}
});
//LOGIN
router.post("/login", async (req, res) => {
try {
const user = await User.findOne({ email: req.body.email });
!user && res.status(404).json("user not found");
const validPassword = await bcrypt.compare(req.body.password, user.password)
!validPassword && res.status(400).json("wrong password")
res.status(200).json(user)
} catch (err) {
res.status(500).json(err)
}
});
module.exports = router;
const PORT = config.get('PORT') || 5000
mongoose.connect(
process.env.MONGO_URL,
{ useNewUrlParser: true, useUnifiedTopology: true, useCreateIndex: true },
() => {
console.log("Connected to MongoDB");
}
);
app.use("/images", express.static(path.join(__dirname, "public/images")));
//middleware
app.use(express.json());
app.use(helmet());
app.use(morgan("common"));
const storage = multer.diskStorage({
destination: (req, file, cb) => {
cb(null, "public/images");
},
filename: (req, file, cb) => {
cb(null, req.body.name);
},
});
const upload = multer({ storage: storage });
app.post("/api/upload", upload.single("file"), (req, res) => {
try {
return res.status(200).json("File uploded successfully");
} catch (error) {
console.error(error);
}
});
app.use("/api/auth", authRoute);
app.use("/api/users", userRoute);
app.use("/api/posts", postRoute);
app.use("/api/conversations", conversationRoute);
app.use("/api/messages", messageRoute);
app.listen(PORT, () => {
console.log("Backend server is running! "+ PORT)
});
Answer the question
In order to leave comments, you need to log in
So, why are you throwing back? if redirecting to the page does your front, skin the react component of your registration
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question