error

mail@pastecode.io avatar
unknown
javascript
2 years ago
3.6 kB
3
Indexable
Never
const express = require('express');
const { appendFile } = require('fs');
const mongoose = require('mongoose');
const users = require('./schemas/users.js');
const jwt = require('jsonwebtoken');
const { nextTick } = require('process');
const MongoClient = require('mongodb').MongoClient;


const app = express();
app.use(express.json());
app.use(express.urlencoded());


const dbURI = "mongodb://localhost:27017/rdp"

mongoose.connect(dbURI,{

},(err) =>{
  if(err){
    console.log("we can't connect")
  }else{
    console.log("start listening")
    app.listen(5000)
  }
});






function accesstoken(user){
  signing = jwt.sign({username : user},'crypto',{expiresIn:"2h"});
  return signing;
}


function verifytoken(token,key){
  var test = 'test'
  jwt.verify(token,key,(err,user)=>{
    if(err){
      console.log('we have an error')
    }else{
      console.log('its okay')
      test = 'untest'

    }
  })
  console.log(test)
}

//console.log(accesstoken('omar'))

verifytoken(accesstoken('omar'),'crypto')


//find_username('omarmax')


//Add new user
app.post('/signup',(req,res) => {
  console.log(req.body);
  const data = req.body;
  const url = 'mongodb://localhost:27017/'
  MongoClient.connect(url,(err,db) => {
    //checking USERname and mail
    if(err){
      console.log("not connected")
    }else{
      const dbo = db.db('rdp')
      dbo.collection("users").findOne({username:req.body.username},(err,result) =>{
        if(err || result == null){
          //checking email here
          dbo.collection("users").findOne({email:req.body.email},(err,result)=>{
            if(err || result == null){
              //save the new user
              const new_user = new users(req.body)
              new_user.save((error)=>{
                if(error){
                  res.json({msg:'Failed to create new user'})
                }else{
                  res.json({msg:"Succes! We created new user"})
                  
                }
              })
            }else{
              res.json({msg:"Already Registered"})
            }
          })
        }else{
          res.json({msg:"Already Registered"})
        }
      })

    }
    
  })

  //const new_user = new users(data)
  //new_user.save((error)=>{
   // if(error){
    //  res.json({msg:"Failed"})
   // }else{
      //res.json({msg:"Success"})
   // }
  //})



})






app.post('/login',(req,res) => {
  //console.log(req.body)
  const url = "mongodb://localhost:27017"
  //req.user = { username: 'omarmax', iat: 1651388758, exp: 1651395958 }
  //console.log(req.headers)

  const user = req.body.username;


  ///Checking the user if exist
  MongoClient.connect(url,(err,db) => {
    if(err){
      console.log("we couldn't connect")
    }else{
      const dbo = db.db("rdp")
      dbo.collection("users").findOne({username:req.body.username},(err,result) => {
        if(err || result == null){
          res.json({msg:"this user is not registered yet"})
        }else{
          if(result.password == req.body.password){
            //STart session
            res.json({msg:"Success logged in"})
            req.headers["authorization"] = accesstoken(user)
            //hna fin endi mochkil mkn9derch nchanger value d chi variable
            
          }else{
            res.json({msg:"Wrong password"})
          }
          
        }
        
      })
    }
    console.log(req.headers)
  })


})