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
- Auto Updating Backend & Auth via Appwrit...
<@870607367597850624> Hey everyone 👋 I wanted to ask to ask for a friend, he’s asking if Appwrite be used in a similar way to Supabase when integrated with AI ...
- execution failed
When executing an appwrite function, I'm getting a 500 error, but I don't see it in executions. This issue appeared today. Here's the appwrite function ID: 68b4...
- Is Database Operators available in Cloud...
Is it possible to do the above?