Commit a82832bf authored by Arthur.Pautrel's avatar Arthur.Pautrel
Browse files

Initial commit

parent 41fecf98
const mongoose = require('mongoose');
module.exports = async () => {
try {
await mongoose.connect('mongodb://127.0.0.1:27017/internet_programming', {
autoIndex: true,
serverSelectionTimeoutMS: 30000 //default 30 seconds
});
console.log("MongoDB connected");
} catch (err) {
console.log("Mongoose: ", err);
}
}
\ No newline at end of file
......@@ -11,6 +11,9 @@ app.use(cors({
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));
app.use(require('./routes'))
// Connect the mongoDB here
require('./configs/db')();
app.use(require('./routes'));
app.listen(3001, () => console.log('App available on http://localhost/3001'));
var mongoose = require('mongoose');
var userSchema = new mongoose.Schema({
email: {
type: String,
unique: true,
required: true
},
username: {
type: String,
unique: true,
required: true
},
firstName: {
type: String,
required: true
},
lastName: {
type: String,
required: true
},
password: {
type: String,
required: true
},
}, {
timestamps: true,
});
var Users = mongoose.model('Users', userSchema);
module.exports = Users;
\ No newline at end of file
This diff is collapsed.
......@@ -10,6 +10,7 @@
"body-parser": "^1.19.2",
"cors": "^2.8.5",
"express": "^4.17.2",
"mongoose": "^6.2.7",
"vue": "^3.2.31",
"vue-router": "^4.0.12"
}
......
......@@ -9,7 +9,7 @@ router.post("/login", (req, res) => {
res.send(result);
})
router.post("/register", (req, res) => {
router.post("/register", async (req, res) => {
const {email, username, firstName, lastName, password, repeatPassword} = req.body;
const result = register(email, username, firstName, lastName, password, repeatPassword);
res.send(result);
......
......@@ -2,26 +2,14 @@ const { getMaxListeners } = require("process");
const { writeUsers, readUsers } = require("../db/db");
const login = (email, password) => {
const data = readUsers();
let boolEmailFounded = 0;
let i = 0;
try {
if (data == "") {
return "You are not registered"
}
for (i = 0; i < data.users.length && !boolEmailFounded; i++) {
if (data.users[i].email == email) {
boolEmailFounded = 1;
}
}
console.log(Users.find({email: "pif"}));
if (!boolEmailFounded) {
return "This email is not registered"
} else if (data.users[i-1].password == password) {
return "Successfully connected !"
} else {
return "Incorrect password"
} catch (err) {
return {
success: false,
error: err || 'error' }
}
}
......
const { writeUsers, readUsers } = require("../db/db");
const Users = require("../models/user");
const register = (email, username, firstName, lastName, password, repeatPassword) =>{
let dataToStore = readUsers();
let boolSameEmail = 0;
let boolSameUsername = 0;
if (dataToStore == "") {
dataToStore = {};
dataToStore.users = [];
}
for (var i = 0; i < dataToStore.users.length && !boolSameEmail && !boolSameUsername; i++) {
if (dataToStore.users[i].email == email) {
boolSameEmail = 1;
} else if (dataToStore.users[i].username == username) {
boolSameUsername = 1;
}
}
if (boolSameEmail) {
return "This email is already used"
}
if (boolSameUsername) {
return "This username is already taken"
}
if (password != repeatPassword) {
return "Passwords are not the same"
}
let myJson = {'email':email};
myJson.username = username;
myJson.firstName = firstName;
myJson.lastName = lastName;
myJson.password = password;
if (password == repeatPassword) {
try {
const newUser = {
email,
username,
firstName,
lastName,
password
}
dataToStore.users[dataToStore.users.length] = myJson;
console.log(dataToStore);
writeUsers(dataToStore);
const createdUser = await Users.create(newUser);
return "Successfully registered !"
return {
success: true,
data: createdUser
}
} catch (err) {
return {
success: false,
error: err || 'error'
}
}
} else {
return {
success: false,
error: "Passwords mismatch"
}
}
}
module.exports = {
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment