A Quick Tour with Loco
![](tour.png)
Let's create a blog backend on `loco` in 4 commands. First install `loco-cli` and `sea-orm-cli`:
Now you can create your new app (choose "SaaS
app").
)
)
)
loco:loco
and a db named is the [insert app]_development.
You can use Docker to run a Postgres instance:
When generating a starter, the database name incorporates your application
name and the environment. For instance, if you include myapp
, the database
name in the test.yaml
configuration will be myapp_test
, and in the
development.yaml
configuration, it will be myapp_development
.
A more advanced set of docker-compose.yml
and Dockerfiles
that include Redis and the mailtutan
mailer are available for each starter on GitHub.
Now cd
into your myapp
and start your app:
Adding a CRUD API
We have a base SaaS app with user authentication generated for us. Let's make it a blog backend by adding a post
and a full CRUD API using scaffold
:
Your database have been migrated and model, entities, and a full CRUD controller have been generated automatically.
Start your app:
Next, try adding a post
with curl
:
You can list your posts:
For those counting -- the commands for creating a blog backend were:
cargo install loco-cli
cargo install sea-orm-cli
loco new
cargo loco generate scaffold post title:string content:text
Done! enjoy your ride with loco
🚂
Checking Out SaaS Authentication
Your generated app contains a fully working authentication suite, based on JWTs.
To authenticate, you will need a running redis server.
This docker command starts up a redis server:
Use doctor command to check the needed resources:
)
Registering a New User
The /api/auth/register
endpoint creates a new user in the database with an email_verification_token
for account verification. A welcome email is sent to the user with a verification link.
For security reasons, if the user is already registered, no new user is created, and a 200 status is returned without exposing user email details.
Login
After registering a new user, use the following request to log in:
The response includes a JWT token for authentication, user ID, name, and verification status.
{
}
Get current user
This endpoint is protected by auth middleware.
Check out the source code for controllers/auth.rs
to see how to use the authentication middleware in your own controllers.