K
Kinde7mo ago
Andersson

Getting updated user data from NextJS App Router Middleware.

How can i get updated user data from middleware. i tried refreshing the token before getting user data with
await refreshTokens();
await refreshTokens();
const user = await getUser();
const user = await getUser();
. My issue is that i tried deleting the user from the kinde ui and refreshed my nextjs app to see if i would get redirected to "/" because there is no user, but the user was still autheticated.
2 Replies
Andersson
AnderssonOP7mo ago
Forget it i moved to lucia auth
onderay
onderay7mo ago
Sorry to hear that you wanted to move quickly. For anyone else coming across this issue, here some tips to help. To get updated user data from middleware in your Next.js app, you need to ensure that the tokens are refreshed and the session is updated correctly. Here is a step-by-step guide to achieve this: Refresh Tokens: Use the refreshTokens helper function to refresh the tokens in your session. Get Updated User Data: After refreshing the tokens, use the getUser function to get the updated user data. Here is an example of how you can implement this in your middleware:
import { withAuth } from "@kinde-oss/kinde-auth-nextjs/middleware";
import { getKindeServerSession } from "@kinde-oss/kinde-auth-nextjs/server";

export default withAuth(
async function middleware(req) {
const { refreshTokens, getUser } = getKindeServerSession();

// Refresh tokens
await refreshTokens();

// Get updated user data
const user = await getUser();

if (!user || user == null || !user.id) {
// Redirect to home if user is not authenticated
return new Response("Unauthorized", { status: 401 });
}

console.log("Updated user data:", user);
},
{
isReturnToCurrentPage: true,
loginPage: "/login",
isAuthorized: ({ token }) => {
return token.permissions.includes("eat:chips");
},
}
);

export const config = {
matcher: ["/admin"],
};
import { withAuth } from "@kinde-oss/kinde-auth-nextjs/middleware";
import { getKindeServerSession } from "@kinde-oss/kinde-auth-nextjs/server";

export default withAuth(
async function middleware(req) {
const { refreshTokens, getUser } = getKindeServerSession();

// Refresh tokens
await refreshTokens();

// Get updated user data
const user = await getUser();

if (!user || user == null || !user.id) {
// Redirect to home if user is not authenticated
return new Response("Unauthorized", { status: 401 });
}

console.log("Updated user data:", user);
},
{
isReturnToCurrentPage: true,
loginPage: "/login",
isAuthorized: ({ token }) => {
return token.permissions.includes("eat:chips");
},
}
);

export const config = {
matcher: ["/admin"],
};
In this example: The refreshTokens function is called to refresh the tokens. The getUser function is then called to get the updated user data. If the user data is not available or the user is not authenticated, a 401 Unauthorized response is returned, which can be used to redirect the user to the home page or login page. Make sure you have enabled the application's access to the Kinde Management API in your Kinde settings. For more details, you can refer to the NextJS App Router SDK documentation.
Want results from more Discord servers?
Add your server