My App
Flow

Auth và chọn branch

Flow FE-BE từ login tới khi có branch-scoped accessToken.

Auth và chọn branch

Mục tiêu

Đưa FE từ trạng thái chưa đăng nhập tới trạng thái có branch-scoped accessToken.

Chỉ cover Flow 1 login và Flow 2 select branch. Không cover current-context, setup, database hoặc business APIs.

Trạng thái

ItemTrạng thái
POST /api/auth/loginĐã đóng
POST /api/auth/refreshĐã đóng sau valid session
POST /api/auth/select-branchĐã đóng
GET /api/workspaces/current-contextRoute reachable, Entitlement chưa đóng

GET /api/auth/mePOST /api/auth/logout tồn tại trong Go code nhưng chưa là checkpoint chính của flow này.

Service tham gia

FE
→ API Gateway
→ Identity Service

API Gateway route auth requests tới Identity Service qua route_id=identity-auth.

Token dùng trong flow

TokenDùng khi nàoKhông dùng cho
accountAccessTokenselect/switch branchbusiness/protected APIs
accessTokensau khi có branch contextselect branch
refreshTokenrefresh session/tokengọi business API trực tiếp

refreshToken có thể nằm trong body JSON field refreshToken hoặc HttpOnly cookie tùy client/runtime.

Flow 1 — Login

API sequence

FE
→ POST /api/auth/login
→ API Gateway
→ Identity Service
→ response

Case A — Single branch

  • Input: email, password.
  • Expected response: auth.accessToken, auth.refreshToken, account, workspace, member, branches[], nextAction.type = load_current_context.
  • FE action: lưu accessToken làm active token, lưu account/workspace/member/branches tối thiểu, đi Flow 3 GET /api/workspaces/current-context.

Case B — Multi branch

  • Input: email, password.
  • Expected response: auth.accountAccessToken, auth.refreshToken, account, workspace, member, branches[], nextAction.type = select_branch.
  • FE action: lưu accountAccessToken tạm, render branch selection UI từ branches[], không gọi business/protected APIs.

Login error FE cần xử lý

Error codeFE action
VALIDATION_ERRORHiển thị lỗi field email/password.
INVALID_CREDENTIALSHiển thị login failed, không giữ token cũ.
ACCOUNT_LOCKEDChặn login, hiển thị hướng liên hệ support.
ACCOUNT_DISABLEDChặn login, clear auth state.
WORKSPACE_DISABLED / MEMBER_DISABLEDChặn tiếp tục vào workspace.

Flow 2 — Select branch

Khi nào gọi

Gọi khi login trả auth.accountAccessToken, branches[], nextAction.type = select_branch.

Request dùng branchId từ branches[].

API sequence

FE
→ POST /api/auth/select-branch
→ API Gateway
→ Identity Service
→ response branch-scoped accessToken

FE action sau khi success

  • Lưu auth.accessToken làm active token.
  • Bỏ hoặc hạ quyền accountAccessToken khỏi business request path.
  • Lưu workspace, member, branch đã chọn.
  • Clear branch selection UI state.
  • Đi tiếp Flow 3: GET /api/workspaces/current-context.

Select branch error FE cần xử lý

Error codeFE action
VALIDATION_ERRORbranchId thiếu/null; giữ màn chọn branch.
TOKEN_MISSINGClear auth state, quay lại login.
TOKEN_INVALIDClear auth state hoặc thử refresh nếu policy FE cho phép.
TOKEN_EXPIREDThử refresh; nếu fail thì logout/clear auth state.
BRANCH_NOT_FOUND / BRANCH_DISABLEDReload branch list bằng login/refresh flow.
BRANCH_ACCESS_DENIEDClear selected branch, không retry cùng branchId.

Refresh trong flow này

  • First stale/missing /api/auth/refresh trả 401 là acceptable nếu sau đó login/refresh hợp lệ thành công.
  • Sau valid login/session, refresh should work.
  • Refresh có thể update accessToken, accountAccessToken, refreshToken, branches[] tùy session stage.
  • Nếu refresh fail bằng REFRESH_TOKEN_INVALID, logout/clear auth state.

FE state tối thiểu

StateKhi nào có
accountSau login/refresh success
accountAccessTokenMulti branch stage hoặc refresh account-stage
accessTokenSingle branch login hoặc select branch success
refresh/session cookieSau login/refresh nếu runtime dùng cookie
branchesLogin/refresh response khi cần branch selection
selectedBranchSau select branch success
authStageVí dụ: anonymous, select_branch, branch_ready

Redirect rule

Tình huốngFE action
Single branch login successĐi Flow 3 current-context.
Multi branch login successMở branch selection UI.
Select branch successReplace active token rồi đi Flow 3.
Invalid credentialsỞ lại login, clear token state.
Refresh failedLogout/clear auth state, quay lại login.

Không làm trong flow này

  • Không gọi business API bằng accountAccessToken.
  • Không tự build workspace context ở FE.
  • Không xử lý setup/current-context ở Flow 1/2.
  • Không dùng GET /api/auth/me/branches vì Go router chưa có endpoint này.
  • Không verify JWT trong FE để quyết định backend authorization.

Flow tiếp theo

Sau khi có branch-scoped accessToken, FE gọi:

GET /api/workspaces/current-context

API Gateway route này tới Entitlement Service. Route reachable through Gateway, nhưng Entitlement chưa đóng.

Liên quan

On this page