Squadbase

User Authentication

With Squadbase’s built-in authentication, you can enable user sign-in, management, and role-based access control—without writing any code.

Squadbase provides built-in authentication so that you can handle user sign-in, management, and role-based access control without adding code. For apps whose visibility is not set to Public, only members added to the Squadbase team and invited to the project can access the application.

Adding members

To give someone access to an app deployed on Squadbase, follow these steps:

  1. From the dashboard Settings page, invite the member to your Team.
  2. The member accepts the invitation and joins the team.
  3. From the Project settings page, add the member to the project and assign a project role.

Managing member permissions

Squadbase offers two scopes of roles:

  • Team roles – govern actions at the team level.
  • Project roles – govern actions within a specific project.

Configuring team roles

Team roles are managed from the dashboard’s Settings page. Available roles:

  • Admin – Can change settings for any project.
  • Developer – Can create new projects, invite team members, and change project settings.
  • Contributor – Can deploy to projects they’ve been invited to.
  • Viewer – Can view projects they’ve been invited to.

Configuring project roles

Project roles are managed in the Settings tab of each project’s dashboard page.

Project role

You can use any string you like for a project role. In code, you can access the assigned roles like this:

import streamlit as st
import squadbase.streamlit as sq

user_info = sq.auth.get_user()
st.write(f"Hello, {user_info['firstName']} {user_info['lastName']}")

if "admin" in user_info['roles']:
  st.write("You are an admin")
else:
  st.write("You are not an admin")
import { createNextjsServerClient } from "@squadbase/nextjs";

// In your Next.js server component or API route
const client = createNextjsServerClient({
  projectId: "your-project-id",
});

// Get the current authenticated user
const user = await client.getUser();
console.log(user);
// {
//   username: string,
//   email: string,
//   firstName: string,
//   lastName: string,
//   iconUrl: string | null,
//   roles: string[]
// }
import { createServerClient } from "@squadbase/server";

const client = createServerClient({
  projectId: "your-project-id",
  cookieOptions: {
    getCookie: () => {
      // Implement your cookie retrieval logic here
      // This should return the session cookie string
    },
  },
});

// Get the current authenticated user
const user = await client.getUser();
console.log(user);
// {
//   username: string,
//   email: string,
//   firstName: string,
//   lastName: string,
//   iconUrl: string | null,
//   roles: string[]
// }
import requests

def get_user_info_from_cookie(cookie):
    subdomain = "your-subdomain"

    # Construct the URL for the POST request
    url = f"https://{subdomain}.squadbase.app/_sqcore/auth"
    token = cookie.get("__Host-squadbase-session")
    headers = {
        "Authorization": f"Bearer {token}"
    }
    response = requests.post(url, headers=headers)
    if response.status_code == 200:
        return response.json()
    else:
        return {"error": "Failed to retrieve user information"}

cookie = {"__Host-squadbase-session": "your-auth-token"}
user_info = get_user_info_from_cookie(cookie)
print(user_info)

Important: Retrieving user information only works after the app has been deployed to Squadbase.

For local development, provide mock data:

import streamlit as st
import squadbase.streamlit as sq

mock_user_data = {
    "username": "testuser",
    "firstName": "Test",
    "lastName": "User",
    "iconUrl": None,
    "email": "test@example.com",
    "roles": ["admin"]
}

user_info = sq.auth.get_user(mock_data=mock_user_data)
st.write(f"Hello, {user_info['firstName']} {user_info['lastName']}")
if "admin" in user_info['roles']:
  st.write("You are an admin")
else:
  st.write("You are not an admin")
const client = createNextjsServerClient({
  projectId: "your-project-id",
  mockUser: {
    username: "test-user",
    email: "test@example.com",
    firstName: "Test",
    lastName: "User",
    iconUrl: null,
    roles: ["user"],
  },
});
const client = createServerClient({
  projectId: "your-project-id",
  cookieOptions: {
    getCookie: () => undefined,
  },
  mockUser: {
    username: "test-user",
    email: "test@example.com",
    firstName: "Test",
    lastName: "User",
    iconUrl: null,
    roles: ["user"],
  },
});
import requests

mock_user_data = {
    "username": "testuser",
    "firstName": "Test",
    "lastName": "User",
    "iconUrl": None,
    "email": "test@example.com",
    "roles": ["admin"]
}

def get_user_info_from_cookie(cookie):
    subdomain = "your-subdomain"

    # Construct the URL for the POST request
    url = f"https://{subdomain}.squadbase.app/_sqcore/auth"
    token = cookie.get("__Host-squadbase-session")
    if token is None:
        return mock_user_data
    headers = {
        "Authorization": f"Bearer {token}"
    }
    response = requests.post(url, headers=headers)
    if response.status_code == 200:
        return response.json()
    else:
        return {"error": "Failed to retrieve user information"}

cookie = {"__Host-squadbase-session": "your-auth-token"}
user_info = get_user_info_from_cookie(cookie)
print(user_info)

For details on the authentication API, see REST API. SDKs are also available for each framework: