Squadbase

ユーザー認証

Squadbaseでは組み込みのユーザー認証によって、コードの追加なしでユーザー認証と管理、ロールベースのアクセスコントロールを実現できます。

Squadbaseにデプロイされたアプリは自動的にユーザー認証とロールベースのアクセス制御で保護されます。 公開設定がPublicではないアプリでは、Squadbaseのチームに追加されたメンバーのうち、プロジェクトに招待されたメンバーのみがアプリケーションにアクセスできます。

メンバーの追加

Squadbaseにデプロイされたアプリにアクセスできるメンバーを追加するには、以下の手順を実行します。

  1. ダッシュボードの設定画面から、チームにメンバーを招待する
  2. メンバーがチームに参加する
  3. プロジェクトの設定画面から、プロジェクトにメンバーを追加する。この際、プロジェクトロールを設定できます。

メンバーの権限管理

Squadbaseでは、チームスコープの操作を管理する「チームロール」とプロジェクトスコープの操作を管理する「プロジェクトロール」の2種類のロールを提供しています。

チームロールの設定

チームロールはダッシュボードの設定ページから管理することができます。ロールの種別と権限は以下の通りです。

  • Admin: 全てのプロジェクトの設定を変更できる
  • Developer: 新規プロジェクト作成・チームメンバーの招待・プロジェクトの設定変更ができる
  • Contributer: 招待されたプロジェクトにデプロイができる
  • Viewer: 招待されたプロジェクトの閲覧ができる

プロジェクトロールの設定

プロジェクトロールは、ダッシュボードのプロジェクトページ内の設定タブから管理することができます。

Project role

プロジェクトロールには自由な文字列を設定することができます。設定したロールは、以下のようにコードからアクセスすることができます。

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)

このユーザー情報の取得は、Squadbaseにデプロイされた時のみ有効です

そのため、ローカル環境では以下のようにモック情報を設定してください。

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)

ユーザー情報取得用のAPIの詳細については、REST APIをご覧ください。各種フレームワークで使用するためのSDKも提供しています。