I'm developing a simple forum website. It has a react frontend + node backend, with mongodb cloud as database and AWS Cognito for signup/ verification/ sign in. When a user signs up: 1. create a user in Cognito user pool 2. create a user in mongodb users collection 3. update the user's userID entry inside Mongodb users collection using the User ID (Sub) from Cognito.
My user schema:
const userSchema = new mongoose.Schema(
{
userID: {type: String},
username: {type: String, required: true, unique: true},
email: {type: String, required: true},
issuesPosted: [{ type: Schema.Types.ObjectId, ref: 'issue' }],
issuesFavorited: [{ type: Schema.Types.ObjectId, ref: 'issue' }],
commentsAdded: [{ type: Schema.Types.ObjectId, ref: 'comment' }],
profileImageUrl: { type: String, default: '(a link to default user pic in S3)' }
}
)
Everything works in dev. However in deploy, I noticed after signing up in my forum website, it ONLY creates a new user in aws cognito, but NOT in mongodb. Same problem with sign in: it doesn't fetch any user data from mongodb. In a page where it's supposed to show existing posts which I created during dev as mock users, it also doesn't show those data in deploy.
Could anyone help me?
Below is the sign up page:
//a bunch of imports
export default function Signup() {
const [username, setUsername] = useState('')
const [email, setEmail] = useState('')
const [password, setPassword] = useState('')
const [verifyProcess, setVerifyProcess] = useState(false)
const [OTP, setOTP] = useState('')
const navigate = useNavigate()
const { authenticate, getCurrentUsername, getCurrentUserId } = useContext(AccountContext)
// when user clicks sign up
const onSubmit = (e) => {
e.preventDefault()
// setups for sign up in Cognito
const verifyAccount = (e) => {
e.preventDefault()
// setups for verification in Cognito
user.confirmRegistration(OTP, true, async(err, data) => {
if (err) {
alert(`Couldn't verify account: ${err}`)
} else {
try{
// create user in mongodb (the problem arises here)
await axios.post('/api/signup', { username, email })
// authenticate the user in cognito to get the session (works fine)
// update userID in mongodb (the problem also here)
const realUsername = getCurrentUsername()
const userID = await getCurrentUserId()
try {
await axios.put(`/api/useridupdate/${realUsername}`, {
userID: userID
})
} catch (err) {
console.log('Error updating userID:', err);
}
alert('Account verified, please log in')
navigate('/signin')
}catch(err){
console.error('Error creating user in MongoDB:', err)
}
}
})
}
// const resendVerifyCode = () =>{}
// return (page components)
}
Below are the logs after user sign up in deploy:
HTTP 01/07/2024 22:06:07 ::1 POST /api/signup
HTTP 01/07/2024 22:06:07 ::1 Returned 200 in 1 ms
HTTP 01/07/2024 22:06:07 ::1 PUT /api/useridupdate/leahalt
HTTP 01/07/2024 22:06:07 ::1 Returned 200 in 1 ms
It seems that it's creating a user in mongodb and updating the userID entry successfully. What could be the problem?
I have the mongodb connection data in .env. When I noticed the problem, I tried directly putting the connection data in my server.js to test the connection in deploy, yet this still failed. Therefore I don't think it's a problem of the .env file.
I've deployed on both local machine and EC2 instance, similar result. The build React frontend seems to be working fine, tho.