Why am I getting a 431 status error when passing in a JWT token in Authorization section of the header?

Viewed 290

So I am creating a full stack React app with Express and Node. I have implemented auth0 for authentication and authorization but I am getting the following error, does anyone have any suggestions on how to solve it? Thanks!

"Failed to load resource: the server responded with a status of 431 (Request Header Fields Too Large)"

The following is the class component where I am calling my api/tasks route and passing in the jwt access token from Auth0.

import React, { Component } from 'react';
import axios from "axios";
import {withAuth0} from "@auth0/auth0-react";
import config from "../auth_config.json";
const {apiOrigin = "http://localhost:3001"} = config

componentDidMount() {
        const getProtectedTasks = async () => {
            const {getAccessTokenSilently} = this.props.auth0;
            const accessToken = await getAccessTokenSilently();
            try {
                const token = accessToken;
                const self = this;
                const response = await axios.get(`${apiOrigin}/api/tasks`, {
                    headers: {
                        Authorization: `Bearer ${token}`
                    }
                })
                self.setState({
                    allTasks: response.data
                })

            } catch {

            }
        }
        getProtectedTasks()

    }

The following is my code that handles the get api/tasks endpoint

const db = require("../models");
const jwt = require("express-jwt");
const jwksRsa = require("jwks-rsa");

module.exports = function (router) {
    const authorizeAccessToken = jwt({
        secret: jwksRsa.expressJwtSecret({
            cache:true,
            rateLimit:true,
            jwksRequestsPerMinute:5,
            jwksUri: `https://task-motivator.us.auth0.com/.well-known/jwks.json`
        }),
        audience: "https://quickstarts/api",
        issuer: "https://task-motivator.us.auth0.com",
        algorithms: ["RS256"]
    });

    router.get("/api/tasks", authorizeAccessToken, (req, res) => {
        db.Task.findAll({}).then(data => {
            res.json(data);
        });
    });
0 Answers
Related