Code Monkey home page Code Monkey logo

chat-backend's Introduction

require("dotenv").config(); const express = require("express"); const bodyParser = require("body-parser"); const mongoose = require("mongoose"); const passport = require("passport"); const LocalStrategy = require("passport-local").Strategy;

const app = express(); const port = 8080; const cors = require("cors"); app.use(cors());

app.use(bodyParser.urlencoded({ extends: false })); app.use(bodyParser.json()); app.use(passport.initialize()); const jwt = require("jsonwebtoken");

mongoose .connect( "mongodb+srv://sengar:[email protected]/test?retryWrites=true&w=majority&appName=Cluster0" // {useNewUrIParser: true, useUnifiedTop010gy: true, useCreateIndex: true}, ) .then(() => { console.log("connected to Mongoose"); }) .catch((err) => { console.log("err in mongodb", err); });

app.listen(port, () => { app.listen(process.env.PORT || 3000, () => { console.log("server listening on port", port); }); });

const User = require("./models/users"); // const Message = require('./models/message');

// end points for ragistration of the user

const createToken = (userId) => { const payload = { userId: userId, }; const token = jwt.sign(payload, "Q$r2K6W8n!jCW%Zk", { expireIn: "1h" }); return token; };

app.post("/register", async (req, res) => { const { name, email, password, image } = req.body;

// create new user const newUser = new User({ name, email, password, image, });

// save the user to the database

await newUser .save() .then(() => { res.status(200).json({ massage: "user registered ssdfuccessfully", user: { name, email, password, image }, }); }) .catch((err) => { console.log("err in ragister user", err); res.status(500).json({ massage: "error in ragister user" }); }); });

app.post("/login", (req, res) => { const { email, password } = req.body;

if (!email || !password) { return res.status(404).json({ message: "email and password are required" }); }

User.findOne({ email }) .then((user) => { if (!user) { // if user not found return res.status(404).json({ message: "user not found" }); }

  // compare
  if (user.password !== password) {
    return res.status(404).json({ message: "invalid password" });
  }
  res.status(200).json({message: 'user found', user});
  // const token = createToken(user._id);
  // res.status(200).json({token: token});
})
.catch((err) => {
  console.log("err in finding user", err);
  res.status(500).json({ message: "internal server error!" });
});

});

app.get("/data", (req, res) => {});

chat-backend's People

Contributors

pranjalsinghsengar avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.