AuthenticationHTTP Middleware

HTTP Middleware

If you are using the HTTP API, you can authenticate your requests using the Authorization header.

Client-side: Set the auth token

client.js
// set the auth token
client.auth.token = "YOUR AUTH TOKEN";

If you are using the @colyseus/auth module, this token is managed automatically. See Authentication → Module.

Client-side: Make HTTP requests

The HTTP requests contain the auth token in the Authorization header.

client.js
client.http.get("/profile").then((response) => {
    console.log(response.data);
});

Server-side: Validate the auth token

If you are using the @colyseus/auth module, you can use the auth.middleware() method to validate the authentication token in your routes.

src/app.config.ts
import { auth } from "@colyseus/auth";
// ...
    initializeExpress: (app) => {
        app.use("/profile", auth.middleware(), (req, res) => {
            console.log("authenticated user:", req.auth);
            res.json(req.auth);
        });
    }
// ...
Last updated on