Back

[SOLVED] Account not working in appwrite functions

  • 0
  • Functions
  • Accounts
sswastik02
23 Dec, 2023, 09:08

I have a function that checks for authentication in the following way

  1. Initialize the client using jwt passed by the user
TypeScript
  const client = new Client()
    .setEndpoint(process.env.APPWRITE_API_ENDPOINT)
    .setProject(process.env.APPWRITE_FUNCTION_PROJECT_ID)
    .setJWT(req.headers['x-appwrite-user-jwt'])
  1. Authenticate function to check if a user is a valid one
TypeScript
import { Account } from "node-appwrite"

export async function authenticate(client, log, error) {
    log("Initializing Account")
    const account = new Account(client)
    let user = null
    try {
        log("Getting account")
        user = await account.get()
        log(user)
        if (!user.email)
            throw Error("User does not have email")
        log(user.$id, 'authenticated')
    } catch (e) {
        user = null
        error(e.toString())
    }
    return user
}

However I get the following logs,

TypeScript
Initializing Account
Getting account

And the following error logs,

TypeScript
Error: connect ECONNREFUSED 127.0.0.1:80

It probably means the Accounts API is not working in node-appwrite, Can someone please help me out here?

TL;DR
The user was experiencing an issue with their account not working in appwrite functions. The issue was related to setting the `APPWRITE_API_ENDPOINT` environment variable with quotes. After removing the quotes, the issue was resolved and the account started working.
D5
23 Dec, 2023, 13:01

Is this cloud or self-hosted?

sswastik02
24 Dec, 2023, 05:53

In cloud

ideclon
24 Dec, 2023, 06:49

What have you set APPWRITE_API_ENDPOINT to?

sswastik02
25 Dec, 2023, 08:06

Isn't that passed during execution in cloud?

sswastik02
25 Dec, 2023, 08:10

Has that changed in the recent update ?

sswastik02
25 Dec, 2023, 08:27

Ok now I have set APPWRITE_API_ENDPOINT='https://cloud.appwrite.io/v1' Still the error is the same

ideclon
25 Dec, 2023, 17:27

Not to my knowledge - at least, it’s not in the list of variables in the docs

ideclon
25 Dec, 2023, 17:29

Interesting. Have you tried dumping out the vars to check that they’re actually being set correctly?

sswastik02
25 Dec, 2023, 17:41

Yes I did that too, it prints whatever I have set, in fact I am using the same instantiation of client in another function (except the JWT part), it works fine

sswastik02
25 Dec, 2023, 17:47

I have also found that the error is at user = await account.get() in the authenticate function, as I am catching and error logging it.

sswastik02
25 Dec, 2023, 17:47

Everything before it prints in the log perfectly

ideclon
25 Dec, 2023, 17:48

Yeah, that’s what I assumed

sswastik02
25 Dec, 2023, 17:50

Ok

ideclon
25 Dec, 2023, 17:51

What if you just put https://cloud.appwrite.io/v1 in .setEndpoint(), instead of using the envvar?

sswastik02
25 Dec, 2023, 17:52

Ok trying now

sswastik02
25 Dec, 2023, 17:54

If you don't mind me asking, why do you think that the client is the problem ?

ideclon
25 Dec, 2023, 17:55

It looks like it’s trying to connect to the wrong thing. It shouldn’t be attempting a connection to 127.0.0.1 - it should be trying to connect to cloud.appwrite.io

ideclon
25 Dec, 2023, 17:56

And I don’t see anything else it could be (in the code)

sswastik02
25 Dec, 2023, 17:58

Yea it seems to be working now

sswastik02
25 Dec, 2023, 17:58

Thank you so much I did not notice the quotes here in the log for the environement variable

sswastik02
25 Dec, 2023, 17:59

APPWRITE_API_ENDPOINT=https://cloud.appwrite.io/v1

Setting it to this works (without quotes)

sswastik02
25 Dec, 2023, 17:59

Thanks again @ideclon

sswastik02
25 Dec, 2023, 17:59

[SOLVED] Account not working in appwrite functions

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