├── .gitignore ├── package.json └── index.js /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "bcryptjs-testing", 3 | "version": "1.0.0", 4 | "description": "", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "author": "", 10 | "license": "MIT", 11 | "dependencies": { 12 | "bcryptjs": "^2.4.3", 13 | "express": "^4.17.3", 14 | "mongoose": "^6.2.9" 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | const express = require('express') 2 | const app = express() 3 | const port = 3000 4 | const mongoose = require('mongoose') 5 | mongoose.connect('mongodb://localhost:27017/test') 6 | const bcrypt = require('bcryptjs') 7 | const {Schema} = mongoose 8 | const User = mongoose.model('user',new Schema({ 9 | email: String, 10 | password: String 11 | })) 12 | 13 | app.use(express.json({limit:'10mb'})) 14 | app.use(express.urlencoded({limit:'10mb',extended:false})) 15 | 16 | app.post('/signup', async (req, res) => { 17 | try { 18 | const hashPassword = await bcrypt.hash(req.body.password, 10) 19 | req.body.password = hashPassword 20 | const user = await User.create(req.body) 21 | if (user) { 22 | return res.json({ 23 | success: true, 24 | message: user 25 | }) 26 | } 27 | return res.json({ 28 | success:false, 29 | message:'failed to add data' 30 | }) 31 | } catch (error) { 32 | return res.json(error) 33 | } 34 | }) 35 | 36 | app.post('/login', async (req, res) => { 37 | try { 38 | const {email} = req.body 39 | const user = await User.findOne({email}) 40 | if (!user) { 41 | return res.json({ 42 | success:false, 43 | message:'invalid email' 44 | }) 45 | } 46 | const match = await bcrypt.compare(req.body.password,user.password) 47 | if(match){ 48 | return res.json({ 49 | success: true, 50 | message: user, 51 | match:match 52 | }) 53 | } 54 | return res.json({success:false, message:'invalid password'}) 55 | 56 | } catch (error) { 57 | return res.json(error) 58 | } 59 | }) 60 | 61 | app.listen(port, () => { 62 | console.log('server successfully started !') 63 | }) --------------------------------------------------------------------------------