R
Reactiflux

✅ – Adi – 11-00 Jan 23

✅ – Adi – 11-00 Jan 23

AAdi1/23/2023
[How to secure API routes in Nextjs] I am using this code snippet:
import nocountries from '../data/nocountries.json';
import countryList from "country-list";
import { getSession } from "next-auth";

export default async (req, res) => {
const session = await getSession({ req });
if (!session) {
return res.status(401).send({ Error: 'Unauthenticated User' });
}

if (req.method === 'GET') {
try {
const { country, filter } = req.query;
const code = countryList.getCode(country);

const result = nocountries[code].filter(item => item.target_type === filter);

res.status(200).send(result);
} catch (error) {
console.error("Error: ", error)
res.status(500).send({ message: "Failed" });
}
} else {
res.status(500).send({ message: "Internal Server Error" });
}
}
import nocountries from '../data/nocountries.json';
import countryList from "country-list";
import { getSession } from "next-auth";

export default async (req, res) => {
const session = await getSession({ req });
if (!session) {
return res.status(401).send({ Error: 'Unauthenticated User' });
}

if (req.method === 'GET') {
try {
const { country, filter } = req.query;
const code = countryList.getCode(country);

const result = nocountries[code].filter(item => item.target_type === filter);

res.status(200).send(result);
} catch (error) {
console.error("Error: ", error)
res.status(500).send({ message: "Failed" });
}
} else {
res.status(500).send({ message: "Internal Server Error" });
}
}
I am getting error: getSession) is not a function I used import { getSession } from "next-auth/client"; still it did not work. Any idea what I am missing?
Solution:
you need to use getserversession
Jump to solution
DDawid1/23/2023
get session is for client side
Solution
DDawid1/23/2023
you need to use getserversession
UUUnknown User1/24/2023
3 Messages Not Public
Sign In & Join Server To View

Looking for more? Join the community!

R
Reactiflux

✅ – Adi – 11-00 Jan 23

Join Server