Skip to content

Instantly share code, notes, and snippets.

@sj82516
Last active February 22, 2020 02:54
Show Gist options
  • Select an option

  • Save sj82516/683a54746fc664f9a25a103843ea4213 to your computer and use it in GitHub Desktop.

Select an option

Save sj82516/683a54746fc664f9a25a103843ea4213 to your computer and use it in GitHub Desktop.
const express = require('express');
const MongoClient = require('mongodb').MongoClient;
const bodyParser = require('body-parser');
const md5 = require('md5');
const app = express();
app.use(bodyParser.json());
const dbUrl = "mongodb://localhost:27017"
MongoClient.connect(dbUrl, function(err, client){
if(err){
return console.error(err);
}
const userCollection = client.db("test").collection("user");
app.post("/login", function(req, res){
const {
account, password
} = req.body;
const isAccountValid = verifyAccount(account);
if(!isAccountValid){
return res.json(403, {
message: "AccountInvalid"
})
}
// just for demo
// never used md5 for hash password in production
const hashedPassword = md5(password);
userCollection.findOne({
account,
password: hashedPassword
}, function(err, result){
if(err){
return res.json(500, {
message: "DBError"
})
}
if(result){
return res.json(200, {
user: result
})
}
return res.json(404, {
message: "UserNotFound"
})
})
});
app.listen(3001, function(){
console.log("server started!");
})
})
function verifyAccount(account){
const AccountValidFormat = /@helloworld\.com/;
return AccountValidFormat.test(account);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment