I am developing an SSR app using SvelteKit, in my app the user can create their account, or simply log in if they are already registered.
In both cases I follow the same implementation demonstrated in the SSR authentication tutorials provided in the documentation.
As in the example below 👇
import { SESSION_COOKIE, createAdminClient } from "$lib/server/appwrite.js"
import { redirect } from "@sveltejs/kit"
export const actions = {
login: async ({ request, cookies }) => {
const form = await request.formData()
const email = form.get("email") as string
const password = form.get("pass") as string
const { account } = createAdminClient()
const session = await account.createEmailPasswordSession(email, password)
cookies.set(SESSION_COOKIE, session.secret, {
sameSite: "strict",
expires: new Date(session.expire),
secure: true,
path: "/",
})
redirect(301, "/admin/meu-negocio")
},
}
The cookie that is stored during the login or account creation process is used to create a session client.
export function createSessionClient(cookies: Cookies) {
const session = cookies.get(SESSION_COOKIE)
if (!session) {
throw new Error("Não existe uma sessão válida")
}
const client = new Client()
.setEndpoint(variables.APPWRITE_ENDPOINT)
.setProject(variables.APPWRITE_PROJECT)
.setSession(session)
return {
get account() {
return new Account(client)
},
get databases() {
return new Databases(client)
},
}
}
After the first access everything works perfectly, in the middleware after instantiating the client I can access the logged in user, through the session client, but when I spend some time without using the app and return it is as if the session is no longer valid and I get the following error 👇
{
code: 401,
type: 'general_unauthorized_scope',
response: {
message: 'User (role: guests) missing scope (account)',
code: 401,
type: 'general_unauthorized_scope',
version: '1.5.7'
}
}
I've looked everywhere for a solution to this, or at least to understand what's happening, and I can't find it.
When I check my console, the user still has the session registered in the app and the sessions were configured to last 1 year. Which in this case already comes by default in Appwrite.
Could anyone help me with this?
Recommended threads
- How to change "collection Id" to "collec...
- Oauth issue
Hi, can anyone help me in the Oauth issue, Its working fine in dev but its showing Invalid redirect during production. I have check the redirect url and all. St...
- Google/Apple OAuth to show continue to "...
hi everyone, I am building a react-native app with expo and appwrite. I have setup an OAuth flow with Google and Apple following this doc (https://appwrite.io/b...