79231131

Date: 2024-11-27 16:44:00
Score: 1.5
Natty:
Report link

@noassl was correct with their comment. The real issue I was running into was with my incorrect assignment of the token. The authorization token was initially assigned undefined but I am not sure why I was unable to change it. I am assuming this has something to do with the way Axios instances function. To correct this issue, I moved the assignment of the instance inside the login function.

const fastify = require('fastify')({ logger: true });
const axios = require('axios');
const fs = require('fs');
require('dotenv').config();

const username = process.env.USERNAME
const password = process.env.PASSWORD
let token
let instance

async function login() {
    const response = await axios.post('/login', {username, password})
    token = response.data['token'];
    instance = axios.create({
        baseURL: process.env.URL,
        headers : {
            'Authorization': `Bearer ${token}`
        }
    })
    console.log(token);
}

async function me() {
    const response = await instance.get('/me')
    console.log(response.data);
}

login().then(me())
Reasons:
  • Long answer (-1):
  • Has code block (-0.5):
  • User mentioned (1): @noassl
  • Self-answer (0.5):
  • Looks like a comment (1):
  • Low reputation (0.5):
Posted by: Devin Bowen