User login using NodeJS

Objective: In this article, you will know mongoose model, how to create user using node, router.

Pre-requisite Prior to completing this article, you should have already installed all pre-requisite tooling including: Visual Studio Code, Node Package Ma…


This content originally appeared on DEV Community and was authored by Bipon Biswas

Objective: In this article, you will know mongoose model, how to create user using node, router.

Pre-requisite Prior to completing this article, you should have already installed all pre-requisite tooling including: Visual Studio Code, Node Package Manager (NPM), Node, Postman, Mongo Compass.

Create a Model (ProfileModel.js)

const mongoose = require('mongoose')

const DataSchema = mongoose.Schema({
    FirstName : {type: String},
    LastName : {type: String},
    EmailAddress : {type: String},
    MobileNumber : {type: String},
    City : {type: String},
    UserName : {type: String},
    Password : {type: String}
});

const ProfileModel = mongoose.model('Profile', DataSchema)
module.exports = ProfileModel;

Create a Controller (ProfileController.js)

At first import ProfileModel. Declare a variable reqBody to store body data. Then create user using ProfileModel model

const ProfileModel = require("../models/ProfileModel");


exports.CreateProfile = (req, res) => {

    let reqBody = req.body;
    ProfileModel.create(reqBody, (err, data) => {
        if(err){
            res.status(400).json({status: "Failed to user create", data: err})
        }else{
            res.status(200).json({status: "Successfully user created", data: data})
        }
    })
}

exports.UserLogin = (req, res) => {

    let UserName = req.body['UserName'];
    let Password = req.body['Password'];
    // res.status(200).json({status: "Success", data: Password})

    ProfileModel.find({UserName, Password}, (err, data) => {
       if(err){
        res.status(400).json({status: "Failed to login", data: err})
       }else{
        if(data.length > 0){
            res.status(200).json({status: "Successfully Login", data: data})
        }else{
            res.status(401).json({status: "Unauthorized"})
        }
       }
    })
}

The find() function is used to find particular data from the MongoDB database

Default configuration (app.js)

// Basic import
const express = require('express');
const router = require('./src/routes/api')
const app = new express();
const bodyParser = require('body-parser')

// Database lib import
const mongoose = require('mongoose')

// Body parser implement
app.use(bodyParser.json())

// MongoDB database connection
let uri = 'mongodb://127.0.0.1:27017/PracticeDB'
let options = {user: '', pass: ''}
mongoose.connect(uri, options, (err) => {
    if(err){
        console.log(err)
    }else{
        console.log('Database Connection Success')
    }
})

// Routing Implement
app.use('/api/v1', router)

// Undefined Route Implement
app.use("*", (req, res) => {
    res.status(404).json({status: "Failed", data: "Not Found"})
})

module.exports = app;

Routes configuration (api.js)

const express = require('express');
const ProfileController = require('../controller/ProfileController')
const router = express.Router();

router.post('/CreateProfile', ProfileController.CreateProfile)
router.post('/UserLogin', ProfileController.UserLogin)

module.exports = router;

Index file (index.js)

const app = require('./app')

app.listen(5000, function(){
    console.log('Server run at @5000 port')
})

Now open the Postman and configure few thing like
Image description

Then login a user giving basic information and click Send Button
Image description
If giving wrong information then showing Unauthorized
Image description

Thanks for reading. Happy journey.

Reference

User create using NodeJS
Mongoose find() Function


This content originally appeared on DEV Community and was authored by Bipon Biswas


Print Share Comment Cite Upload Translate Updates
APA

Bipon Biswas | Sciencx (2022-04-14T06:31:53+00:00) User login using NodeJS. Retrieved from https://www.scien.cx/2022/04/14/user-login-using-nodejs/

MLA
" » User login using NodeJS." Bipon Biswas | Sciencx - Thursday April 14, 2022, https://www.scien.cx/2022/04/14/user-login-using-nodejs/
HARVARD
Bipon Biswas | Sciencx Thursday April 14, 2022 » User login using NodeJS., viewed ,<https://www.scien.cx/2022/04/14/user-login-using-nodejs/>
VANCOUVER
Bipon Biswas | Sciencx - » User login using NodeJS. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/04/14/user-login-using-nodejs/
CHICAGO
" » User login using NodeJS." Bipon Biswas | Sciencx - Accessed . https://www.scien.cx/2022/04/14/user-login-using-nodejs/
IEEE
" » User login using NodeJS." Bipon Biswas | Sciencx [Online]. Available: https://www.scien.cx/2022/04/14/user-login-using-nodejs/. [Accessed: ]
rf:citation
» User login using NodeJS | Bipon Biswas | Sciencx | https://www.scien.cx/2022/04/14/user-login-using-nodejs/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.