my access_token
from const access_token = await getToken({ req, secret })
always returns undefined
inside the next-js
API Middleware
which is called by await axios.get()
inside getServerSideProps()
, is this normal?
I can get the accessToken
inside getServerSideProps()
and pass it as parameter over to the API Middleware
page but not getting the accessToken
directly inside the API Middleware
page
pages/user.js
page:
function User({user_data, request_params}) {
....
}
export default User;
export async function getServerSideProps( context ) {
const user_data = await fetchUserData( context.query );
return {
props: {
user_data: user_data,
request_params: context.query
}
}
}
async function fetchUserData( params ) {
// Create AJAX URL
const ajax_url = 'http://localhost:3000/api/user/1/info';
let result;
await axios.get(ajax_url)
.then(response => {
result = response.data;
}).catch((error) => {
// Error
if (error.response) {
// The request was made and the server responded with a status code
// that falls out of the range of 2xx
console.log('Error.response > Data: ', error.response.data);
console.log('Error.response > Status: ', error.response.status);
console.log('Error.response > Headers: ', error.response.headers);
} else if (error.request) {
// The request was made but no response was received
// `error.request` is an instance of XMLHttpRequest in the
// browser and an instance of
// http.ClientRequest in node.js
console.log('Error.request: ', error.request);
} else {
// Something happened in setting up the request that triggered an Error
console.log('Error: ', error.message);
}
console.log('Unknown Error: ', error.config);
});
return result;
}
api/user/[user]/info.js
page:
import axios from "axios";
import { getToken } from "next-auth/jwt";
const secret = process.env.SECRET
export default async (req, res) => {
// Get Access Token [ undefined ]
const access_token = await getToken({
req: req,
secret: secret,
})
console.log('access token?: ' + access_token.accessToken); // undefined
// Config
const config = {
headers: {
'authorization': `Bearer ${access_token.accessToken}`,
'Cache-Control': 'no-cache',
'Pragma': 'no-cache',
'Expires': '0',
}
};
const ajax_url = 'http://localhost/actualwebsite/public/api/v1/user/' + req.query.user + '/' + 'info';
await axios.get(ajax_url, config)
.then(response => {
res.setHeader('Cache-Control', 'no-cache');
res.setHeader('Expires', '0');
res.status(200).json(response.data);
}).catch((error) => {
// Error
if (error.response) {
// The request was made and the server responded with a status code
// that falls out of the range of 2xx
console.log('Api > Error.response > Data: ', error.response.data);
console.log('Api > Error.response > Status: ', error.response.status);
console.log('Api > Error.response > Headers: ', error.response.headers);
} else if (error.request) {
// The request was made but no response was received
// `error.request` is an instance of XMLHttpRequest in the
// browser and an instance of
// http.ClientRequest in node.js
console.log('Api > Error.request: ', error.request);
} else {
// Something happened in setting up the request that triggered an Error
console.log('Api > Error: ', error.message);
}
console.log('Api > Unknown Error: ', error.config);
});
}