<repo_name>NguyenVanLamCNTT/Clone-Trello-Nodejs<file_sep>/server/controllers/users.js
const Users = require('../models/users');

const Accounts = require('../models/accounts');

module.exports.findUser = async function (req, res) {
    try {
        const username = req.params.username;
        const account = await Accounts.findOne({username: username});
        const user = await Users.findById(account.id_user);
        if (!user) throw Error("Error!");
        let data = {
            username: username,
            firstname: user.firstname,
            lastname: user.lastname,
            phone: user.phone,
            email: user.email,
            avatar: user.avatar.path
        }
        res.status(200).json(data);
    }catch (err){
        res.status(400).json({message: err.toString(),success: false});
    }
}
<file_sep>/server/validate/checkToken.validate.js
const Sessions = require('../models/sessions');
module.exports.checkToken = async function(req, res, next){
    const token = req.headers['authorization'].split(' ')[1];
    const session = await Sessions.findOne({token: token});
    if(!session){
        return res.status(400).json({message: "You are not logged in", success: false});
    }
    next();
}
<file_sep>/server/models/accounts.js
const mongoose = require('mongoose');

let accountSchema = new mongoose.Schema({
    id_user: String,
    username: String,
    password: String
})
let Accounts = mongoose.model('Accounts',accountSchema,'accounts');
module.exports = Accounts;
<file_sep>/server/models/users.js
const mongoose = require('mongoose');
let userSchema = new mongoose.Schema({
    firstname: String,
    lastname: String,
    phone: String,
    email: String,
    avatar: {path: String,cloudId: String}
})

let Users = mongoose.model('Users',userSchema,'users');
module.exports = Users;
<file_sep>/server/models/sessions.js
const mongoose = require('mongoose');
let sessionSchema = new mongoose.Schema({
    id_user: String,
    token: String,
    created: Date
})
let Sessions = mongoose.model('Sessions',sessionSchema,'sessions');
module.exports = Sessions;
<file_sep>/app.js
require('dotenv').config();
const express = require('express');
const mongoose = require('mongoose');
const cors = require('cors');
const app = express();
app.use(cors());
app.use(express.json());
app.get('/',(req,res) => {
    res.send('Clone Trello');
});
//Connect mongodb
mongoose.connect(process.env.MONGO_URL,{
    useNewUrlParser: true,
    useUnifiedTopology: true,
})
    .then(()=> console.log('Mongodb connected'))
    .catch(err => console.log(err));
//Router
const RegisterRouter = require('./server/routers/api/register');
const LoginRouter = require('./server/routers/api/login');
const LogoutRouter = require('./server/routers/api/logout');
const UserRouter = require('./server/routers/api/users');
app.use('/api/auth/register', RegisterRouter);
app.use('/api/auth/login', LoginRouter);
app.use('/api/auth/logout', LogoutRouter);
app.use('/api/users', UserRouter);
const PORT = process.env.PORT || 3000

app.listen(PORT,() =>{
    console.log(`http://localhost:${PORT}`);
})
<file_sep>/server/validate/auth/register.validate.js
module.exports.regexp = function (req,res,next){
    const reg_username = /^[a-z0-9]{0,30}$/;
    const reg_phone = /^0[389][0-9]{8}$/;
    const reg_firstname =/^([A-Z][a-z]{0,20} ){0,6}([A-Z][a-z]{0,20} {0,2})$/;
    const reg_lastname = /^([A-Z][a-z]{0,20} ){0,6}([A-Z][a-z]{0,20} {0,2})$/;
    const reg_email = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
    const username = req.body.username;
    const password = <PASSWORD>;
    const firstname = req.body.firstname;
    const lastname = req.body.lastname;
    const phone = req.body.phone;
    const email = req.body.email;
    if(!username || typeof username !== "string" || reg_username.test(username) == false){
        return res.status(400).json({message: "Invalid username!"});
    }
    if(!password || typeof password !== "string" || password.length < 8){
        return res.status(400).json({message: "Invalid password!"});
    }
    if(!firstname || typeof firstname !== "string" || reg_firstname.test(firstname) == false){
        return res.status(400).json({message: "Invalid firstname!"});
    }
    if(!lastname || typeof lastname !== "string" || reg_lastname.test(lastname) == false){
        return res.status(400).json({message: "Invalid lastname!"});
    }
    if(!phone || typeof phone !== "string" || reg_phone.test(phone) == false){
        return res.status(400).json({message: "Invalid phone!"});
    }
    if(!email || typeof email !== "string" || reg_email.test(email) == false){
        return res.status(400).json({message: "Invalid email!"});
    }
    next();
}
const Users = require('../../models/users');
const Accounts = require('../../models/accounts');
module.exports.checkUser = async function(req, res, next){
    const user_phone = await Users.findOne({phone: req.body.phone});
    const user_email = await Users.findOne({email: req.body.email});
    const account = await Accounts.findOne({username: req.body.username});
    if(user_email){
        return res.status(400).json({message: "email exists!", success: false});
    }
    if (user_phone){
        return res.status(400).json({message: "phone exists!", success: false});
    }
    if (account){
        return res.status(400).json({message: "username exists!", success: false});
    }
    next();
}

<file_sep>/server/routers/api/logout.js
const express = require('express');
const router = express.Router();
const auth = require('../../controllers/auth');
const validateCheckToken = require('../../validate/checkToken.validate');

router.get('/',validateCheckToken.checkToken,auth.logout);

module.exports = router;
