Created
September 28, 2021 05:09
-
-
Save hieptl/441e4f197256cac77507e9679611c77a to your computer and use it in GitHub Desktop.
users.js - Creating a New Account - Tinder Clone
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
app.post("/users/create", upload.single("avatar"), (req, res, next) => { | |
// validate the avatar. The avatar is requied. | |
const file = req.file; | |
if (!file || !file.mimetype.includes("jpeg")) { | |
res.status(200).jsonp({ | |
message: "Please upload your avatar, the image should be .jpg format", | |
}); | |
} else { | |
const avatar = `/img/${file.filename}`; | |
// get user information and check the required fields. | |
const { email, password, fullname, age, gender, ccUid } = req.body; | |
if (email && password && fullname && age && gender) { | |
// validate the email existed in the system, or not. | |
const sql = "SELECT * FROM user_account WHERE user_email = ?"; | |
dbConn.query(sql, [email], function (err, result) { | |
if (result && result.length !== 0) { | |
res.status(200).jsonp({ message: 'The email existed in the system' }); | |
} else { | |
// create a new user if the email did not exist in the sytem. | |
const users = [[email, password, fullname, age, avatar, gender, ccUid]]; | |
const insertSql = "INSERT INTO user_account (user_email, user_password, user_full_name, user_age, user_avatar, user_gender, user_cometchat_uid) VALUES ?"; | |
dbConn.query(insertSql, [users], function (err, result) { | |
if (err) { | |
res.status(200).jsonp({ message: "Cannot create your account, please try again" }); | |
} else { | |
res.status(200).jsonp({ avatar, insertId: result.insertId }); | |
} | |
}); | |
} | |
}); | |
} else { | |
return res.status(200).jsonp({ message: "Please input required fields" }); | |
} | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment