Welcome to the Kinde community

Updated 5 hours ago

What is the proper way to test if user is authenticated?

I am using sequence below to test if a user is authenticated or not. However, this causes an async call to kinde as I understand for each isAuthenticated() for ex.

Is there a better way to handle this and obtain the authentication status?
const { getUser, isAuthenticated } = getKindeServerSession();
const authenticated = (await isAuthenticated());
const newuser = await getUser();
if (authenticated) {
const newuser = await getUser();
A
b
2 comments
Hi @bazar21 ,

Thank you for your question. To determine if a user is authenticated without making multiple asynchronous calls, you can streamline your code as follows:

const { isAuthenticated, getUser } = getKindeServerSession();

if (await isAuthenticated()) {
const user = await getUser();
// Proceed with authenticated user logic
} else {
// Handle unauthenticated user scenario
}

This approach ensures that you only call getUser() when the user is authenticated, reducing unnecessary asynchronous operations.

For more details, you can refer to the Kinde TypeScript SDK documentation: https://docs.kinde.com/developer-tools/sdks/backend/typescript-sdk

If you have further questions or need additional assistance, feel free to ask
Add a reply
Sign up and join the conversation on Discord