Authenticating Functionality
// routes/admin.js var express = require('express'); var router = express.Router(); router.post('/login', function(req, res, next) { const login = require('../controllers/login'); login.postLogin(req, res); }); module.exports = router;
// controllers/login.js class Login{ constructor(){ this.deepcopy = require('deepcopy'); this.vdict = require('../config'); this.usersdb = require('../models/usersdb'); this.bcrypt = require('bcryptjs'); } postLogin(req, res){ const self = this; const data = this.deepcopy(this.vdict); delete data.databaseAccess; this.usersdb.checkUser(req, function(user){ if(user){ if(self.bcrypt.compareSync(req.body.password, user.password)){ req.session.user = user; data.success = true; res.json(data); }else{ data.message = 'The password is wrong.'; data.success = false; res.json(data); } }else{ data.message = 'The email is wrong.'; data.success = false; res.json(data); } }); } }//end class module.exports = new Login();
// models/usersdb.js class Usersdb{ constructor(){ const mongoose = require('mongoose'); const bcrypt = require('bcryptjs'); const usersSchema = new mongoose.Schema({ username: {type: String, required: true}, userid: {type: String, required: true}, password: {type: String, required: true}, email: {type: String, required: true}, role: {type: String, required: true}, info: {type: String, required: false}, date: {type: Date, required: true} }); const users = mongoose.model('users', usersSchema); this.users = users; this.bcrypt = bcrypt; users.findOne(function (err, user){ if (err) return console.error(err); if(!user){ const hash = bcrypt.hashSync('password', 12); const id = (new Date()).getTime().toString(36) + Math.random().toString(36).slice(2); const root = new users({userid:id, username:'root', password:hash, email:'root@multimedia.com', role:'Admin', info:'test', date: new Date()}); root.save(function (err, root){ if (err) return console.error(err); }); } }); } checkUser(req, callback){ this.users.findOne({email:req.body.email}, function (err, user){ if (err) return console.error(err); return callback(user); }); } }//class ending module.exports = new Usersdb();
import React from 'react'; import './login.scss'; import tool from '../tool.js'; class Login extends React.Component{ constructor(props){ super(props); this.state = { message: '', email: '', password: '', siteTitle: 'Dashboard' }; } onChangeHandler = (event) => { let nam = event.target.name; let val = event.target.value; this.setState({[nam]: val}); } onSubmitHandler = async (event) => { event.preventDefault(); const option = { email: this.state.email, password: this.state.password } const result = await tool.fetchPostAPI('/admin/login', option); this.setState({ message: result.message, siteTitle: result.siteTitle }); } render(){ return( <div className='Login region'> <form id="login" action="/admin/login" method="post" onSubmit={this.onSubmitHandler}> <span>Email:</span><input onChange={this.onChangeHandler} type="email" name="email" required /> <span>Password:</span><input onChange={this.onChangeHandler} type="password" name="password" required /> <span></span><input type="submit" /> </form> <div className="message"> {this.state.message} </div> </div> ); } } export default Login;
GitHub: "https://github.com/Sokhavuth/emultimedia
Heroku: https://khmerweb-emultimedia.herokuapp.com/
Comments
Post a Comment