
I am trying to implement the NextJS Todo Demo app with Appwrite. But, I am using a XYZ app to authenticate users.
Now once the user is checked as isAuthorized
, based on its userId, I want to create a jwt token and sign it for the Appwrite session so that I will be able to fetch ToDo documents associated with this userId from Appwrite.
I just want someone to check my code and guide me to do it in a correct way.
I am currently using getServerSideProps
.
export const getServerSideProps = async (context) => {
const loginProps = await getAuthenticatedUserFromSession(
context.req,
context.res
);
if (loginProps.isAuthorized) {
const appwrite = getAppwrite(loginProps.userID);
const database = new Databases(appwrite, process.env.APPWRITE_DATABASE_ID);
const { data } = await database.listDocuments(
process.env.APPWRITE_DATABASE_ID,
process.env.APPWRITE_COLLECTION_ID,
)
return {
props: {
isAuthorized: loginProps.isAuthorized ?? false,
userID: loginProps.userID ?? "",
initialTodos: data ?? [],
},
};
} else {
return {
props: {
isAuthorized: loginProps.isAuthorized ?? false,
userID: loginProps.userID ?? "",
},
};
}
};

And this is how the getAppwrite function looks like. But I am unable to fetch any ToDo documents. So, I believe the user authentication is not working properly. I have created a few records with the same userId column in the console.
const getAppwrite = async (userId) => {
if (userId) {
const payload = {
userId,
exp: Math.floor(Date.now() / 1000) + 60 * 60,
};
try {
const client = new Client()
.setEndpoint(Server.endpoint)
.setProject(Server.project);
const account = new Account(client);
const userAccount = await account.get();
console.log(userAccount);
const app = { account, database };
const response = await account.createJWT();
const jwtToken = response.jwt;
console.log(response);
const token = jwt.sign(payload, jwtToken);
client.setHeader('X-Appwrite-Header', token);
return client;
} catch (error) {
console.log(error);
}
}
};

Uhh how is the user authenticated to Appwrite in your nextjs app? 🧐

I am trying to sign jwt token with the logged in user's userid in the payload. However, my approach could be wrong as I trying to use Appwrite in an app for the first time. So, basically a 3rd part app is taking care of the user login and registrations and I have a TODO collection in appwrite that I want to fetch and show for each user based on the userId. Please suggest what will be the correct workflow to implement this.

account.get and account.createJWT require an Appwrite session. If you're using some other system to authenticate, there is no Appwrite session and you probably can't take advantage of the Appwrite permission system.
Recommended threads
- Sharing cookies
Hi, I’m using Appwrite Cloud, and I have a setup where my Appwrite backend is hosted on a subdomain (e.g., api.example.com), while my frontend (Next.js app) and...
- Organization not exists anymore
Hello! We have a problem with a cloud database. We are on the Free plan, but after a refresh the site wants me to create a new organisation, and I not see the c...
- JSON and Object Support in Collection do...
I am working with Next.Js and Appwrite Cloud, I am relatively New to Appwrite but i have noticed there is no direct support of JSON and Object support in attrib...
