async function addAuthHeader()

in web/js/rest-api.js [42:80]


async function addAuthHeader(options) {

    let jwt
    if (window.location.hostname === "localhost") {
        jwt = LOCAL_JWT
        // jwt.js needs a JWT token and we need to be on localhost
        Cookies.set("jwt.id", jwt)
    } 

    jwt = Cookies.get("jwt.id")
    if (!jwt) {
        // User is not logged in
        console.log("Not logged in")

        // Redirect to Cognito
        window.location = LOGIN_URL
        return
    }

    // Refresh the token if it is expired
    const expiration = Cookies.get("jwt.expires")
    if (expiration) {
        const expires = new Date(expiration)
        if (expires < new Date()) {
            const refresh = Cookies.get("jwt.refresh")

            console.log("Refreshing jwt token: " + refresh)

            // Refresh the token
            const data = await get(`jwt-get?refresh=${refresh}`, null, false)
            console.log("jwt-get refresh response: " + JSON.stringify(data, null, 0))

            setAuthCookies(data)
            jwt = Cookies.get("jwt.id")
        }
    }

    options.headers.Authorization = "Bearer " + jwt
}