Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: remove useless environment variables #113

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 1 addition & 5 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
POSTGREST_API=http://postg-rest:3000
DETAILED_ERRORS=false

ESP_PATH=./esp32/config
DB_PATH=./database/data
DEBUG=false

JWT_SECRET=
POSTGRES_PASSWORD=
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ sed -i '/POSTGRES_PASSWORD/d' .env
echo "POSTGRES_PASSWORD=$(tr -dc A-Za-z0-9 </dev/urandom | head -c 32)" >> .env
```

It is recommended to let the other environment variables as they are
It is recommended to let the environment variables "DEBUG" on false for production

---
Finally, you **have to** fill the Wi-Fi credentials in `esp32/config/secrets.yaml`\
Expand Down
4 changes: 2 additions & 2 deletions docker-compose.prod.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ services:
esphome:
image: esphome/esphome:2024.4
volumes:
- "${ESP_PATH}:/config"
- "./esp32/config:/config"
- /etc/localtime:/etc/localtime:ro
- /dev/ttyUSB0:/dev/ttyUSB0
privileged: true
Expand All @@ -13,7 +13,7 @@ services:
image: postgres:16.3-alpine
shm_size: 128mb
volumes:
- "${DB_PATH}:/var/lib/postgresql/data"
- "./database/data:/var/lib/postgresql/data"
environment:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
Expand Down
4 changes: 2 additions & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ services:
esphome:
image: esphome/esphome:2024.4
volumes:
- "${ESP_PATH}:/config"
- "./esp32/config:/config"
- /etc/localtime:/etc/localtime:ro
- /dev/ttyUSB0:/dev/ttyUSB0
privileged: true
Expand All @@ -13,7 +13,7 @@ services:
image: postgres:16.3-alpine
shm_size: 128mb
volumes:
- "${DB_PATH}:/var/lib/postgresql/data"
- "./database/data:/var/lib/postgresql/data"
environment:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
Expand Down
18 changes: 13 additions & 5 deletions php/src/Login.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,27 +16,35 @@ public function __invoke(Request $request, Response $response, mixed $args): Res

// Generate a token and use it to get the user
$token = JWT::encode(['role' => 'web_login', 'exp' => time()], $_ENV['JWT_SECRET'], 'HS256');
$user = callAPI('GET', $_ENV['POSTGREST_API'] . "/users?username=eq.{$data['username']}&limit=1&select=password,id", [], ["Authorization: Bearer $token"]);
$user = callAPI('GET', "http://postg-rest:3000/users?username=eq.{$data['username']}&limit=1&select=password,id", [], ["Authorization: Bearer $token"]);

// Check if the answer is valid
if ($user === false)
return output($response, ['error' =>
$_ENV['DETAILED_ERRORS'] === 'true' ?
$_ENV['DEBUG'] === 'true' ?
'Unable to connect to the API' :
'Unknown error'
], 500);
$user = json_decode($user, true);
if (isset($user["message"]))
return output($response, ['error' =>
$_ENV['DETAILED_ERRORS'] === 'true' ?
$_ENV['DEBUG'] === 'true' ?
$user :
'Unknown error'
], 500);

if (empty($user))
return output($response, ['error' => 'Unknown user'], 401);
return output($response, ['error' =>
$_ENV['DEBUG'] === 'true' ?
'Unknown user' :
'Invalid username or password'
], 401);
if (!password_verify($data['password'], $user[0]['password']))
return output($response, ['error' => 'Invalid password'], 401);
return output($response, ['error' =>
$_ENV['DEBUG'] === 'true' ?
'Invalid password' :
'Invalid username or password'
], 401);

// Generate a token for the user that expires at midnight
$payload = [
Expand Down
Loading