selfhosting icon indicating copy to clipboard operation
selfhosting copied to clipboard

Cannot sign up user on new install

Open alvitali opened this issue 1 year ago • 7 comments

Hi,

I just installed swetrix v4.0.1 from scratch on a new host. Everything seems to be running, however, when I try to sign up with a new user, I get the error Cannot read properties of undefined (reading 'data').

Looking at the browser console, I see requests to the backend failing with:

Access to XMLHttpRequest at 'https://swetrix-api.example.com/v1/auth/register' from origin 'https://swetrix.example.com' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.

POST https://swetrix-api.example.com/v1/auth/register net::ERR_FAILED

I'm calling the frontend from swetrix.example.com, and have configured API_URL to swetrix-api.example.com. Both are proxied through traefik with the same configuration. Is this a misconfiguration on my side?

alvitali avatar Sep 25 '25 14:09 alvitali

Same

cirrusflyer avatar Sep 25 '25 16:09 cirrusflyer

Hey, so sorry for that, will release a fix shortly

Blaumaus avatar Sep 25 '25 18:09 Blaumaus

Hmm, can't find anything wrong with the code, but I think the problem might be that the database was not initialised for some reason. I've tried doing a deployment from scratch and it seems to work fine, so probably it's some race condition between the database and API docker services

Can you please run these scripts and let me know if you still have this issue?

docker compose exec swetrix-api node migrations/clickhouse/initialise_database.js
docker compose exec swetrix-api node migrations/clickhouse/initialise_selfhosted.js

Blaumaus avatar Sep 25 '25 19:09 Blaumaus

Thank you for the quick feedback! Running the above scripts fails as swetrix-api cannot authenticate to clickhouse because the container is missing the password env:

[[email protected]] injecting env (0) from .env -- tip: 🔐 prevent committing .env to code: https://dotenvx.com/precommit
Query ERROR:  CREATE DATABASE IF NOT EXISTS analytics
ClickHouseError: default: Authentication failed: password is incorrect, or there is no user with such name.

If you have installed ClickHouse and forgot password you can reset it in the configuration file.
The password for default user is typically located at /etc/clickhouse-server/users.d/default-password.xml
and deleting this file will reset the password.
See also /etc/clickhouse-server/users.xml on the server where ClickHouse is installed.

. 
    at parseError (/app/node_modules/@clickhouse/client-common/dist/error/error.js:38:16)
    at ClientRequest.onResponse (/app/node_modules/@clickhouse/client/dist/connection/node_base_connection.js:443:107)
    at process.processTicksAndRejections (node:internal/process/task_queues:105:5) {
  code: '516',
  type: 'AUTHENTICATION_FAILED'
}
Query ERROR:  CREATE TABLE IF NOT EXISTS analytics.analytics
  (
    psid Nullable(UInt64),
    pid FixedString(12),
    host Nullable(String),
    pg Nullable(String),
    dv LowCardinality(Nullable(String)),
    br LowCardinality(Nullable(String)),
    brv Nullable(String),
    os LowCardinality(Nullable(String)),
    osv Nullable(String),
    lc LowCardinality(Nullable(String)),
    ref Nullable(String),
    so Nullable(String),
    me Nullable(String),
    ca Nullable(String),
    te Nullable(String),
    co Nullable(String),
    cc Nullable(FixedString(2)),
    rg LowCardinality(Nullable(String)),
    ct Nullable(String),
    meta Nested
    (
      key String,
      value String
    ),
    created DateTime('UTC')
  )
  ENGINE = MergeTree()
  PARTITION BY toYYYYMM(created)
  ORDER BY (pid, created);
ClickHouseError: default: Authentication failed: password is incorrect, or there is no user with such name.

If you have installed ClickHouse and forgot password you can reset it in the configuration file.
The password for default user is typically located at /etc/clickhouse-server/users.d/default-password.xml
and deleting this file will reset the password.
See also /etc/clickhouse-server/users.xml on the server where ClickHouse is installed.

. 
    at parseError (/app/node_modules/@clickhouse/client-common/dist/error/error.js:38:16)
    at ClientRequest.onResponse (/app/node_modules/@clickhouse/client/dist/connection/node_base_connection.js:443:107)
    at process.processTicksAndRejections (node:internal/process/task_queues:105:5) {
  code: '516',
  type: 'AUTHENTICATION_FAILED'
}

Checking docker compose exec swetrix-api env | grep CLICKHOUSE I can see the swetrix-api container is missing the value for CLICKHOUSE_PASSWORD.

I had manually set this variable in the .env which I just realised is not part of the example .env, it is part of the env variables for the clickhouse container in the compose file however.

I assume not setting any value for CLICKHOUSE_PASSWORD in my .env file will work, as clickhouse will initialize with an empty password. This should be fine, as clickhouse is running on a separate docker network anyways.

If not using a password for the clickhouse db is intended, maybe the env variable could be removed from the default compose altogether. I assume docker compose does not allow containers to resolve .env variables not referenced in another container config?

alvitali avatar Sep 26 '25 14:09 alvitali

Thank you! Makes sense

I added the CLICKHOUSE_PASSWORD variable to swetrix-api, I think it should fix the issue now! The Swetrix API itself supports this variable, I just forgot to add it to compose config earlier

https://github.com/Swetrix/selfhosting/commit/8b02d67cfd04faeec336028b20f7e20998e3f979

Blaumaus avatar Sep 27 '25 20:09 Blaumaus

I also just tried to deploy it on a new server and it seems to work just fine!

https://youtu.be/e5PdKBX3nsw

@alvitali @cirrusflyer can you please give it another try and let me know if it works fine now?

Blaumaus avatar Sep 27 '25 20:09 Blaumaus

@Blaumaus works great for me! Looking forward to testing the application. Thank you for open sourcing the project!

I'll close the issue if that's okay

alvitali avatar Sep 28 '25 20:09 alvitali