Skip to content

Commit

Permalink
Merge pull request #365 from Saksham2k3s/fix/login-redirect-issue-350
Browse files Browse the repository at this point in the history
Solved issue 350
  • Loading branch information
hustlerZzZ authored Jun 24, 2024
2 parents e775e7d + e20345e commit d96dcec
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 17 deletions.
24 changes: 14 additions & 10 deletions server/controllers/Auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const User = require("../models/studentLoginInfo");
const jwt = require("jsonwebtoken");
const Canteen = require("../models/canteenLoginInfo");
const Session = require("../models/session");
const Contact = require('../models/Contact');
const Contact = require("../models/Contact");
const {
forgotPasswordToken,
verifyToken,
Expand Down Expand Up @@ -76,7 +76,7 @@ exports.studentSignup = async (req, res) => {

exports.studentLogin = async (req, res) => {
try {
console.log(req.body);

const { email, password } = req.body;

if (!email || !password) {
Expand All @@ -87,13 +87,16 @@ exports.studentLogin = async (req, res) => {
}

let user = await User.findOne({ email });

if (!user) {
console.log("User not found");
return res.status(401).json({
success: false,
message: "User is not registred",
});
}

console.log("This is our user", user);
const payload = {
email: user.email,
id: user._id,
Expand All @@ -115,7 +118,6 @@ exports.studentLogin = async (req, res) => {
user = user.toObject();
user.token = token;
user.password = undefined;
console.log(user);

// const options = {
// expires: new Date(Date.now() + 3 * 24 * 60 * 60 * 1000),
Expand All @@ -142,7 +144,6 @@ exports.studentLogin = async (req, res) => {
user,
});
} else {

return res.status(403).json({
success: false,
message: "Pasword Incorrect",
Expand Down Expand Up @@ -265,7 +266,11 @@ exports.canteenSignup = async (req, res) => {

// Create a token
const token = jwt.sign(
{ id: canteen._id, email: canteen.email, accountType: canteen.accountType, },
{
id: canteen._id,
email: canteen.email,
accountType: canteen.accountType,
},
process.env.JWT_SECRET,
{
expiresIn: "1h", // Set token expiration time as needed
Expand Down Expand Up @@ -442,21 +447,20 @@ exports.changeCanteenPassword = async (req, res) => {
});
};


//contactUs

exports.saveContactMessage = async (req, res) => {
try {
const { name, email, message } = req.body;
if (!name || !email || !message) {
return res.status(400).send('All fields are required');
return res.status(400).send("All fields are required");
}
const newContact = new Contact({ name, email, message });
await newContact.save();
res.status(201).send('Message received');
res.status(201).send("Message received");
} catch (error) {
console.error('Error saving message:', error.message, error);
res.status(500).send('Error saving message');
console.error("Error saving message:", error.message, error);
res.status(500).send("Error saving message");
}
};

Expand Down
4 changes: 2 additions & 2 deletions server/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ app.use("/api/v1", studentRoutes);
app.use("/api/v1", uploadFileRouter);
app.use('/api/contact', contactRoutes);

app.listen(PORT, () => {
console.log(`Server started succesfully at ${PORT}`);
app.listen(process.env.PORT, () => {
console.log(`Server started succesfully at ${process.env.PORT}`);
});

//getting connected to databse
Expand Down
13 changes: 8 additions & 5 deletions src/pages/Login.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,16 @@ function Login() {
async function submitHandler(event) {
event.preventDefault();

const apiUrl =
formData.accountType === "User"
? `${process.env.REACT_APP_BASE_URL}/studentLogin`
: `${process.env.REACT_APP_BASE_URL}/canteenLogin`;
// const apiUrl =
// formData.accountType === "User"
// ? `${process.env.REACT_APP_BASE_URL}/studentLogin`
// : `${process.env.REACT_APP_BASE_URL}/canteenLogin`;

const apiUrl = 'http://localhost:8000/api/v1/studentLogin'

try {
const response = await axios.post(apiUrl, formData);
console.log("This is response data", response.data);
toast.success("User Logged in");

if (rememberMe) {
Expand All @@ -84,7 +87,7 @@ function Login() {
}

} catch (error) {
console.error(error);
console.error("This is error",error);
toast.error("Failed to login");
}
}
Expand Down

0 comments on commit d96dcec

Please sign in to comment.