I'm trying to fetch data from the Middleware API and am using A secret key to Secure the API.
But when I try to fetch the data it just doesn't recognize the Secret params,
How can I make it fetch the data and make my secret key Secret.
.env.local
LOCAL_API_KEY=5b45f6898634abf9c3769b763c5fa8ddfc57d2cc943a0601f7bda4269ebe7342
updateUser.js
const {data: session} = useSession()
useEffect(() => {
if(session){
const checkUserInfo = async() => {
try{
const fetchUserInfo = await axios.get(`/api/users/${session.user?._id}`,{
params: {
API_ROUTE_SECRET: process.env.LOCAL_API_KEY
}
})
console.log(fetchUserInfo) // Output: Code 401
delete fetchUserInfo.data.user.password
delete fetchUserInfo.data.user.admin
fetchUserInfo.data.user && localStorage.setItem('userSession', JSON.stringify(fetchUserInfo.data.user))
}catch(err){
console.log(err)
}
}
checkUserInfo()
}else{
localStorage.removeItem('userSession')
}
}, [session])
[id].js
import db from "@/database/connect"
import Users from "@/database/modals/users"
export default async function handler(req, res){
const {id, API_ROUTE_SECRET} = req.query
console.log('key', API_ROUTE_SECRET) // Output: undefiend
if(API_ROUTE_SECRET !== process.env.LOCAL_API_KEY){
res.status(401).json({error: 'Unauthorized User..'})
}
if(req.method === 'GET'){
try{
await db.connect()
const userInfo = await Users.findById(id)
await db.disconnect()
if(userInfo){
res.status(201).json({user: userInfo})
}else{
res.status(404).json({error: 'User not found'})
}
}catch(err){
res.status(404).json({error: 'No Data For This User'})
}
}else{
res.status(500).json({error: 'ONLY GET REQUESTS ARE ALLOWED'})
}
}