Back

Nuxt3 an Appwrite Auth

  • 0
  • Accounts
  • Web
WuGGu
6 Oct, 2023, 14:33

Hi all, I try to build a webapp which uses SSR and the Appwrite Auth to Login. I found some Templates which uses H3 for Session mgmt. But I can't get it to work with my middleware. Maybe someone did this already and can me push in the right direction. I use Pinia to save the User data after login in, but reload of cause the pinia store is cleared. It works only after direct login and handling of the middleware on client side. I have a middleware which checks if the session right now is not expired.

TypeScript
import { useUserStore } from "~/stores/userStore";

export default defineNuxtRouteMiddleware(async (to, from) => {
  const authStore = useUserStore();
  if (authStore.user) {
    const sessionStatus = await authStore.checkSession(authStore.user.$id);
    if (!sessionStatus) {
      return navigateTo("/login");
    }
  } else {
    return navigateTo("/login");
  }
});

If this is more a Nuxt topic and wrong in this place. Im sorry and please close this.

Thanks a lot.

TL;DR
The user is struggling to add authentication with Appwrite to their Nuxt 3 app. Someone recommends using the appwrite-ssr package and provides a link to the package and examples. Another user explains that they use the client SDK on the server side and provides a solution involving creating a session as REST and sending cookies from server to client. Another user suggests avoiding SSR if possible due to complexity. The original user asks for a repo where they can see an approach using Appwrite authentication with Nuxt, and someone provides a link to a repo that may be helpful. The original user also shares their middleware code for checking session expiration. The
Drake
6 Oct, 2023, 19:38

SSR is a million times more complicated. i highly suggest avoiding all of it if you can

Drake
6 Oct, 2023, 19:39

if you really want to use SSR, the only resource i can provide is https://github.com/Meldiron/appwrite-ssr-nuxt. You'll have to read through and understand it...then see if you can apply it to middlewares

WuGGu
7 Oct, 2023, 05:23

thanks a lot. this was really helpful. Now I understand, what the challenge is with ssr. Now I have to decided if it is worth it. πŸ˜„

otik
7 Oct, 2023, 08:52

Perhaps it is really bad, but i use client-sdk on server-side. I only have to create session as REST because i need to get cookies and then send cookies from server to client. Then with every request to server you get session in cookies. You will only have to set this session to appwrite request headers on server-side.

Drake
7 Oct, 2023, 14:46

Client SDK server side is perfectly fine

WuGGu
13 Oct, 2023, 08:54

Hi Otik, I still struggle to add auth with Appwrite to my Nuxt 3 app. Your approach sounds interesting. Do you have any repo where I can look at your approach?

otik
13 Oct, 2023, 08:58

Yes i made npm package for it. But today i will change many things there (i will text you here then) NPM package: appwrite-ssr

There you can see my example with sveltekit, but you can easly implement it to Nuxt3.

otik
13 Oct, 2023, 09:05

I dont have any repo you could see. But on appwrite-ssr you can find few examples. (!but how i have already said i will change it a little. I recommend you to wait, however if you download it now your code will work even in next version)

otik
13 Oct, 2023, 09:07
WuGGu
13 Oct, 2023, 09:22

awesome I will have a look πŸ™‚

otik
13 Oct, 2023, 18:35

Now it should be all ok to use. If you consider to use it and will find any mistake send me a message (either email that is on site or discord) I even added repo that you could see how to implement it or you can use example codes on npm site.

otik
14 Oct, 2023, 20:31

Did you try?

WuGGu
14 Oct, 2023, 20:39

sorry had no time till now. will look into it next week. πŸ™‚

Reply

Reply to this thread by joining our Discord

Reply on Discord

Need support?

Join our Discord

Get community support by joining our Discord server.

Join Discord

Get premium support

Join Appwrite Pro and get email support from our team.

Learn more