Back

SSR Login

  • 0
  • Auth
Pingu
20 May, 2024, 08:27

Hey, having problems with the intended way to do SSR login...

I'm looking at this as a reference: https://appwrite.io/docs/products/auth/server-side-rendering#creating-sessions however I am using a .net back end (front end also, but thats not relevant here).

I create a singleton for my Admin Client in DI, and create the session client as a transient which will be instantiated each time as so (I've removed the project and key's, and this is a test project so no need for hiding them in secrets right now):

TypeScript
var adminClient = new Appwrite.Client()
    .SetEndpoint("")
    .SetProject("")
    .SetKey("");

builder.Services.AddKeyedSingleton("admin", adminClient);
builder.Services.AddKeyedTransient("session", (provider, key) => new Appwrite.Client().SetEndpoint("").SetProject(""));

I've created an API endpoint, which currently isn't setting cookies as I haven't gotten that far, but thats to be done, as follows:

TypeScript
public static async Task<IResult> Login(LoginModel login, [FromKeyedServices("admin")] Appwrite.Client client)
{
    var accountClient = new Account(client);

    var result = await accountClient.CreateEmailPasswordSession(login.Email, login.Password);

    return TypedResults.Ok();
}

This appears to match the docs I referenced above - using the admin client not the session client, and calling create email password session.

Everything works at first, but then any action I try and make after this call with the admin client will fail with the following error:

TypeScript
Appwrite.AppwriteException: API key and session used in the same request. Use either `setSession` or `setKey`. Learn about which authentication method to use in the SSR docs: https://appwrite.io/docs/products/auth/server-side-rendering

I never call setSession on the admin client, but it appears to be having its session set by calling CreateEmailPasswordSession?

Is this meant to be the case?

I could solve it be injecting the AdminClient everywhere as Transient rather than a singleton, but that's not the real solution I don't feel...

TL;DR
Issue with SSR login implementation in .NET backend. Using Admin Client for session creation, facing API key and session conflict error when making subsequent calls. Session seems to be set by CreateEmailPasswordSession. Solution: Inject Admin Client as Transient instead of singleton.
Reply

Reply to this thread by joining our Discord

Reply on Discord

Need support?

Join our Discord

Get community support by joining our Discord server.

Join Discord

Get premium support

Join Appwrite Pro and get email support from our team.

Learn more