NextAuth.js Revalidation without logout
Unanswered
Barbary Lion posted this in #help-forum
Barbary LionOP
How do i revalidate my session within NextAuth.js without logging out?
Right now on my settings page i have a form to change the user's name. On submit this form runs an action to update the user row and updates the session on the client side:
actions.ts:
component.client.tsx(settings client page):
Right now on my settings page i have a form to change the user's name. On submit this form runs an action to update the user row and updates the session on the client side:
actions.ts:
'use server';
import { revalidatePath } from 'next/cache';
import { auth } from '@/auth';
import { db, user } from '@/db';
import { eq } from 'drizzle-orm';
import { UpdateProfileValues, updateProfileSchema } from '@/lib/validation';
export async function updateProfile(values: UpdateProfileValues) {
const session = await auth();
const id = session?.user?.id;
if (!id) {
throw Error('Unauthorized');
}
const { name } = updateProfileSchema.parse(values);
await db.update(user).set({ name }).where(eq(user.id, id));
revalidatePath('/');
}component.client.tsx(settings client page):
// ... more component imports
import { signOut, useSession } from 'next-auth/react';
import { updateProfile } from './actions';
import { useForm } from 'react-hook-form';
import { zodResolver } from '@hookform/resolvers/zod';
import { UpdateProfileValues, updateProfileSchema } from '@/lib/validation';
interface Props {
user: ExtendedUser;
}
export default function SettingsPage({ user }: Props) {
const session = useSession();
const form = useForm<UpdateProfileValues>({
resolver: zodResolver(updateProfileSchema),
defaultValues: { name: user.name || '' },
});
async function onSubmit(data: UpdateProfileValues) {
try {
await updateProfile(data);
await session.update();
} catch (err) {
console.log(err);
}
}
return(/* Markup...*/)
}7 Replies
Barbary LionOP
So right now the data gets updated only on session expire => logout
Barbary LionOP
Should I share the repo for a wider view of the project?
Barbary LionOP
I have managed to update the jwt token through the client session, the applying the client's session token's name to the server's
Now the only issue im encountering is that I have to refresh the page for the server's token to refresh
Barbary LionOP
This is what im doing in the server action to update the database, and refresh client token
'use server';
import { revalidatePath } from 'next/cache';
import { auth } from '@/auth';
import { db, user } from '@/db';
import { eq } from 'drizzle-orm';
import { UpdateProfileValues, updateProfileSchema } from '@/lib/validation';
import { RedirectType, redirect } from 'next/navigation';
export async function updateProfile(values: UpdateProfileValues) {
const session = await auth();
const id = session?.user?.id;
if (!id) {
throw Error('Unauthorized');
}
const { name } = updateProfileSchema.parse(values);
console.log(await db.update(user).set({ name }).where(eq(user.id, id)).returning().execute());
revalidatePath('/');
redirect('/settings');
}Barbary LionOP
now, isnt the redirect supposed to refresh the page?
Barbary LionOP
if you need further information ping me