2. Run it and sign in
Start the server
With your settings loaded (chapter 1):
go run ./cmd/server2026/11/19 10:09:02 Using sqlite store.
2026/11/19 10:09:02 Listening on http://localhost:3000Leave this terminal running and open a second one for the rest of the chapter. Unlike the Node backends, Go doesn't restart by itself when you change a file: press Ctrl+C and run go run ./cmd/server again after each change.
Check it's alive:
curl localhost:3000/health{"status":"ok"}Be a user for a few minutes
You'll use curl to do what a browser would. Two things every request needs:
- A cookie file. After you sign in, the server sends a session cookie.
-b jar -c jarmakes curl keep it in a file calledjarand send it back, like a browser does. - The CSRF header. Requests that change something must carry
X-SelfAuth-CSRF: 1. It proves the request came from your own app and not from another website. How it works explains why.
Rather than typing those options every time, put them in a variable:
H=(-s -H 'Content-Type: application/json' -H 'X-SelfAuth-CSRF: 1' -b jar -c jar)On Windows
These commands are written for bash. The easiest way to run them on Windows is Git Bash, which comes with Git for Windows. In PowerShell, call curl.exe (not curl, which is a different command there) and write the options out in each command.
Register
curl "${H[@]}" localhost:3000/auth/register \
-d '{"email":"maya@example.com","password":"correct horse battery staple"}'{"status":"accepted"}The answer is the same whether or not the email already has an account, so nobody can use this form to find out who's registered.
Verify the email
New accounts must confirm their email before they can sign in. In development, SelfAuth doesn't send email. It prints it in the server's terminal instead. Look there:
Open this link to verify your email address: http://localhost:5173/verify-email#token=-lTTHdiNdyM0GRMB76HyZukeiUVZm7okw-C2T91RyKITry signing in before you verify, to see what happens:
curl "${H[@]}" localhost:3000/auth/login \
-d '{"email":"maya@example.com","password":"correct horse battery staple"}'{"error":{"code":"EMAIL_NOT_VERIFIED","message":"Please verify your email address before signing in.","requestId":"6c2627e4-29fc-4518-b5dd-c722dc82df2b"}}Copy the token from your own terminal, everything after token=, and send it:
curl "${H[@]}" localhost:3000/auth/email/verify -d '{"token":"PASTE_YOUR_TOKEN"}'{"status":"verified"}Sign in
curl "${H[@]}" localhost:3000/auth/login \
-d '{"email":"maya@example.com","password":"correct horse battery staple"}'{"user":{"id":"e930577e-5e70-47cc-8c6b-cb752ca868de","email":"maya@example.com","emailVerified":true,"state":"active","roles":["user"],"permissions":[]}}Maya has the user role, which every new account gets. Her permissions list is empty for now. You'll fill it in chapter 5.
Call a protected endpoint
The project comes with one endpoint of its own, /api/hello, which only signed-in users may call:
curl "${H[@]}" localhost:3000/api/hello{"database":"connected","hello":"maya@example.com"}Gin writes the fields of a response in alphabetical order, so database comes first.
See the CSRF check at work
Sign out, but leave the CSRF header out on purpose:
curl -s -H 'Content-Type: application/json' -b jar -c jar -X POST localhost:3000/auth/logout{"error":{"code":"CSRF_FAILED","message":"Request origin could not be verified.","requestId":"e9bd2b1d-23c1-497a-ac5d-e4d37a7873de"}}This check covers every POST, PUT, PATCH and DELETE in your app, including the endpoints you'll write yourself. Now sign out properly:
curl "${H[@]}" -X POST localhost:3000/auth/logout -o /dev/null -w '%{http_code}\n'204Check it worked
With the session gone, /api/hello refuses you:
curl "${H[@]}" localhost:3000/api/hello{"error":{"code":"UNAUTHENTICATED","message":"Authentication required.","requestId":"0234fe92-9f38-4502-9262-68b3bbd4386a"}}Every error looks like this: a code your code can check, a message for people, and a requestId you can find in the server log. The error codes reference lists them all.
If something went wrong
curl: (7) Failed to connect. The server isn't running, or it's on another port. Look at the first terminal.could not start on port 3000: … address already in use. Another program, perhaps an earlier server, is using the port. Stop it, or setPORTto another number.INVALID_CREDENTIALS. The email or password doesn't match. SelfAuth doesn't say which one, on purpose.RATE_LIMITED. You tried to sign in too many times. The limit is kept in the database, so restarting doesn't reset it. Wait 15 minutes and try again.INVALID_TOKENwhen verifying. You copied only part of the token, or used an older one. Each new registration or resend prints a new link.