Logout Functionality

import React from 'react';
import {Redirect} from 'react-router-dom';
import './dheader.scss';
import tool from '../../tool';

class DHeader extends React.Component{
  constructor(props){
    super(props);
    this.state = {
      dTitle: 'Dashboard',
      redirect: false
    }
    this.checkLogin();
  }

  checkLogin = async () => {
    const result = await tool.checkLogin();
    if(!result){
      this.setState({
        redirect: '/login'
      })
    }
  }

  logOut = async (uri) => {
    const result = await tool.fetchAPI(uri);
    if(result.success){
      this.setState({
        redirect: '/'
      })
    }
  }

  render(){
    if(this.state.redirect) {
      return <Redirect to={this.state.redirect} />
    }
    return(
      <div className="DHeader">
        <section id="header">
          <div id="header-outer">
            <div className="header-inner region">
              <h1>{this.state.dTitle}</h1>
              <form id="search" action="/admin/search" method='post'>
                <select id="search-label" name="fsearch-label">
                  <option>Post</option>
                  <option>Page</option>
                  <option>Author</option>
                </select>
                <input type="text" name="querry" placeholder="Search" />
                <input type="submit" />
              </form>
              <div id="logout"><a href onClick={()=>this.logOut('/admin/logout')}>Logout</a></div>
            </div>
          </div>
        </section>
      </div>
    );
  }
}

export default DHeader;
// 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);
});

router.get('/logged', function(req, res, next){
  const login = require('../controllers/login');
  login.checkLogin(req, res);
});

router.get('/logout', function(req, res, next){
  const login = require('../controllers/login');
  login.logOut(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);
      }
    });
  }

  checkLogin(req, res){
    if(req.session.user){
      res.json({logged: true});
    }else{
      res.json({logged: false});
    }
  }

  logOut(req, res){
    if(req.session.user){
      req.session.destroy(function (err) {
        console.log(err);
        res.json({success: false});
      });  
    }
    res.json({success: true});
  }

}//end class

module.exports = new Login();

GitHub: "https://github.com/Sokhavuth/emultimedia
Heroku: https://khmerweb-emultimedia.herokuapp.com/

Comments

Popular posts from this blog