K
Kinde4w ago
readme

Next.js Refresh Token null

Hi! In my Next.js application i'm trying to use the refresh token method from getKindeServerSession() in my route handler. I have the kinde debug mode set to true in my env variables. The problem is that i receive a null value upon logging the value of the refresh token. And I also get an error message that says:
Error: The request is missing a required parameter, includes an invalid parameter value, includes a parameter more than once, or is otherwise malformed. Refresh token provided is malformed.
at AuthorizationCode.eval (webpack-internal:///(rsc)/./node_modules/@kinde-oss/kinde-typescript-sdk/dist/sdk/oauth2-flows/AuthCodeAbstract.js:325:31)
at step (webpack-internal:///(rsc)/./node_modules/@kinde-oss/kinde-typescript-sdk/dist/sdk/oauth2-flows/AuthCodeAbstract.js:53:23)
at Object.eval [as next] (webpack-internal:///(rsc)/./node_modules/@kinde-oss/kinde-typescript-sdk/dist/sdk/oauth2-flows/AuthCodeAbstract.js:34:53)
at fulfilled (webpack-internal:///(rsc)/./node_modules/@kinde-oss/kinde-typescript-sdk/dist/sdk/oauth2-flows/AuthCodeAbstract.js:25:58)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
Error: The request is missing a required parameter, includes an invalid parameter value, includes a parameter more than once, or is otherwise malformed. Refresh token provided is malformed.
at AuthorizationCode.eval (webpack-internal:///(rsc)/./node_modules/@kinde-oss/kinde-typescript-sdk/dist/sdk/oauth2-flows/AuthCodeAbstract.js:325:31)
at step (webpack-internal:///(rsc)/./node_modules/@kinde-oss/kinde-typescript-sdk/dist/sdk/oauth2-flows/AuthCodeAbstract.js:53:23)
at Object.eval [as next] (webpack-internal:///(rsc)/./node_modules/@kinde-oss/kinde-typescript-sdk/dist/sdk/oauth2-flows/AuthCodeAbstract.js:34:53)
at fulfilled (webpack-internal:///(rsc)/./node_modules/@kinde-oss/kinde-typescript-sdk/dist/sdk/oauth2-flows/AuthCodeAbstract.js:25:58)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
5 Replies
readme
readmeOP4w ago
i am also attaching my code for reference
import { db } from "@/config/firebase";
import { decodeToken } from "@/helpers/decodeToken";
import { NextResponse } from "next/server";
import { getKindeServerSession } from "@kinde-oss/kinde-auth-nextjs/server";

export const GET = async (req) => {
try {

const {refreshTokens} = getKindeServerSession();

const tokens = await refreshTokens(req);
console.log(tokens)

const { sub: user, error } = await decodeToken(req);

if (error) {
return NextResponse.json({ error: "Not a valid token" }, { status: 401 });
}

const { searchParams } = new URL(req.url);
const pageSize = parseInt(searchParams.get("pageSize") || "5", 10);

let query = db
.collection("receipts")
.where("user", "==", user)
.limit(pageSize);

const receipts = await query.get();

if (receipts.empty) {
return NextResponse.json({ data: [], message: "No more data." });
}

const data = receipts.docs.map((doc) => ({
id: doc.id,
...doc.data(),
}));

return NextResponse.json({
data,
message: "Data fetched successfully.",
});
} catch (error) {
console.error("Error fetching receipts:", error);
return NextResponse.json(
{ error: "Failed to fetch receipts." },
{ status: 500 }
);
}
};
import { db } from "@/config/firebase";
import { decodeToken } from "@/helpers/decodeToken";
import { NextResponse } from "next/server";
import { getKindeServerSession } from "@kinde-oss/kinde-auth-nextjs/server";

export const GET = async (req) => {
try {

const {refreshTokens} = getKindeServerSession();

const tokens = await refreshTokens(req);
console.log(tokens)

const { sub: user, error } = await decodeToken(req);

if (error) {
return NextResponse.json({ error: "Not a valid token" }, { status: 401 });
}

const { searchParams } = new URL(req.url);
const pageSize = parseInt(searchParams.get("pageSize") || "5", 10);

let query = db
.collection("receipts")
.where("user", "==", user)
.limit(pageSize);

const receipts = await query.get();

if (receipts.empty) {
return NextResponse.json({ data: [], message: "No more data." });
}

const data = receipts.docs.map((doc) => ({
id: doc.id,
...doc.data(),
}));

return NextResponse.json({
data,
message: "Data fetched successfully.",
});
} catch (error) {
console.error("Error fetching receipts:", error);
return NextResponse.json(
{ error: "Failed to fetch receipts." },
{ status: 500 }
);
}
};
agr
agr4w ago
I'm having the same issue!
Claire_Kinde
Claire_Kinde4w ago
Hi there. Just referring this to a NextJS expert in our team. Thanks for your patience. A few triage questions: What version of NextJS and the SDK are you using? Has this stopped working? Or are you setting up for first time? Can you share your backend/Frontend/Middleware setup?
Daniel_Kinde
Daniel_Kinde4w ago
Hi @readme and @agr We have recently updated our guidance for tokens to be automatically refreshed.
https://docs.kinde.com/developer-tools/sdks/backend/nextjs-sdk/#set-up-the-kinde-auth-provider Is your application wrapped in the AuthProvider? if not could you adjust and try again please?
Kinde docs
Next.js App Router SDK
Our developer tools provide everything you need to get started with Kinde.
readme
readmeOP3w ago
@Daniel_Kinde sorry for the late reply. I wrapped the application and now i can see the refresh tokens. But when should i use it. I know that they must be used when the previous token gets expired but where in the application should one use it. Thank you.
Want results from more Discord servers?
Add your server