Authentication


Authentication is performed through the Login and Logout functions.

It is an HTTPS POST request that will include the username, password, and the accountId we're logging into.

Wherever a sessionId is accepted you can pass either a session token from a login or a long-lived access token. The library authenticates every request with Authorization: Bearer <token>, so it does not need to know which kind you hold.

Typescript
Javascript
import { Api, ENVIRONMENT, Session } from "bruce-models";

// Setup global defaults.
ENVIRONMENT.PARAMS = {
    ...ENVIRONMENT.PARAMS,
    accountId: "your-account-id"
};

Session.Login({
    // Login to your specific account, this will not fallback to a default.
    accountId: "your-account-id",
    // Username can be your user's ID, email, or username.
    username: "your-username",
    password: "your-password"
}).then((data) => {
    console.log("Login result", data);

    const ssid = data.session.ID;

    // Record this inside internal storage or other to retain a session.
    localStorage.setItem("ssid", ssid);

    // Reset the environment to use the session.
    ENVIRONMENT.Reset({
        sessionId: ssid
    });
}).catch((e) => {
    console.log(e);
});

Session.Get({
    // Loaded from internal storage in some way!
    // This accepts a session token or an access token, the library sends either as a bearer token.
    sessionId: "your-token"
}).then((data) => {
    console.log(data);

    // Dead session.
    if (!data.session?.User || data.session.User.ID == "anonymous") {
        return;
    }

    // Reset the environment to use the session.
    // It is good to do this after requesting it instead of straight from your local-storage.
    // That way you know it's valid.
    ENVIRONMENT.Reset({
        sessionId: data.session.ID
    });
}).catch((e) => {
    console.log(e);
});

Here is an example login screen you can reference. This example does not handle the success/failure gracefully and just displays a simple message. You will need to edit it to set your accountId.