---
title: "Authentication"
section: "Libraries: Web"
route: /webauth
account: {accountId}
bruce_api: https://{accountId}.api.nextspace.host
guardian_api: https://guardian.nextspace.host
---
# 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**

```typescript
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);
});
```

**Javascript**

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

BModels.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.
    BModels.ENVIRONMENT.Reset({
        sessionId: ssid
    });
}).catch((e) => {
    console.log(e);
});

BModels.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.
    BModels.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`.

[JSFiddle example](https://jsfiddle.net/g5oh392q/53/)

---

Urls on this page are resolved for account `{accountId}`.
Site index: https://docs.nextspace.host/llms.txt · whole site in one file: https://docs.nextspace.host/llms-full.txt
Human-readable version of this page: https://docs.nextspace.host/webauth
