awesome-appwrite icon indicating copy to clipboard operation
awesome-appwrite copied to clipboard

Error: No session at createSessionClient

Open mrwebninja opened this issue 1 year ago • 1 comments

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

mrwebninja avatar Jun 12 '24 13:06 mrwebninja

maybe the APPWRITE_SECRET in .env is wrong as in the tutorial you need to change it just like the appwrite provide which is NEXT_APPWRITE_KEY

ronank7z avatar Jun 20 '24 08:06 ronank7z