You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
So I'm making a web app that has both credentials and google login, however I need people to have usernames in order to use the app, so when they sign up with google I redirect them to pick a username, and then I add it to the database and i set a needsUsernameSetup: false boolean. I check if needsUsernameSetup is true in middleware, however my issue is that I dont see a way to update the session with the flag set to false after the user submits the form. I'm using a server action, I tried await signIn("google") but obviously that doesn't work since it just reinstanciates the entire sign in process and the user needs to login again. So in the end the cookie still contains the needsUsernameSetup=true and the user just keeps getting redirected to pick a username even though they already have done so. Do I need to manually decode and reencode the JWT with the new info?
Here's the server action in question:
"use server";
import { auth } from "@/auth";
import { prisma } from "@/lib/prisma";
import { jwtDecode } from "jwt-decode";
import { cookies } from "next/headers";
import { z } from "zod";
const UsernameSchema = z.object({
username: z
.string()
.min(3, "Username must be at least 3 characters")
.max(20, "Username must be at most 20 characters")
.regex(
/^[a-zA-Z0-9_-]+$/,
"Username can only contain letters, numbers, underscore, and hyphen"
),
email: z.string().email(),
});
export async function updateUsername(formData: FormData) {
const username = formData.get("username") as string;
const formEmail = formData.get("email") as string;
// Get the authenticated user's session
const session = await auth();
if (!session?.user?.email) {
throw new Error("Not authenticated");
}
// Verify that the email from the form matches the authenticated user's email
if (formEmail !== session.user.email) {
throw new Error("Unauthorized action");
}
// Validate input
const result = UsernameSchema.parse({ username, email: formEmail });
// Check if username is already taken
const existingUsername = await prisma.user.findUnique({
where: { username: result.username },
});
if (existingUsername) {
throw new Error("Username already taken");
}
// Update user
await prisma.user.update({
where: { email: result.email },
data: { username: result.username, needsUsernameSetup: false },
});
//Here is where I'm stuck, I want the JWT to update and for the user to get redirected to "/"
}
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
So I'm making a web app that has both credentials and google login, however I need people to have usernames in order to use the app, so when they sign up with google I redirect them to pick a username, and then I add it to the database and i set a
needsUsernameSetup: false
boolean. I check ifneedsUsernameSetup
is true in middleware, however my issue is that I dont see a way to update the session with the flag set to false after the user submits the form. I'm using a server action, I triedawait signIn("google")
but obviously that doesn't work since it just reinstanciates the entire sign in process and the user needs to login again. So in the end the cookie still contains theneedsUsernameSetup=true
and the user just keeps getting redirected to pick a username even though they already have done so. Do I need to manually decode and reencode the JWT with the new info?Here's the server action in question:
Beta Was this translation helpful? Give feedback.
All reactions