Back

Obtain Appwrite user object from createEmailPasswordSession()

  • 0
  • Web
Isak
17 Jun, 2024, 17:24
TypeScript
// Create the Appwrite client.
const { account } = createAdminClient();

// Create the session using the client
const session = await account.createEmailPasswordSession(username, password);

How can I obtain the user object from this?

I use this code in my hooks.server.ts

TypeScript
const { account } = createSessionClient(event);
let appwriteUser = await account.get();

But running account.get() on the account variable that I get from createAdminClient will throw a error:

"(role: applications) missing scope (account)"

TL;DR
Developers are discussing how to obtain the Appwrite user object from `createEmailPasswordSession()`. They mention using types for `Models`, defining user preferences, and setting up services like `Users`. The solution involves ensuring the `account` is from the `session` call and not `clientAdmin`. The issue seems to lie in the handling of keys and scopes. By properly configuring the `createSessionClient()` function and handling session data, the user object can be obtained successfully using `account.get()`.
Meldiron
17 Jun, 2024, 17:25

Hii 👋 Is this Svelte Kit?

Isak
17 Jun, 2024, 17:25

Yupp

Meldiron
17 Jun, 2024, 17:26

Can you please show us your createSessionClient() function?

Isak
17 Jun, 2024, 17:26
TypeScript
export function createSessionClient(event: any) {
    const client = new Client()
        .setEndpoint(PUBLIC_APPWRITE_ENDPOINT)
        .setProject(PUBLIC_APPWRITE_PROJECT);

    // Extract our custom domain's session cookie from the request
    const session = event.cookies.get(SESSION_COOKIE);
    if (!session) {
        redirect(301, "/login");
    }

    client.setSession(session);

    // Return the services we want to use.
    return {
        get account() {
            return new Account(client);
        }
    };
}
Meldiron
17 Jun, 2024, 17:28

Hmm, nowhere in this method you setKey. But the error seems to be that API Key is used when doing account.get(). Can you please ensure account is from this clientSession call, and not from clientAdmin?

Isak
17 Jun, 2024, 17:29

The problem isn't in the createSessionClient()

Isak
17 Jun, 2024, 17:30

I can successfully obtain the authed user from that function, its when I do the login that the issue occurs. I want to get the appwrite user object once I initilize a session with createEmailPasswordSession

Meldiron
17 Jun, 2024, 17:31

For this scenario, considering you have admin session already, you could:

TypeScript
session = await createEmailPasswordSession(...)
userId = session.userId
user = users.get(userId)

(users is new Users(client) where client is from createAdminClient())

Isak
17 Jun, 2024, 17:34
TypeScript
const { account } = createAdminClient();
const users = new Users(account)

Passing the value of createAdminClient into the Users is throwing an error

Argument of type 'Account' is not assignable to parameter of type 'Client'.

Isak
17 Jun, 2024, 17:36

client doesn't exist on createAdminClient

Meldiron
17 Jun, 2024, 17:37

I meant something like this:

TypeScript
const { users } = createAdminClient();
const user = await users.get(userId);

You might need to define this inside createAdminClient with getter, something along

TypeScript
    // Return the services we want to use.
    return {
        get users() {
            return new Users(client);
        }
    };
Isak
17 Jun, 2024, 17:38

ah, i see

Isak
17 Jun, 2024, 17:43

@Meldiron how do I set what perfs there are?

const user: Models.User<Models.Preferences>

Error: Property 'domain' does not exist on type 'Preferences'.

Meldiron
17 Jun, 2024, 17:47

You can do Models.User<any> to not type it at all, and it will work. Otherwise you could define your own type of prefs that you expect, like

TypeScript
export type UserPrefsType = {
  theme: string;
  devices: number;
  nickname: string;
}

And to use custom type, you can const user: Models.User<UserPrefsType>

Meldiron
17 Jun, 2024, 17:47

Both approaches are valid, up to you. I personally dont sture too much in prefs, so benefits from type isn't too significant. So I go the easy way of doing any usually.

Isak
17 Jun, 2024, 17:47

Ah

Isak
17 Jun, 2024, 17:48

ill rock with that, i do that on other places aswell

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