Hello @abhishek_09 ,
Welcome to Google Cloud Community!
The given code sets up Express.js in Node.js, it's not immediately obvious from your code why the cookies might not be getting sent in the headers.
You can try troubleshooting with these following steps:
1. Ensure HTTPS for secure cookies `(secure: true)`. Secure your connection.
2. Don't miss `httpOnly: true` for session cookie.
3. Set sameSite for cross-origin cookie behavior.
4. Inspect response headers in your browser.
5. Enable trust proxy in Express.js for proxies.
6. Use `res.cookie()` correctly with attributes.
7. Add console logs for code analysis. Debug your code.
Alternatively, if you encounter any further issues, you can refer to this Stack Overflow post which discusses the same problem.
If the provided steps and resources do not resolve your issue, you can contact Google Cloud Support for further assistance.
Hi Julia, Thanks for your reply. I have tried all that.
app.post('/signup', upload.single('myfile'), async (req, res) => {
try {
winston.info('Received signup request');
const formFields = JSON.parse(req.body.formFields);
const { firstName, lastName, email, password } = formFields;
const referredBy = req.query.referralId; // The ID of the person who referred the user (if any)
const referralId = uuidv4(); // The unique ID for this user to refer others
const hashedPassword = await bcrypt.hash(password, 10);
const user = { firstName, lastName, email, password: hashedPassword, referredBy, referralId };
const userCollection = client.db('instadb').collection('users');
const existingUser = await userCollection.findOne({ email: email });
if (existingUser) {
return res.status(409).json({ error: 'Email already exists' });
}
const result = await userCollection.insertOne(user);
if (result.acknowledged) {
winston.info('User created successfully:', result.insertedId);
// Create a JWT token for the user
const jwtToken = jwt.sign({ userId: result.insertedId }, JWT_SECRET, { expiresIn: '1h' });
// Set the JWT token in a cookie
res.cookie('token', jwtToken);
if (req.file) {
const file = req.file;
const fileContent = file.buffer;
const fileExtension = path.extname(file.originalname);
const filename = `${uuidv4()}${fileExtension}`;
const params = {
Bucket: AWS_BUCKET_NAME,
Key: filename,
Body: fileContent
};
await uploadFileToS3(params, result.insertedId.toString());
winston.info('File uploaded and reference saved');
}
// Retrieve the original URL from the session
const originalUrl = req.session.originalUrl;
console.log('ORIGINAL URL FROM SESSION', req.session.originalUrl)
// Redirect to the original URL if present, otherwise redirect to the user's profile
const redirectUrl = originalUrl ? originalUrl : `/rate_profile?userId=${result.insertedId.toString()}`;
console.log('FINAL REDIRECT URL', redirectUrl)
// Clear the originalUrl from the session
req.session.originalUrl = null;
return res.json({ redirectUrl: redirectUrl });
} else {
winston.error('Failed to create user in MongoDB');
res.status(500).send('Failed to create user');
}
} catch (error) {
winston.error('Error creating user:', error);
res.status(500).json({ error: 'Failed to create user' });
}
}); run out of options to try.
Now, there is a cloud shell quota exceeded error. Creating a case needs standard support, it appears. This is too much hassle for a simple app. Just going to deploy it somewhere else. Thanks for your reply.