`Thread` - Anonymous User Creation --> Conversi...
# ask-a-descoper
j
Thread
- Anonymous User Creation --> Conversion to Authenticated User
Hey Descope Team, TL;DR - is there a way create an anonymous user/JWT, and then eventually convert that same user to authenticated via the
anonymous-user-conversion
flow/screen? I specifically want to ensure that the
sub
persists between generating the anonymous user JWT and authenticating the user in
anonymous-user-conversion
flow, but for some reason, the JWT (stored in a cookie atm) is regenerated and contains a different
sub
when the user authenticates. For reference, I started this thread in #C07P7AN1EQH, but no luck so far.
m
Looping @Aviad Lichtenstadt here
👍 1
a
j
That's the flow I'm currently using. Previously I was using the
create-anonymous-user-with-custom-information
flow (to create the anonymous user) in tandem with the
anonymous-user-conversion
flow (to authenticate the user), which was working. Both of those are using the
Descope
component from
@descope/nextjs-sdk
. --- However, I need a bit more flexibility so I created an API route (I've named it
/create-anonymous-user
) which uses the
@descope/node-sdk
to create an anonymous user and have paired that with the
Descope
component from the
@descope/nextjs-sdk
Copy code
// "/create-anonymous-user" API route

try {
    const client = DescopeClient({ projectId, managementKey });
    const resp = await client.management.jwt.anonymous({});

    if (!resp.ok || !resp.data?.sessionJwt) {
      return NextResponse.json({ success: false, error: "Failed to create session" }, { status: 500 });
    }

    // httpOnly: false so Descope SDK can read it
    cookieStore.set("DS", resp.data.sessionJwt, {
      httpOnly: false,
      secure: process.env.NODE_ENV === "production",
      sameSite: "lax",
      path: "/",
    });

    return NextResponse.json({ success: true });

// <Descope /> component:
 <DescopeFlowStep
     flowId="anonymous-user-conversion"
     redirectUrl={redirectUrl}
     onSuccess={handleAuthSuccess}
 />
m
@Nick Rodriguez can you follow up here ?
n
Yes, let me take a look
j
Thank you! And please let me know if you need any additional context from me
n
From what you described, the behavior typically indicates that the anonymous JWT isn’t being detected by the conversion flow, resulting in the creation of a brand-new user instead of converting the existing one. A few things to double-check: • Is the frontend SDK actually reading the cookie you set? If you’re relying on cookies, make sure
sessionTokenViaCookie={true}
is enabled in your Descope provider. Ref: https://docs.descope.com/auth-methods/oauth/with-sdks/client#import-and-initialize-sdk • Confirm the cookie name lines up everywhere (default is
DS
) — backend, frontend config, and the flow End Action if customized. • Make sure the cookie is accessible to the flow (domain/path/baseUrl match, especially if using a custom domain). • Right before rendering the
anonymous-user-conversion
flow, it’s worth verifying the SDK sees the anonymous JWT (with
danu: true
) rather than an empty/new session. Ref: https://docs.descope.com/management/user-management/anonymous-users#creating-an-anonymous-user If the flow can see the anonymous JWT at the start, it should convert it and retain the same
subject
.
j
Thanks, Nick! That is my understanding as well; the
anonymous-user-conversion
flow can't detect the anon user's JWT. I will go through these suggestions and see if I can get it up-and-running