Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement