Hydra is a zero-config API boilerplate with Laravel 10x + Laravel Sanctum that comes with an excellent user and role management API out of the box
Hydra is a zero-config API boilerplate with Laravel 10x + Laravel Sanctum that comes with an excellent user and role management API out of the box
Hydra is a zero-config API boilerplate with Laravel Sanctum and comes with excellent user and role management API out of the box. Start your next big API project with Hydra, focus on building business logic, and save countless hours of writing boring user and role management API again and again.
Hydra works with Laravel 10.x and Sanctum 3.x. If you want to work with Laravel 9.x then checkout the 9.x branch.
It's super easy to get Hydra up and running.
First clone the project and change the directory
git clone https://github.com/hasinhayder/hydra.git
cd hydraThen follow the process using either Docker or without Docker (simple).
composer install.env.example to .envcp .env.example .envphp artisan key:generatephp artisan serveThat's mostly it! You have a fully running laravel installation with Sanctum, all configured.
docker run --rm \
-u "$(id -u):$(id -g)" \
-v $(pwd):/var/www/html \
-w /var/www/html \
laravelsail/php81-composer:latest \
composer install --ignore-platform-reqs.env.example to .envcp .env.example .env./vendor/bin/sail up./vendor/bin/sail artisan key:generateTo learn more about Sail, visit the official Doc.
Open your .env file and change the DATABASE options. You can start with SQLite by following these steps
touch database/hydra.sqliteOr simply create a new file as hydra.sqlite inside your database folder.
php artisan migrate:fresh --seedOR
you can run them separately using the following commands
php artisan migrate:freshNow your database has essential tables for user and roles management.
Run db:seed, and you have your first admin user, some essential roles in the roles table, and the relationship correctly setup.
php artisan db:seedPlease note that the default admin user is [email protected] and the default password is hydra. You should create a new admin user before deploying to production and delete this default admin user. You can do that using the available Hydra user management API or any DB management tool.
Here is a list of default routes. Run the following artisan command to see this list in your terminal.
php artisan route:listHydra comes with these super-admin,admin,editor,customer & user roles out of the box. For details, open the roles table after database seeding, or open the laravel tinker and experiment with the Role model.
php artisan tinkerrun the following command
>>> Role::select(['id','slug','name'])->get()
//or
>>> Role::all(['id','name','slug'])
//or
>>> Role::all()Let's have a look at what Hydra has to offer. Before experimenting with the following API endpoints, run your Hydra project using php artisan serve command. For the next part of this documentation, we assumed that Hydra is listening at http://localhost:8000
You can make an HTTP POST call to create/register a new user to the following endpoint. Newly created users will have the user role by default.
http://localhost:8000/api/usersAPI Payload & Response
You can send a Form Multipart payload or a JSON payload like this.
{
"name":"Hydra User",
"email":"[email protected]",
"password":"Surprisingly A Good Password"
}Voila! Your user has been created and is now ready to log in!
If this user already exists, then you will receive a 409 Response like this
{
"error": 1,
"message": "user already exists"
}Remember Hydra comes with the default admin user? You can log in as an admin by making an HTTP POST call to the following route.
http://localhost:8000/api/loginAPI Payload & Response
You can send a Form Multipart or a JSON payload like this.
{
"email":"[email protected]",
"password":"hydra"
}You will get a JSON response with user token. You need this admin token for making any call to other routes protected by admin ability.
{
"error": 0,
"token": "1|se9wkPKTxevv9jpVgXN8wS5tYKx53wuRLqvRuqCR"
}For any unsuccessful attempt, you will receive a 401 error response.
{
"error": 1,
"message": "invalid credentials"
}You can log in as a user by making an HTTP POST call to the following route
http://localhost:8000/api/loginAPI Payload & Response
You can send a Form Multipart or a JSON payload like this
{
"email":"[email protected]",
"password":"Surprisingly A Good Password"
}You will get a JSON response with user token. You need this user token for making any calls to other routes protected by user ability.
{
"error": 0,
"token": "2|u0ZUNlNtXgdUmtQSACRU1KWBKAmcaX8Bkhd2xVIf"
}For any unsuccessful attempt, you will receive a 401 error response.
{
"error": 1,
"message": "invalid credentials"
}To list the users, make an HTTP GET call to the following route, with Admin Token obtained from Admin Login. Add this token as a standard Bearer Token to your API call.
http://localhost:8000/api/usersAPI Payload & Response
No payload is required for this call.
You will get a JSON response with all users available in your project.
[
{
"id": 1,
"name": "Hydra Admin",
"email": "[email protected]"
},
{
"id": 2,
"name": "Test User",
"email": "[email protected]"
},
]For any unsuccessful attempt or wrong token, you will receive a 401 error response.
{
"message": "Unauthenticated."
}Make an HTTP PUT request to the following route to update an existing user. Replace {userId} with actual user id. You must include a Bearer token obtained from User/Admin authentication. A bearer admin token can update any user. A bearer user token can only update the authenticated user by this token.
http://localhost:8000/api/users/{userId}For example, to update the user with id 3, use this endpoint http://localhost:8000/api/users/3
API Payload & Response
You can include name or email, or both in a URL Encoded Form Data or JSON payload, just like this
{
"name":"Captain Cook",
"email":"[email protected]"
}You will receive the updated user if the bearer token is valid.
{
"id": 3,
"name": "Captain Cook",
"email": "[email protected]",
}For any unsuccessful attempt with an invalid token, you will receive a 401 error response.
{
"error": 1,
"message": "invalid credentials"
}If a bearer user token attempts to update any other user but itself, a 409 error response will be delivered
{
"error": 1,
"message": "Not authorized"
}For any unsuccessful attempt with an invalid user id, you will receive a 404 not found error response. For example, when you are trying to delete a non-existing user with id 16, you will receive the following response.
{
"error": 1,
"message": "No query results for model [App\\Models\\User] 16"
}To delete an existing user, make a HTTP DELETE request to the following route. Replace {userId} with actual user id
http://localhost:8000/api/users/{userId}For example to delete the user with id 2, use this endpoint http://localhost:8000/api/users/2
API Payload & Response
No payload is required for this call.
If the request is successful and the bearer token is valid, you will receive a JSON response like this
{
"error": 0,
"message": "user deleted"
}You will receive a 401 error response for any unsuccessful attempt with an invalid token.
{
"error": 1,
"message": "invalid credentials"
}For any unsuccessful attempt with an invalid user id, you will receive a 404 not found error response. For example, you will receive the following response when you try to delete a non-existing user with id 16.
{
"error": 1,
"message": "No query results for model [App\\Models\\User] 16"
}To list the roles, make an HTTP GET call to the following route, with Admin Token obtained from Admin Login. Add this token as a standard Bearer Token to your API call.
http://localhost:8000/api/rolesAPI Payload & Response
No payload is required for this call.
You will get a JSON response with all the
No open issues yet, or sync has not completed.