Commit 41fecf98 authored by root's avatar root
Browse files

Apps working

parent a937fef0
const fs = require("fs");
const path = require('path');
const { encode } = require("punycode");
const readUsers = () => {
let rawdata = fs.readFileSync(path.join(process.cwd(), 'db/user.json'), 'utf8');
let rawdata = fs.readFileSync(path.join(process.cwd(), 'db/users.json'), 'utf8');
if(!rawdata)
return [];
......@@ -10,7 +11,7 @@ const readUsers = () => {
}
const writeUsers = async (data) => {
fs.writeFileSync(path.join(process.cwd(), 'db/user.json'), JSON.stringify(data), 'utf8');
fs.writeFileSync(path.join(process.cwd(), 'db/users.json'), JSON.stringify(data),"utf8");
}
module.exports = {
......
{"users":[{"email":"emailTest","username":"usernameTest","firstName":"firstNameTest","lastName":"lastNameTest","password":"passwordTest"}]}
\ No newline at end of file
const express = require('express');
var bodyParser = require("body-parser");
const cors = require('cors')
const bodyParser = require("body-parser");
const app = express();
app.use(cors({
origin: "http://localhost:3000"
}))
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));
......
This diff is collapsed.
......@@ -7,8 +7,9 @@
"start": "nodemon index.js"
},
"dependencies": {
"express": "^4.17.2",
"body-parser": "^1.19.2",
"cors": "^2.8.5",
"express": "^4.17.2",
"vue": "^3.2.31",
"vue-router": "^4.0.12"
}
......
const express = require('express');
const { login } = require("../services/login");
const { register } = require("../services/register");
var router = express.Router();
router.post("/login", (req, res) => {
const {email, password} = req.body;
const {email, password} = req.body
const result = login(email, password);
// console.log("body: ", req.body);
// res.json({name: "test"});
res.send(result);
})
router.post("/register", (req, res) => {
res.send('DEEEEETAIL');
const {email, username, firstName, lastName, password, repeatPassword} = req.body;
const result = register(email, username, firstName, lastName, password, repeatPassword);
res.send(result);
})
module.exports = router;
\ No newline at end of file
const { getMaxListeners } = require("process");
const { writeUsers, readUsers } = require("../db/db");
const login = (email, password) => {
const data = readUsers();
let boolEmailFounded = 0;
let i = 0;
const users = readUsers();
if (data == "") {
return "You are not registered"
}
return "login return"
for (i = 0; i < data.users.length && !boolEmailFounded; i++) {
if (data.users[i].email == email) {
boolEmailFounded = 1;
}
}
if (!boolEmailFounded) {
return "This email is not registered"
} else if (data.users[i-1].password == password) {
return "Successfully connected !"
} else {
return "Incorrect password"
}
}
module.exports = {
......
const { writeUsers, readUsers } = require("../db/db");
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;
dataToStore.users[dataToStore.users.length] = myJson;
console.log(dataToStore);
writeUsers(dataToStore);
return "Successfully registered !"
}
module.exports = {
register
}
\ No newline at end of file
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