#[UNSOLVED] Need Help regarding server side authentication/client setup

10 messages · Page 1 of 1 (latest)

grim shore
#

I have initialised two clients in appwrite one with .setSession() other with .setKey()

. If have to call the function for creating those clients and get something from it like databses, account, user, storage etc
I have used these things in many places so i have to call the function in many places and the function will get triggered everytime the page loads but before i had the exports to databses , account, user, storage now will it consume more resources and cost more time , or is there something i can do ?

#

prev code

import env from '@/env';
import { Client, Storage, Databases, Users, Avatars } from 'node-appwrite';

let client = new Client();

client
  .setEndpoint(env.appwrite.endpoint)
  .setProject(env.appwrite.projectId)
  .setKey(env.appwrite.apiKey);
;

const databases = new Databases(client);
const avatars = new Avatars(client);
const storage = new Storage(client);
const users = new Users(client);

export { client, databases, users, avatars, storage };
#

current code


import { SESSION_COOKIE } from '@/const';
import env from '@/env';
import { cookies } from 'next/headers';
import { Client, Storage, Databases, Users, Avatars, Account } from 'node-appwrite';

export async function createSessionClient() {
  const client = new Client()
    .setEndpoint(env.appwrite.endpoint)
    .setProject(env.appwrite.projectId);
  
  const session = cookies().get(SESSION_COOKIE);

  if (!session || !session.value) {
    throw new Error("No session found");
  }

  client.setSession(session.value);

  return {
    get account() {
      return new Account(client);
    },
    get storage() {
      return new Storage(client);
    },
    get databases() {
      return new Databases(client);
    },
    get users() {
      return new Users(client);
    },
    get avatars() {
      return new Avatars(client);
    },
  };
}


export async function createAdminClient() {
  const client = new Client()
    .setEndpoint(env.appwrite.endpoint)
    .setProject(env.appwrite.projectId)
    .setKey(env.appwrite.apiKey);
  
  return {
    get account() {
      return new Account(client);
    },
    get storage() {
      return new Storage(client);
    },
    get databases() {
      return new Databases(client);
    },
    get users() {
      return new Users(client);
    },
    get avatars() {
      return new Avatars(client);
    },
  };
}

export async function getLoggedInUser() {
  try {
    const { account } = await createSessionClient()
    return await account.get();
  } catch (err) {
    return null;
  }
}

grim shore
#

I tried setting auth now, i have to do everything with api routes, when login i have to create a login route where the user will get logged in and a session will be set in the cookies, if i have to logout i have to also create a api route for it and same for getting the logged in user i have to create a api route for it

golden basin
#

after deployment my blog with appwrite project in react. Sometimes userData is not get. Please help me

pearl robin
golden basin
#

@pearl robin

#

when i click submit sometimes done but sometimes it shows error

#
    async createPost({title,slug,content,featuredImage,status,userId}){
          try{
            return await this.databases.createDocument(
                conf.appwriteDatabaseId,
                conf.appwriteCollectionId,
                slug,
                {
                    title,
                    content,
                    featuredImage,
                    status,
                    userId,
                }
            )
          }
          catch(error){
            console.log("Appwrite serive :: createPost :: error", error);
          }
    } ```
pearl robin