Passport authentication on an external API server

I want to have a secure api server in the cloud. It contains all passport codes and routes for logging in, registering, logging out, etc. The problem is that I want to call this API an external client using an http client such as $ http or "request".

Are there any examples of who does passport authentication using external APIs? As for my case, when the only thing I presented is an HTTP client.

I tried setting it up locally using the Http client calling localhost, and that didn't work.

This is the login api that I would like to eventually move to the cloud and access using the http client over the Internet.

app.post('/api/login', passport.authenticate('local'), function (req, res) {
console.log("external Login API: " + req.user.email)
if(req.isAuthenticated()){
    console.log("In Login. Apparently authenticated")
}
res.send(JSON.stringify(req.user))
});

This is a local route calling a function that calls an external api.

app.post('/app/login', publicLoginApi.login);

, login api

exports.login = function(req,res){
    request({
        url: 'http://localhost:3000/api/login', //URL to hit
        qs: {from: 'Web App', time: +new Date()}, //Query string data
        method: 'POST',
        headers: {
            'Content-Type': 'application/json',
        },
        email: req.body.email,
        password: req.body.password,
        body: JSON.stringify({email: req.body.email, password: req.body.password})
    }, function(error, response, body){
        if(error) {
            console.log("login request failed");
        } else {
            var user = JSON.parse(response.body)
            if(req.isAuthenticated()){
                console.log("Authentication working");
            }
            console.log("login request success");
            console.log("Success user email: " + user.email)
            //console.log(body)
            //console.log(response.body.email)
            return res.send(200,user)
        }
    });
    }

, . /api/login HTML, . //, /api/login http, req.isAuthenticated /api/login /app/login. , isAuthenticated . . , (err, req, res, next) , , ?

, - api, http-.

+4

Source: https://habr.com/ru/post/1648327/


All Articles