Back

Retrieve Session via Web-SDK without Cookies

  • 0
  • Accounts
  • Web
JustinBack
14 Apr, 2023, 06:30

Hi!

This is probably an edge case for Appwrite but is it possible to use the Web SDK getSession without cookie functionality?

The application is currently a NodeJS Console App, using the Server SDK is not feasible as accounts are created externally and API Keys is not an option.

Creating a Session via the email session function works just fine (it obviously doesn't save a cookie)

TypeScript

            let credentails = process.env.LOGIN_CREDENTIALS.split(":");
            console.log(color.yellow("** Authenticating with master server using provided credentials **"));
            session = await account.createEmailSession(
                credentails[0],
                credentails[1]
            );

            console.log(color.yellow(`** Logged in as ${session.providerUid}`));

Now onto the part I am getting stuck on:

I noticed there is an getSession(sessionId: string) function in the Account class, unfortunately it crashes Appwrite as it cannot read the cookie for obvious reasons.

TypeScript
appwriteexception: User (role: guests) missing scope (account)    at Client.<anonymous> (/workspace/**/node_modules/appwrite/dist/cjs/sdk.js:393:27)    at Generator.next (<anonymous>)    at fulfilled (/workspace/**/node_modules/appwrite/dist/cjs/sdk.js:24:58)    at process.processTicksAndRejections (node:internal/process/task_queues:95:5) {  code: 401,  type: 'general_unauthorized_scope',  response: {    message: 'User (role: guests) missing scope (account)',    code: 401,    type: 'general_unauthorized_scope',    version: '1.3.1'  }}

My question is: Is it possible to:

1st, verify a session by session id without a cookie 2nd, get user details by session id without a cookie

I know that using the Web SDK is not the proper way, but I haven't found a way to authenticate without exposing an api key to users, as it is a public image.

TL;DR
The user is trying to retrieve a session via the Web SDK without using cookies. They have found a hacky solution by polyfilling the localStorage using node-localstorage. The issue of not being able to generate a JWT without an active session cookie is raised. A suggestion is made to try sending an HTTP request without using the SDK. The user is encountering an error when using the session.$id variable. They ask if it's possible to use the Web SDK getSession function without cookie functionality and if it's possible to verify a session and get user details by session ID without a cookie. The user acknowledges that using the Web SDK is not the
Arth
14 Apr, 2023, 06:40

you can get the session by sessionId using getSession(sessionId: string)

Arth
14 Apr, 2023, 06:40

I don't understand why it would try to read cookie

Arth
14 Apr, 2023, 06:40

it only reads the cookie when you pass "current" as the session

JustinBack
14 Apr, 2023, 06:41

thats what I've tried, according to SO it tries to read a cookie (though I cannot confirm via the source code).

I get message: 'User (role: guests) missing scope (account)', when using the session.$id variable

Arth
14 Apr, 2023, 06:41

alternatively if that's a bug you can try sending an http request without the sdk https://appwrite.io/docs/client/account?sdk=rest-default#accountGetSession

Arth
14 Apr, 2023, 06:41

you will require a JWT though

JustinBack
14 Apr, 2023, 06:42

yeah JWT is an issue right now, as I cannot generate one without an active session cookie :/

Arth
14 Apr, 2023, 06:42

ah that's why the error

JustinBack
14 Apr, 2023, 06:43

JWT wouldnt be a big deal if I could retrieve session details by ID, that way I can store the SID in a file locally

JustinBack
14 Apr, 2023, 06:46
JustinBack
14 Apr, 2023, 07:11

Alright so this issue has been solved in a hacky way:

I've polyfilled the localStorage using node-localstorage and the following 3 lines:

TypeScript

global.window = {
    console: {warn: (() => {})},
    localStorage: new localstorage.LocalStorage(process.env.STORAGE_PATH ?? "../sessions")
};
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