LaVOZs

The World’s Largest Online Community for Developers

'; javascript - Calling external api endpoints when working with node and ejs and geting the details for your users - LavOzs.Com

i have a signup and login form,am using ejs to render as view, i have been given some api endpoint to call, and i have added them to their ejs form action views when i fill in the user details in my ejs view form i get this response.

i get this when am trying to register a user

    {
      "status": 200,
      "message": "Account created successfully.",
      "data": {
        "name": "name of user",
        "email": "user email",
        "password": "$2b$10$0IPgBNCJhjjkasdkGb2y0CviOuxRe/bAfhFqFDqrofMaCz5f0SMtwkgBxIzm"
      }
    }

i get this when am trying to login a registered user

{
  "status": 200,
  "message": "Account logged in successfully.",
  "data": [
    {
      "id": 9,
      "name": "username",
      "email": "useremail@gmail.com",
      "password": "$2b$10$v3/zhn0pP6BMaTlfcoaMUuMlaHPGht6v2sf03e6uB3OPIQ3ggdpdi",
      "date_created": "2020-02-21T13:15:33.000Z"
    }
  ]
}

i get this when i post the sign in form with the details of an already registered user

    {
      "status": 400,
      "message": "Provided email address already exists, try another",
      "data": null
    }

i get this when am trying to login with bad credentials

    {
      "status": 400,
      "message": "Incorrect password provided, try again.",
      "data": null
    }

please my question is how do i have access to this details so i can send them to the client in my ejs view

you can use the npm package request, so you install it with this npm i request now you will need one function that you will always call whenever you want to use the api endpoint


    var request = require("request");

    module.exports = {

        // DATA IN { name: 'aaa', email: 'pass@gmail.com', password: '123' } FORMAT
        callAPI:function(url, data, callback){

            var options = { method: 'POST',
            url: url,
            headers: 
            {
                'cache-control': 'no-cache',
                'content-type': 'application/json'
            },
            body: data,
            json: true };

            request(options, function (error, response, result) {
            if (error){
                return callback(error);
            }else{
                callback(null, result);  
            };

            });

        }

    }

and you can use this as an example to call the function


    app.post('/signup', function (req, res) {
            var data = {
                "name": req.body.name,
                "email": req.body.email,
                "password": req.body.password
            }

            functions.callAPI("https://api/endpoin/signup", data, function (error, result) {
                if (error) {
                    var response = {
                        "status": 400,
                        "message": error,
                        "data": null
                    }
                    res.render('signup', {response: response});
                } else {
                    var response = result;
                    if (response.status === 400) {
                        res.render('signup', {response: response});
                    } else {
                        res.redirect('/login');
                    }
                }
            });


        });

Related
User Login is not working with node.js and mongoose
Req.flash(errors) Not Displaying -NodeJS 5.7.0
API: Loading user details using J Query and AJAX without refreshing
Nothing returned when deserializing user in node and passport
Receiving NULL on response error body if the status code is 400 with retrofit in android
user login fails even if correct details are provided