Skip to content

Error: No session at createSessionClient #514

Open
@mrwebninja

Description

@mrwebninja

I am building nextjs 14 app using appwrite cloud V1.5.7, trying to authenticate users with appwrite SSR following this article "https://appwrite.io/docs/tutorials/nextjs-ssr-auth/step-1"
signIn & signUp are working fine but getting issues with get LoggedIn user both return response success
after first signup get LoggedIn user working fone but after login in getting issue with get LoggedIn

SignUp function

export const signUp = async (userData: SignUpParams) => {
	const {email, password, firstName, lastName} = userData;
	try {
		const { account } = await createAdminClient();

		const newUserAccount = await account.create(
			ID.unique(), 
			email, 
			password, 
			name
		);
		const session = await account.createEmailPasswordSession(email, password);
	  
		cookies().set("appwrite-session", session.secret, {
		  path: "/",
		  httpOnly: true,
		  sameSite: "strict",
		  secure: true,
		});
		return parseStringify(newUserAccount);
		
	} catch (error) {
		console.log('error', error)
	}
}

SignIn function

export const signIn = async ({email, password}:signInProps) => {
	try {
		const { account } = await createAdminClient();
		const response = await account.createEmailPasswordSession(email, password);
		return parseStringify(response);
	} catch (error) {
		console.log('error', error)
	}
}

getLoggedInUser function

export async function getLoggedInUser() {
	try {
	  const { account } = await createSessionClient();
	  const user = await account.get();
	  return parseStringify(user);
	} catch (error) {
	  console.log( error)
	  return null;
	}
  }

createSessionClient() in appwrite.ts

export async function createSessionClient() {
  const client = new Client()
    .setEndpoint(process.env.NEXT_PUBLIC_APPWRITE_ENDPOINT!)
    .setProject(process.env.NEXT_PUBLIC_APPWRITE_PROJECT!);

  const session = cookies().get("appwrite-session");
  if (!session || !session.value) {
    throw new Error("No session");
  }

  client.setSession(session.value);

  return {
    get account() {
      return new Account(client);
    },
  };
}

error in getLoggedInUser after signing

getLoggedInUser Error: No session
    at createSessionClient (webpack-internal:///(rsc)/./lib/appwrite.ts:20:15)
    at getLoggedInUser (webpack-internal:///(rsc)/./lib/actions/user.action.ts:56:97)
    at Home (webpack-internal:///(rsc)/./app/(root)/page.tsx:21:101)
    at e_ (D:\STUDY\NEXTJS\javascriptmastery\jsm_banking\node_modules\next\dist\compiled\next-server\app-page.runtime.dev.js:35:264092)
    at e (D:\STUDY\NEXTJS\javascriptmastery\jsm_banking\node_modules\next\dist\compiled\next-server\app-page.runtime.dev.js:35:268224)
    at eF (D:\STUDY\NEXTJS\javascriptmastery\jsm_banking\node_modules\next\dist\compiled\next-server\app-page.runtime.dev.js:35:268712)
    at eq (D:\STUDY\NEXTJS\javascriptmastery\jsm_banking\node_modules\next\dist\compiled\next-server\app-page.runtime.dev.js:35:274676)
    at ej (D:\STUDY\NEXTJS\javascriptmastery\jsm_banking\node_modules\next\dist\compiled\next-server\app-page.runtime.dev.js:35:264920)
    at e_ (D:\STUDY\NEXTJS\javascriptmastery\jsm_banking\node_modules\next\dist\compiled\next-server\app-page.runtime.dev.js:35:263962)
    at e (D:\STUDY\NEXTJS\javascriptmastery\jsm_banking\node_modules\next\dist\compiled\next-server\app-page.runtime.dev.js:35:268224)
    at eF (D:\STUDY\NEXTJS\javascriptmastery\jsm_banking\node_modules\next\dist\compiled\next-server\app-page.runtime.dev.js:35:268712)
    at D:\STUDY\NEXTJS\javascriptmastery\jsm_banking\node_modules\next\dist\compiled\next-server\app-page.runtime.dev.js:35:265943
    at Array.toJSON (D:\STUDY\NEXTJS\javascriptmastery\jsm_banking\node_modules\next\dist\compiled\next-server\app-page.runtime.dev.js:35:266407)
    at stringify (<anonymous>)
    at eq (D:\STUDY\NEXTJS\javascriptmastery\jsm_banking\node_modules\next\dist\compiled\next-server\app-page.runtime.dev.js:35:274775)
    at eJ (D:\STUDY\NEXTJS\javascriptmastery\jsm_banking\node_modules\next\dist\compiled\next-server\app-page.runtime.dev.js:35:275293)
    at Timeout._onTimeout (D:\STUDY\NEXTJS\javascriptmastery\jsm_banking\node_modules\next\dist\compiled\next-server\app-page.runtime.dev.js:35:265080)
    at listOnTimeout (node:internal/timers:569:17)
    at process.processTimers (node:internal/timers:512:7)

Please help me with this
Thanks in advance

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions