I created a login function that invokes a backend with params email and password, to receive a JWT. I have tried save the jwt value in a variable for use in the next request.
This is the method I developed, it has a post request that sends the mail and pass parameters.
async function main() {
const data = await login("mail", "pass")
console.log(data);
return data
}
async function login(mail, pass) {
const url = "https://inventario/api/login";
var data = JSON.stringify({ "email": mail, "password": pass });
var requestOptions = {
method: 'POST',
body: data,
headers: {
"Content-type": "application/json; charset=UTF-8"
}
}
const response = await fetch(url, requestOptions)
const json = await response.json()
console.log(json)
return json;
}
I call the function this way
var tokentest = require("./Tokentest.js");
var key = tokentest.main();
console.log(key)
There are multiple ways to do this depending on your final use case, but I would recommend starting by just saving it to a json
file.
const fs = require('fs').promises;
// Write token to file
await fs.writeFile(path.join(__dirname, './token.json'), JSON.stringify(key));
// Use it again by reading the file and passing it to a variable
const rawData = await fs.readFile(path.join(__dirname, './token.json'), 'utf8');
const token = JSON.parse(rawData);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.