Advertisement
NHeroOP

config appwrite

Sep 22nd, 2024
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. import { SESSION_COOKIE } from '@/const';
  3. import env from '@/env';
  4. import { cookies } from 'next/headers';
  5. import { Client, Storage, Databases, Users, Avatars, Account } from 'node-appwrite';
  6.  
  7. export async function createSessionClient() {
  8.   const client = new Client()
  9.     .setEndpoint(env.appwrite.endpoint)
  10.     .setProject(env.appwrite.projectId);
  11.  
  12.   const session = cookies().get(SESSION_COOKIE);
  13.  
  14.   if (!session || !session.value) {
  15.     throw new Error("No session found");
  16.   }
  17.  
  18.   client.setSession(session.value);
  19.  
  20.   return {
  21.     get account() {
  22.       return new Account(client);
  23.     },
  24.     get storage() {
  25.       return new Storage(client);
  26.     },
  27.     get databases() {
  28.       return new Databases(client);
  29.     },
  30.     get users() {
  31.       return new Users(client);
  32.     },
  33.     get avatars() {
  34.       return new Avatars(client);
  35.     },
  36.   };
  37. }
  38.  
  39.  
  40. export async function createAdminClient() {
  41.   const client = new Client()
  42.     .setEndpoint(env.appwrite.endpoint)
  43.     .setProject(env.appwrite.projectId)
  44.     .setKey(env.appwrite.apiKey);
  45.  
  46.   return {
  47.     get account() {
  48.       return new Account(client);
  49.     },
  50.     get storage() {
  51.       return new Storage(client);
  52.     },
  53.     get databases() {
  54.       return new Databases(client);
  55.     },
  56.     get users() {
  57.       return new Users(client);
  58.     },
  59.     get avatars() {
  60.       return new Avatars(client);
  61.     },
  62.   };
  63. }
  64.  
  65. export async function getLoggedInUser() {
  66.   try {
  67.     const { account } = await createSessionClient()
  68.     return await account.get();
  69.   } catch (err) {
  70.     return null;
  71.   }
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement