0

I'm trying to send a JSON object with a response in a 403 event, that contains token expiration info, amongst others, to the client. However, for some reason the response is sent as a string object instead of JSON.

The response looks like this:

HTTP Response

Here are the relevant functions.

This is from the server:

function checkYourPrivilege(checkAdmin, bearerHeader, res, reg, next){
        if (typeof bearerHeader !== 'undefined') {
                var bearer = bearerHeader.split(" ");
                bearerToken = bearer[1];
                jwt.verify(bearerToken, secret, function(err, decoded) {
                        console.log(decoded);
                        if(err){
                                return res.json({ success:false, message: 'Failed to authenticate token'});
                        }else{
                                if(Math.floor(Date.now() / 1000 < decoded.expires)){
                                        if(checkAdmin){
                                                if(decoded.privileges.admin){
                                                        console.log("Tokenisi vanhentuu" + Math.abs(decoded.expires - Math.floo$
                                                        reg.decoded = decoded;
                                                        next();
                                                }else{
                                                        console.log("et ole admin");
                                                        return res.status(403).send({
                                                          success: false,
                                                          tokenstatus: "valid",
                                                          message: "Not admin"
                                                        });
                                                }
                                        }else{
                                                console.log("Tokenisi vanhentuu" + Math.abs(decoded.expires - Math.floor(Date.n$
                                                reg.decoded = decoded;
                                                next();
                                        }
                                }else{
                                        console.log("Token Expired");
                                        return res.status(403).send({
                                                  success: false,
                                                  tokenstatus: "valid",
                                                  message: "Not admin"
                                        });
                                }
                        }
                });
        } else {
                console.log('ei tokenia');
                return res.status(403).send({
                        success: false,
                        message: 'No token provided.'
                });
        }
};

This is from the client:

.factory('authInterceptorService', ['$q', '$location', '$localStorage', function($q, $location, $localStorage){
        return {
                'request': function (config){
                        console.log(config);
                        config.headers = config.headers || {};
                        if ($localStorage.accessToken) {
                                config.headers.Authorization = 'bearer ' + $localStorage.accessToken;
                        }
                        return config;
                },
                'responseError': function(response){
                        if(response.status === 401 || response.status === 403) {
                                console.log(response);
                                $location.path('/');
                        }
                        return $q.reject(response);
                }
        };
}])
4
  • 2
    JSON is a string. Commented Dec 28, 2017 at 10:49
  • 1
    JSON is always sent as a string between machines. Just use JSON.parse() to turn it back into a object on the client side. For the record, there's no such thing as a JSON object. JSON is a notation standard, not a object type. So standard javascript objects can be written as a string formatted as JSON. Commented Dec 28, 2017 at 10:51
  • Holy moly I feel stupid now. I'll appologize for the whole world before crawling back to my cave -.- Commented Dec 28, 2017 at 10:57
  • @nyoatype You should still do what Vitamine is suggesting. If you use res.json it will send the data with the correct content-type and AngularJS will parse it for you automatically. No reason to use JSON.parse(). Commented Dec 28, 2017 at 11:14

1 Answer 1

2

Try use res.status(403).json({}) instead of res.status(403).send({})

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.