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
- Send Email Verification With REST
I am using REST to create a user on the server side after receiving form data from the client. After the account is successfully created i wanted to send the v...
- Use different email hosts for different ...
Hello, I have 2 projects and i want to be able to set up email templates in the projects. Both projects will have different email host configurations. I see ...
- Get team fail in appwrite function
I try to get team of a user inside appwrite function, but i get this error: `AppwriteException: User (role: guests) missing scope (teams.read)` If i try on cl...