A server app built using Shelf, designed to handle user registration and login functionality. The server is configured to be easily run locally or with Docker.
This project demonstrates a simple REST API that allows users to:
- Register (
POST /register
) - Login (
POST /login
)
The user data is stored persistently in a JSON file on disk.
- User Registration: Users can register with a username and password.
- Login Authentication: Verify user credentials to authenticate requests.
- Data Persistence: User data is stored locally in a JSON file.
- Error Handling: Proper HTTP status codes for success and error responses.
- Logging: All incoming requests and responses are logged.
Registers a new user with a username and password.
- Request Body: JSON with
username
andpassword
. - Responses:
200 OK
: User successfully registered.400 Bad Request
: Missing or emptyusername
.409 Conflict
: Username already exists.
Example:
$ curl -X POST http://0.0.0.0:8080/register \
-H "Content-Type: application/json" \
-d '{"username": "testuser", "password": "mypassword"}'
Authenticates a user by checking the provided credentials.
- Request Body: JSON with
username
andpassword
. - Responses:
200 OK
: Login successful.400 Bad Request
: Missing or emptyusername
.401 Unauthorized
: Invalid credentials.
Example:
$ curl -X POST http://0.0.0.0:8080/login \
-H "Content-Type: application/json" \
-d '{"username": "testuser", "password": "mypassword"}'
You can run the server locally with the Dart SDK:
-
Start the server:
$ dart run bin/server.dart Server listening on port 8080
-
Use
curl
or a similar tool to test the API:$ curl -X POST http://0.0.0.0:8080/register \ -H "Content-Type: application/json" \ -d '{"username": "testuser", "password": "mypassword"}' $ curl -X POST http://0.0.0.0:8080/login \ -H "Content-Type: application/json" \ -d '{"username": "testuser", "password": "mypassword"}'
If you have Docker Desktop installed, you can build and run the server using Docker:
-
Build the Docker image:
$ docker build . -t userserver
-
Run the Docker container:
$ docker run -it -p 8080:8080 userserver Server listening on port 8080
-
Test the API from another terminal:
$ curl -X POST http://0.0.0.0:8080/register \ -H "Content-Type: application/json" \ -d '{"username": "testuser", "password": "mypassword"}' $ curl -X POST http://0.0.0.0:8080/login \ -H "Content-Type: application/json" \ -d '{"username": "testuser", "password": "mypassword"}'
You can observe logs of incoming requests in the terminal running the server. For example:
2024-11-27T15:47:04.620417 0:00:00.000158 POST [200] /register
2024-11-27T15:47:08.392928 0:00:00.001216 POST [401] /login
- Dart SDK: Version 3.0 or later.
- Optional: Docker for containerized deployment.
- Add token-based authentication (e.g., JWT).
- Implement HTTPS for secure communication.
- Replace JSON file storage with a database (e.g., SQLite or PostgreSQL).
- Add rate limiting to prevent abuse of the endpoints.
This project is a simple starting point for understanding RESTful APIs in Dart and building user authentication systems.