Quick Start
This page shows the fastest ways to try Rix Auth with Vix.cpp.
You can use Rix in two ways:
1. Run a single C++ file with vix run
2. Use Rix inside a Vix project with vix.appUse the single-file workflow when you want to test Rix quickly.
Use the project workflow when you want a real application, library, tests, dependencies, and a reproducible build.
Option 1: Run a single file
Create a small working folder:
mkdir -p ~/rix-auth-quick-start
cd ~/rix-auth-quick-startCreate a file:
touch auth.cppAdd this code:
#include <rix.hpp>
int main()
{
rix.debug.print("== Rix Auth quick start ==");
auto auth = rix.auth.memory();
auto registered = auth.register_user({"ada@example.com","correct-password"});
if (registered.failed())
{
const auto &error = registered.error();
rix.debug.eprint(
"auth error:",
rix.auth.error.to_string(error),
error.message()
);
return 1;
}
rix.debug.print("registered user");
rix.debug.print("id:", registered.value().id());
rix.debug.print("email:", registered.value().email());
auto login = auth.login({"ada@example.com","correct-password"});
if (login.failed())
{
const auto &error = login.error();
rix.debug.eprint(
"auth error:",
rix.auth.error.to_string(error),
error.message()
);
return 1;
}
rix.debug.print("----------------------------------------");
rix.debug.print("login successful");
rix.debug.print("user:", login.value().user.email());
rix.debug.print("session:", login.value().session.id());
rix.debug.print("token issuer:", login.value().token.issuer());
auto session = auth.authenticate_session(login.value().session.id());
if (session.failed())
{
const auto &error = session.error();
rix.debug.eprint(
"auth error:",
rix.auth.error.to_string(error),
error.message()
);
return 1;
}
rix.debug.print("----------------------------------------");
rix.debug.print("OK:", "session authenticated");
rix.debug.print("session user id:", session.value().user_id());
return 0;
}Run the file:
vix run auth.cppIf Rix is already available in your Vix setup, this is enough.
Install Rix for single-file usage
If the header is not found, install the Rix facade globally:
vix install -g rix/rixThen run again:
vix run auth.cppGlobal installation is useful for quick local experiments.
For real projects, prefer project dependencies with vix add and vix install.
Expected output
The output should look like this:
== Rix Auth quick start ==
registered user
id: user_...
email: ada@example.com
----------------------------------------
login successful
user: ada@example.com
session: session_...
token issuer: rix/auth
----------------------------------------
OK: session authenticated
session user id: user_...The exact user id and session id will be different each time.
Rix Auth generates secure random ids.
Option 2: Use a Vix project
Create a Vix application:
vix new hello-rix --app
cd hello-rixIf the project has .env.example, create your local .env file:
cp .env.example .envAdd Rix:
vix add rix/rix
vix installOpen:
vix.appAdd Rix to the deps array:
deps = [
"rix/rix",
]A small vix.app can look like this:
name = "hello-rix"
type = "executable"
standard = "c++20"
output_dir = "bin"
sources = [
"src/main.cpp",
]
include_dirs = [
"include",
"src",
]
deps = [
"rix/rix",
]
packages = [
"vix",
]
links = [
"vix::vix",
]
resources = [
".env=.env",
]The important part is:
deps = [
"rix/rix",
]deps is for Vix Registry packages.
Do not put Rix packages in packages.
Add the code
Open:
src/main.cppUse the same code from the single-file example.
Then build:
vix buildRun:
vix runOption 3: Use Rix inside a library
Rix can also be used inside a reusable C++ library.
Create a library:
vix new mylib --lib
cd mylibAdd Rix:
vix add rix/rix
vix installIf the library has a vix.app target for an example, demo, or test executable, declare Rix in deps:
deps = [
"rix/rix",
]Then include the facade:
#include <rix.hpp>Example:
#include <rix.hpp>
namespace mylib
{
inline void hello()
{
rix.debug.print("Hello from mylib");
}
}Use this only when your library intentionally depends on the Rix facade.
If your library needs only one Rix package, prefer the independent package.
For Auth only:
deps = [
"rix/auth",
]What the code does
The example includes the unified Rix facade:
#include <rix.hpp>Then it creates a memory-backed authentication service:
auto auth = rix.auth.memory();Memory auth is useful for examples, tests, and local development.
It stores users and sessions in memory.
Data is lost when the process exits.
Register a user
This creates a user:
auto registered = auth.register_user({"ada@example.com","correct-password"});Registration validates the email, checks the password policy, hashes the password, creates a user id, and stores the user.
Always check the result:
if (registered.failed())
{
const auto &error = registered.error();
rix.debug.eprint(
"auth error:",
rix.auth.error.to_string(error),
error.message()
);
return 1;
}Login
This authenticates the user:
auto login = auth.login({"ada@example.com","correct-password"});A successful login returns:
login.value().user
login.value().session
login.value().tokenThe user contains the authenticated identity.
The session represents server-side login state.
The token is a short-lived value attached to the user.
Authenticate the session
After login, the session can be checked:
auto session = auth.authenticate_session(login.value().session.id());A usable session must be:
valid
not revoked
not expiredIf the session is valid, the application can trust the attached user id:
session.value().user_id()Error handling
Rix Auth uses explicit results.
Operations that return values use result objects:
auto result = auth.login(...);
if (result.failed())
{
const auto &error = result.error();
}Operations that only return success or failure use status objects:
auto status = auth.logout(session_id);
if (status.failed())
{
const auto &error = status.error();
}Use:
rix.auth.error.to_string(error)to convert an auth error to a stable string.
Use:
error.message()for a human-readable diagnostic message.
Try a failed login
Change the login password:
auto login = auth.login({"ada@example.com","wrong-password"});Run the file again:
vix run auth.cppOr, inside a project:
vix runThe login should fail with an authentication error.
This is normal.
Invalid credentials are returned as explicit errors.
Use database auth later
The quick start uses memory auth:
auto auth = rix.auth.memory();For a real application, use database-backed auth:
auto db = vix::db::Database::sqlite("storage/app.db");
auto auth = rix.auth.database(db);Memory auth is for temporary storage.
Database auth is for durable users and sessions.
What you should remember
For the fastest experiment, create a file and run it:
mkdir -p ~/rix-auth-quick-start
cd ~/rix-auth-quick-start
touch auth.cpp
vix run auth.cppIf Rix is not available yet, install it globally:
vix install -g rix/rixFor a real project, add Rix locally:
vix add rix/rix
vix installThen declare it in vix.app:
deps = [
"rix/rix",
]Use the public facade:
#include <rix.hpp>Create memory auth:
auto auth = rix.auth.memory();Register and login:
auth.register_user(...);
auth.login(...);Always check results before using values.
Next step
Learn how to use the Rix facade clearly.
Next: Use the Rix Facade