setup-auth
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseSet Up Authentication & Authorization
设置认证与授权
Configure authentication (login/logout via Microsoft Entra ID) and role-based authorization for a Power Pages code site. This skill creates an auth service, type declarations, authorization utilities, auth UI components, and role-based access control patterns appropriate to the site's framework.
为Power Pages代码站点配置通过Microsoft Entra ID实现的登录/登出认证以及基于角色的授权。此技能会创建认证服务、类型声明、授权工具、认证UI组件,以及适合站点框架的基于角色的访问控制模式。
Core Principles
核心原则
- Client-side auth is UX only — Power Pages authentication is server-side (session cookies). Client-side role checks control what users see, not what they can access. Server-side table permissions enforce actual security.
- Framework-appropriate patterns — Every auth artifact (hooks, composables, services, directives, guards) must match the detected framework's idioms and conventions.
- Development parity — Include mock data for local development so developers can test auth flows and role-based UI without deploying to Power Pages.
Initial request: $ARGUMENTS
Prerequisites:
- An existing Power Pages code site created via
/create-site- The site must be deployed at least once (
folder must exist).powerpages-site- Web roles must be created via
/create-webroles
- 客户端认证仅用于用户体验 — Power Pages认证是服务器端的(会话Cookie)。客户端角色检查仅控制用户可见内容,而非实际可访问内容。服务器端表权限才是实际安全控制的保障。
- 适配框架的模式 — 所有认证工件(钩子、组合式函数、服务、指令、守卫)必须符合检测到的框架的设计风格与约定。
- 开发一致性 — 包含本地开发用的模拟数据,以便开发者无需部署到Power Pages即可测试认证流程和基于角色的UI。
初始请求:$ARGUMENTS
前置条件:
- 已通过
创建现有的Power Pages代码站点/create-site- 站点至少已部署一次(必须存在
文件夹).powerpages-site- 已通过
创建Web角色/create-webroles
Workflow
工作流程
- Phase 1: Check Prerequisites — Verify site exists, detect framework, check web roles
- Phase 2: Plan — Gather auth requirements and present plan for approval
- Phase 3: Create Auth Service — Auth service with login/logout and type declarations
- Phase 4: Create Authorization Utils — Role-checking functions and wrapper components
- Phase 5: Create Auth UI — Login/logout button integrated into navigation
- Phase 6: Implement Role-Based UI — Apply role-based patterns to site components
- Phase 7: Verify Auth Setup — Validate all auth files exist, build succeeds, auth UI renders
- Phase 8: Review & Deploy — Summary and deployment prompt
- 阶段1:检查前置条件 — 验证站点是否存在、检测框架、检查部署状态和Web角色
- 阶段2:规划 — 收集认证需求并提交方案供审批
- 阶段3:创建认证服务 — 包含登录/登出功能和类型声明的认证服务
- 阶段4:创建授权工具 — 角色检查函数和包装组件
- 阶段5:创建认证UI — 集成到导航栏的登录/登出按钮
- 阶段6:实现基于角色的UI — 为站点组件应用基于角色的模式
- 阶段7:验证认证设置 — 验证所有认证文件是否存在、构建是否成功、认证UI是否正常渲染
- 阶段8:审核与部署 — 总结工作并提示部署
Phase 1: Check Prerequisites
阶段1:检查前置条件
Goal: Confirm the project exists, identify the framework, verify deployment status and web roles, and check for existing auth code.
目标: 确认项目存在、识别框架、验证部署状态和Web角色、检查是否存在现有认证代码。
Actions
操作步骤
1.1 Locate Project
1.1 定位项目
Look for in the current directory or immediate subdirectories:
powerpages.config.jsontext
**/powerpages.config.jsonIf not found: Tell the user to create a site first with .
/create-site在当前目录或直接子目录中查找:
powerpages.config.jsontext
**/powerpages.config.json若未找到:告知用户需先通过创建站点。
/create-site1.2 Detect Framework
1.2 检测框架
Read to determine the framework (React, Vue, Angular, or Astro). See for the full framework detection mapping.
package.json${CLAUDE_PLUGIN_ROOT}/references/framework-conventions.md读取以确定框架(React、Vue、Angular或Astro)。完整的框架检测映射请参考。
package.json${CLAUDE_PLUGIN_ROOT}/references/framework-conventions.md1.3 Check Deployment Status
1.3 检查部署状态
Look for the folder:
.powerpages-sitetext
**/.powerpages-siteIf not found: Tell the user the site must be deployed first:
"Thefolder was not found. The site needs to be deployed at least once before authentication can be configured.".powerpages-site
Use :
AskUserQuestion| Question | Options |
|---|---|
| Your site needs to be deployed first. Would you like to deploy now? | Yes, deploy now (Recommended), No, I'll do it later |
If "Yes, deploy now": Invoke , then resume.
/deploy-siteIf "No": Stop — the site must be deployed first.
查找文件夹:
.powerpages-sitetext
**/.powerpages-site若未找到:告知用户需先部署站点:
"未找到文件夹。在配置认证前,站点至少需部署一次。".powerpages-site
使用:
AskUserQuestion| 问题 | 选项 |
|---|---|
| 您的站点需要先完成部署。是否现在进行部署? | 是,立即部署(推荐), 否,我稍后自行部署 |
若选择“是,立即部署”:调用,之后继续流程。
/deploy-site若选择“否”:终止流程 — 站点必须先完成部署。
1.4 Check Web Roles
1.4 检查Web角色
Look for web role YAML files in :
.powerpages-site/web-roles/text
**/.powerpages-site/web-roles/*.ymlRead each file and compile a list of existing web roles (name, id, flags).
If no web roles exist: Warn the user that web roles are needed for authorization. Ask if they want to create them first:
| Question | Options |
|---|---|
| No web roles were found. Web roles are required for role-based authorization. Would you like to create them now? | Yes, create web roles first (Recommended), Skip — I'll add roles later |
If "Yes": Invoke , then resume.
/create-webrolesIf "Skip": Continue — auth service and login/logout will still work, but role-based authorization will need roles created later.
在中查找Web角色YAML文件:
.powerpages-site/web-roles/text
**/.powerpages-site/web-roles/*.yml读取每个文件并编译现有Web角色列表(名称、ID、标记)。
若不存在Web角色:警告用户授权需要Web角色。询问用户是否要先创建:
| 问题 | 选项 |
|---|---|
| 未找到Web角色。基于角色的授权需要Web角色。是否要先创建Web角色? | 是,先创建Web角色(推荐), 跳过 — 我稍后自行添加角色 |
若选择“是”:调用,之后继续流程。
/create-webroles若选择“跳过”:继续流程 — 认证服务和登录/登出功能仍可正常工作,但基于角色的授权需后续创建角色后才能使用。
1.5 Check for Existing Auth Code
1.5 检查现有认证代码
Search for existing auth files to avoid duplicating work:
- or
src/services/authService.tssrc/services/authService.js src/types/powerPages.d.ts- or
src/utils/authorization.tssrc/utils/authorization.js - Auth components (e.g., ,
AuthButton.tsx)AuthButton.vue
If auth files already exist, present them to the user and ask whether to overwrite or skip.
搜索现有认证文件以避免重复工作:
- 或
src/services/authService.tssrc/services/authService.js src/types/powerPages.d.ts- 或
src/utils/authorization.tssrc/utils/authorization.js - 认证组件(如、
AuthButton.tsx)AuthButton.vue
若已存在认证文件,展示给用户并询问是否覆盖或跳过。
Output
输出结果
- Project root path confirmed
- Framework identified (React, Vue, Angular, or Astro)
- Deployment status verified
- Web roles inventory compiled
- Existing auth code conflicts identified (if any)
- 已确认项目根路径
- 已识别框架(React、Vue、Angular或Astro)
- 已验证部署状态
- 已整理Web角色清单
- 已识别现有认证代码冲突(若有)
Phase 2: Plan
阶段2:规划
Goal: Gather authentication requirements from the user and present the implementation plan for approval.
目标: 收集用户的认证需求并提交实现方案供审批。
Actions
操作步骤
2.1 Gather Requirements
2.1 收集需求
Use to determine the scope:
AskUserQuestion| Question | Options |
|---|---|
| Which authentication features do you need? | Login & Logout + Role-based access control (Recommended), Login & Logout only, Role-based access control only (auth service already exists) |
If web roles were found in Phase 1.4, also ask:
| Question | Options |
|---|---|
| Which web roles should have access to protected areas of the site? | (List discovered web role names as options) |
使用确定范围:
AskUserQuestion| 问题 | 选项 |
|---|---|
| 您需要哪些认证功能? | 登录&登出 + 基于角色的访问控制(推荐), 仅登录&登出, 仅基于角色的访问控制(已存在认证服务) |
若在阶段1.4中找到Web角色,额外询问:
| 问题 | 选项 |
|---|---|
| 哪些Web角色应拥有站点受保护区域的访问权限? | (列出已发现的Web角色名称作为选项) |
2.2 Present Plan for Approval
2.2 提交方案供审批
Present the implementation plan inline:
- Which files will be created (auth service, types, authorization utils, components)
- How the auth UI will be integrated into the site's navigation
- Which routes/components will be protected and with which roles
- The site setting that needs to be configured ()
Authentication/Registration/ProfileRedirectEnabled = false
Use to get approval:
AskUserQuestion| Question | Options |
|---|---|
| Here is the implementation plan for authentication and authorization. Would you like to proceed? | Approve and proceed (Recommended), I'd like to make changes |
If "Approve and proceed": Continue to Phase 3.
If "I'd like to make changes": Ask the user what they want to change, revise the plan, and present it again for approval.
直接展示实现方案:
- 将创建的文件(认证服务、类型定义、授权工具、组件)
- 认证UI将如何集成到站点导航中
- 哪些路由/组件将受保护以及对应的角色
- 需要配置的站点设置()
Authentication/Registration/ProfileRedirectEnabled = false
使用获取审批:
AskUserQuestion| 问题 | 选项 |
|---|---|
| 以下是认证与授权的实现方案。是否继续? | 批准并继续(推荐), 我需要修改方案 |
若选择“批准并继续”:进入阶段3。
若选择“我需要修改方案”:询问用户需要修改的内容,修订方案后再次提交审批。
Output
输出结果
- Authentication scope confirmed (login/logout, role-based access, or both)
- Target web roles selected
- Implementation plan approved by user
- 已确认认证范围(登录/登出、基于角色的访问控制,或两者兼具)
- 已选择目标Web角色
- 实现方案已获得用户批准
Phase 3: Create Auth Service
阶段3:创建认证服务
Goal: Create the authentication service, type declarations, and framework-specific auth hook/composable with local development mock support.
Reference:
${CLAUDE_PLUGIN_ROOT}/skills/setup-auth/references/authentication-reference.md目标: 创建认证服务、类型声明,以及支持本地开发模拟数据的框架特定认证钩子/组合式函数。
参考:
${CLAUDE_PLUGIN_ROOT}/skills/setup-auth/references/authentication-reference.mdActions
操作步骤
3.1 Create Type Declarations
3.1 创建类型声明
Create with type definitions for the Power Pages portal object and user:
src/types/powerPages.d.ts- interface —
PowerPagesUser,userName,firstName,lastName,email,contactIduserRoles[] - interface —
PowerPagesPortal,User,version,type,id,geo, etc.tenant - Global interface extension for
WindowMicrosoft.Dynamic365.Portal
创建,包含Power Pages门户对象和用户的类型定义:
src/types/powerPages.d.ts- 接口 —
PowerPagesUser、userName、firstName、lastName、email、contactIduserRoles[] - 接口 —
PowerPagesPortal、User、version、type、id、geo等tenant - 全局接口扩展,添加
WindowMicrosoft.Dynamic365.Portal
3.2 Create Auth Service
3.2 创建认证服务
Create the auth service file based on the detected framework:
All frameworks: Create with these functions:
src/services/authService.ts- — reads from
getCurrentUser()window.Microsoft.Dynamic365.Portal.User - — checks if user exists and has
isAuthenticated()userName - — reads from portal config (
getTenantId())window.Microsoft.Dynamic365.Portal.tenant - — fetches from
fetchAntiForgeryToken()and parses HTML response/_layout/tokenhtml - — creates a form POST to
login(returnUrl?)with:/Account/Login/ExternalLogin- Anti-forgery token from
/_layout/tokenhtml - Provider URL:
https://login.windows.net/{tenantId}/ - Return URL (defaults to current page)
- Anti-forgery token from
- — redirects to
logout(returnUrl?)/Account/Login/LogOff - — prefers full name, falls back to userName
getUserDisplayName() - — for avatar display
getUserInitials()
CRITICAL: Power Pages authentication is server-side (session cookies). The login flow posts a form to the server which redirects to Entra ID. There is no client-side token management. The call gets a CSRF token for the form POST, not a bearer token.
fetchAntiForgeryToken()根据检测到的框架创建认证服务文件:
所有框架:创建,包含以下函数:
src/services/authService.ts- — 从
getCurrentUser()读取用户信息window.Microsoft.Dynamic365.Portal.User - — 检查用户是否存在且拥有
isAuthenticated()userName - — 从门户配置读取(
getTenantId())window.Microsoft.Dynamic365.Portal.tenant - — 从
fetchAntiForgeryToken()获取并解析HTML响应/_layout/tokenhtml - — 向
login(returnUrl?)提交表单POST请求,包含:/Account/Login/ExternalLogin- 来自的防伪造令牌
/_layout/tokenhtml - 提供商URL:
https://login.windows.net/{tenantId}/ - 返回URL(默认为当前页面)
- 来自
- — 重定向到
logout(returnUrl?)/Account/Login/LogOff - — 优先使用全名,回退使用userName
getUserDisplayName() - — 用于头像显示
getUserInitials()
重要提示:Power Pages认证是服务器端的(会话Cookie)。登录流程是向服务器提交表单,再由服务器重定向到Entra ID。不存在客户端令牌管理。调用获取的是表单POST用的CSRF令牌,而非Bearer令牌。
fetchAntiForgeryToken()3.3 Create Framework-Specific Auth Hook/Composable
3.3 创建框架特定的认证钩子/组合式函数
Based on the detected framework:
- React: Create — custom hook returning
src/hooks/useAuth.ts{ user, isAuthenticated, isLoading, displayName, initials, login, logout, refresh } - Vue: Create — composable using
src/composables/useAuth.ts,ref,computedreturning reactive auth stateonMounted - Angular: Create — injectable service with
src/app/services/auth.service.tsfor user stateBehaviorSubject - Astro: Create only (no framework-specific wrapper needed — use the service directly in components)
src/services/authService.ts
根据检测到的框架:
- React:创建— 自定义钩子,返回
src/hooks/useAuth.ts{ user, isAuthenticated, isLoading, displayName, initials, login, logout, refresh } - Vue:创建— 使用
src/composables/useAuth.ts、ref、computed的组合式函数,返回响应式认证状态onMounted - Angular:创建— 可注入服务,使用
src/app/services/auth.service.ts管理用户状态BehaviorSubject - Astro:仅创建(无需框架特定包装 — 直接在组件中使用服务)
src/services/authService.ts
3.4 Add Mock Data for Local Development
3.4 添加本地开发用模拟数据
Auth only works when served from Power Pages (not during local ). Add a development mock pattern in the auth service:
npm run devtypescript
// In development (localhost), return mock user data for testing
const isDevelopment = window.location.hostname === 'localhost' || window.location.hostname === '127.0.0.1';The mock should return a fake user with configurable roles so developers can test role-based UI locally.
认证仅在Power Pages部署环境中生效(本地环境不生效)。在认证服务中添加开发环境模拟模式:
npm run devtypescript
// 在开发环境(localhost)中,返回模拟用户数据用于测试
const isDevelopment = window.location.hostname === 'localhost' || window.location.hostname === '127.0.0.1';模拟数据应返回带有可配置角色的虚拟用户,以便开发者在本地测试基于角色的UI。
Output
输出结果
- created with Power Pages type definitions
src/types/powerPages.d.ts - created with login/logout functions
src/services/authService.ts - Framework-specific auth hook/composable created
- Local development mock data included
- 已创建,包含Power Pages类型定义
src/types/powerPages.d.ts - 已创建,包含登录/登出函数
src/services/authService.ts - 已创建框架特定的认证钩子/组合式函数
- 已添加本地开发用模拟数据
Phase 4: Create Authorization Utils
阶段4:创建授权工具
Goal: Create role-checking utilities and framework-specific authorization components (guards, directives, wrapper components).
Reference:
${CLAUDE_PLUGIN_ROOT}/skills/setup-auth/references/authorization-reference.md目标: 创建角色检查工具和框架特定的授权组件(守卫、指令、包装组件)。
参考:
${CLAUDE_PLUGIN_ROOT}/skills/setup-auth/references/authorization-reference.mdActions
操作步骤
4.1 Create Core Authorization Utilities
4.1 创建核心授权工具
Create with:
src/utils/authorization.ts- — returns array of role names from current user
getUserRoles() - — case-insensitive single role check
hasRole(roleName) - — OR check across multiple roles
hasAnyRole(roleNames) - — AND check across multiple roles
hasAllRoles(roleNames) - — re-exports from auth service
isAuthenticated() - — checks for "Administrators" role
isAdmin() - — checks admin or specified roles
hasElevatedAccess(additionalRoles)
创建,包含:
src/utils/authorization.ts- — 返回当前用户的角色名称数组
getUserRoles() - — 不区分大小写的单角色检查
hasRole(roleName) - — 多角色或检查
hasAnyRole(roleNames) - — 多角色与检查
hasAllRoles(roleNames) - — 从认证服务重新导出
isAuthenticated() - — 检查是否拥有“Administrators”角色
isAdmin() - — 检查是否为管理员或拥有指定角色
hasElevatedAccess(additionalRoles)
4.2 Create Framework-Specific Authorization Components
4.2 创建框架特定的授权组件
Based on the detected framework:
React:
- — renders children only for authenticated users, optional login prompt fallback
src/components/RequireAuth.tsx - — renders children only for users with specified roles, supports
src/components/RequireRole.tsxmoderequireAll - — hook returning
src/hooks/useAuthorization.ts{ roles, hasRole, hasAnyRole, hasAllRoles, isAuthenticated, isAdmin }
Vue:
- — composable with computed roles and role-checking functions
src/composables/useAuthorization.ts - —
src/directives/vRole.tsdirective for declarative role-based visibilityv-role
Angular:
- —
src/app/guards/auth.guard.tswith route data for required rolesCanActivateFn - — structural directive
src/app/directives/has-role.directive.ts*appHasRole="'RoleName'"
Astro:
- only (use directly in component scripts)
src/utils/authorization.ts
根据检测到的框架:
React:
- — 仅为已认证用户渲染子组件,可选登录提示回退内容
src/components/RequireAuth.tsx - — 仅为拥有指定角色的用户渲染子组件,支持
src/components/RequireRole.tsx模式requireAll - — 钩子,返回
src/hooks/useAuthorization.ts{ roles, hasRole, hasAnyRole, hasAllRoles, isAuthenticated, isAdmin }
Vue:
- — 包含计算属性角色和角色检查函数的组合式函数
src/composables/useAuthorization.ts - —
src/directives/vRole.ts指令,用于声明式的基于角色的可见性控制v-role
Angular:
- —
src/app/guards/auth.guard.ts,使用路由数据指定所需角色CanActivateFn - — 结构型指令
src/app/directives/has-role.directive.ts*appHasRole="'RoleName'"
Astro:
- 仅使用(直接在组件脚本中使用)
src/utils/authorization.ts
4.3 Security Reminder
4.3 安全提示
Add a comment at the top of the authorization utilities:
typescript
// IMPORTANT: Client-side authorization is for UX only, not security.
// Server-side table permissions enforce actual access control.
// Always configure table permissions via /integrate-webapi.在授权工具顶部添加注释:
typescript
// 重要提示:客户端授权仅用于用户体验,而非安全控制。
// 服务器端表权限才是实际的访问控制保障。
// 请始终通过`/integrate-webapi`配置服务器端表权限。Output
输出结果
- created with role-checking functions
src/utils/authorization.ts - Framework-specific authorization components created (guards, directives, or wrapper components)
- Security reminder comments included
- 已创建,包含角色检查函数
src/utils/authorization.ts - 已创建框架特定的授权组件(守卫、指令或包装组件)
- 已添加安全提示注释
Phase 5: Create Auth UI
阶段5:创建认证UI
Goal: Create the login/logout button component and integrate it into the site's navigation.
目标: 创建登录/登出按钮组件并集成到站点导航中。
Actions
操作步骤
5.1 Create Auth Button Component
5.1 创建认证按钮组件
Based on the detected framework, create a login/logout button component:
- React: +
src/components/AuthButton.tsxsrc/components/AuthButton.css - Vue:
src/components/AuthButton.vue - Angular: + template + styles
src/app/components/auth-button/auth-button.component.ts - Astro:
src/components/AuthButton.astro
The component should:
- Show a "Sign In" button when the user is not authenticated
- Show the user's display name, avatar (initials-based), and a "Sign Out" button when authenticated
- Include a loading state while checking auth status
- Be styled to match the site's existing design (read existing CSS variables/theme)
根据检测到的框架,创建登录/登出按钮组件:
- React:+
src/components/AuthButton.tsxsrc/components/AuthButton.css - Vue:
src/components/AuthButton.vue - Angular:+ 模板 + 样式
src/app/components/auth-button/auth-button.component.ts - Astro:
src/components/AuthButton.astro
组件应:
- 用户未认证时显示“登录”按钮
- 用户已认证时显示用户显示名称、头像(基于首字母)和“登出”按钮
- 检查认证状态时显示加载状态
- 样式与站点现有设计匹配(读取现有CSS变量/主题)
5.2 Integrate into Navigation
5.2 集成到导航栏
Find the site's navigation component and integrate the auth button:
- Search for the nav/header component in the site's source code
- Import the AuthButton component
- Add it to the navigation bar (typically in the top-right area)
Do NOT replace the existing navigation — add the auth button alongside existing nav items.
找到站点的导航组件并集成认证按钮:
- 在站点源码中搜索导航/头部组件
- 导入AuthButton组件
- 将其添加到导航栏中(通常位于右上角区域)
请勿替换现有导航 — 将认证按钮添加到现有导航项旁。
5.3 Git Commit
5.3 Git提交
Stage and commit the auth files:
powershell
git add -A
git commit -m "Add authentication service and auth UI component"暂存并提交认证文件:
powershell
git add -A
git commit -m "添加认证服务和认证UI组件"Output
输出结果
- Auth button component created for the detected framework
- Auth button integrated into the site's navigation
- Changes committed to git
- 已为检测到的框架创建认证按钮组件
- 已将认证按钮集成到站点导航栏中
- 变更已提交到git
Phase 6: Implement Role-Based UI
阶段6:实现基于角色的UI
Goal: Identify protected content areas and apply role-based authorization patterns to the site's components.
目标: 识别受保护内容区域并为站点组件应用基于角色的授权模式。
Actions
操作步骤
6.1 Identify Protected Content
6.1 识别受保护内容
Analyze the site's components to find content that should be role-gated:
- Admin-only sections (dashboards, settings)
- Authenticated-only content (profile, data views)
- Role-specific features (edit buttons, create forms)
Present findings to the user and confirm which areas to protect.
分析站点组件,找到需要角色权限控制的内容:
- 管理员专属区域(仪表板、设置)
- 仅认证用户可见内容(个人资料、数据视图)
- 角色专属功能(编辑按钮、创建表单)
向用户展示分析结果并确认需要保护的区域。
6.2 Apply Authorization Patterns
6.2 应用授权模式
Based on the user's choices, wrap the appropriate components:
React example:
tsx
<RequireAuth fallback={<p>Please sign in to view this content.</p>}>
<Dashboard />
</RequireAuth>
<RequireRole roles={['Administrators']} fallback={<p>Access denied.</p>}>
<AdminPanel />
</RequireRole>Vue example:
vue
<div v-role="'Administrators'">
<AdminPanel />
</div>Angular example:
typescript
{ path: 'admin', component: AdminComponent, canActivate: [authGuard, roleGuard], data: { roles: ['Administrators'] } }根据用户选择,为相应组件添加包装:
React示例:
tsx
<RequireAuth fallback={<p>请登录以查看此内容。</p>}>
<Dashboard />
</RequireAuth>
<RequireRole roles={['Administrators']} fallback={<p>访问被拒绝。</p>}>
<AdminPanel />
</RequireRole>Vue示例:
vue
<div v-role="'Administrators'">
<AdminPanel />
</div>Angular示例:
typescript
{ path: 'admin', component: AdminComponent, canActivate: [authGuard, roleGuard], data: { roles: ['Administrators'] } }6.3 Git Commit
6.3 Git提交
Stage and commit:
powershell
git add -A
git commit -m "Add role-based access control to site components"暂存并提交:
powershell
git add -A
git commit -m "为站点组件添加基于角色的访问控制"Output
输出结果
- Protected content areas identified and confirmed with user
- Role-based authorization patterns applied to components
- Changes committed to git
- 已识别受保护内容区域并获得用户确认
- 已为组件应用基于角色的授权模式
- 变更已提交到git
Phase 7: Verify Auth Setup
阶段7:验证认证设置
Goal: Validate that all auth files exist, the project builds, and the auth UI renders correctly.
目标: 验证所有认证文件是否存在、项目是否可构建、认证UI是否正常渲染。
Actions
操作步骤
7.1 Verify File Inventory
7.1 验证文件清单
Confirm the following files were created:
- — Power Pages type declarations
src/types/powerPages.d.ts - — Auth service with login/logout functions
src/services/authService.ts - Framework-specific auth hook/composable (e.g., for React)
src/hooks/useAuth.ts - — Role-checking utilities
src/utils/authorization.ts - Framework-specific authorization components (e.g., ,
RequireAuth.tsxfor React)RequireRole.tsx - Auth button component (e.g., for React)
src/components/AuthButton.tsx
Read each file and verify it contains the expected exports and functions:
- Auth service: ,
login,logout,getCurrentUser,isAuthenticatedfetchAntiForgeryToken - Authorization utils: ,
hasRole,hasAnyRole,hasAllRolesgetUserRoles
确认已创建以下文件:
- — Power Pages类型声明
src/types/powerPages.d.ts - — 包含登录/登出函数的认证服务
src/services/authService.ts - 框架特定的认证钩子/组合式函数(如React的)
src/hooks/useAuth.ts - — 角色检查工具
src/utils/authorization.ts - 框架特定的授权组件(如React的、
RequireAuth.tsx)RequireRole.tsx - 认证按钮组件(如React的)
src/components/AuthButton.tsx
读取每个文件并验证其包含预期的导出内容和函数:
- 认证服务:、
login、logout、getCurrentUser、isAuthenticatedfetchAntiForgeryToken - 授权工具:、
hasRole、hasAnyRole、hasAllRolesgetUserRoles
7.2 Verify Build
7.2 验证构建
Run the project build to catch any import errors, type errors, or missing dependencies:
powershell
npm run buildIf the build fails, fix the issues before proceeding.
运行项目构建以捕获导入错误、类型错误或缺失依赖:
powershell
npm run build若构建失败,修复问题后再继续。
7.3 Verify Auth UI Renders
7.3 验证认证UI渲染
Start the dev server and verify the auth button appears in the navigation:
powershell
npm run devUse Playwright to navigate to the site and take a snapshot to confirm the auth button is visible:
- Navigate to
http://localhost:<port> - Take a browser snapshot
- Verify the auth button (Sign In / mock user) appears in the navigation area
If the auth button is not visible or the page has rendering errors, fix the issues.
启动开发服务器并验证认证按钮是否出现在导航栏中:
powershell
npm run dev使用Playwright导航到站点并截图,确认认证按钮可见:
- 导航到
http://localhost:<port> - 截取浏览器截图
- 验证认证按钮(登录/模拟用户)出现在导航区域
若认证按钮不可见或页面存在渲染错误,修复问题。
Output
输出结果
- All auth files verified (present and contain expected exports)
- Project builds successfully
- Auth UI renders correctly in the browser
- 所有认证文件已验证(存在且包含预期导出内容)
- 项目构建成功
- 认证UI在浏览器中正常渲染
Phase 8: Review & Deploy
阶段8:审核与部署
Goal: Create required site settings, present a summary of all work, and prompt for deployment.
目标: 创建所需的站点设置、展示所有工作内容的总结、提示部署。
Actions
操作步骤
8.1 Create Site Setting
8.1 创建站点设置
The site needs the setting set to to prevent Power Pages from redirecting users to a profile page after login (which doesn't exist in code sites).
Authentication/Registration/ProfileRedirectEnabledfalseCheck if exists. If it does, create the site setting file:
.powerpages-site/site-settings/powershell
node "${CLAUDE_PLUGIN_ROOT}/scripts/generate-uuid.js"Create :
.powerpages-site/site-settings/authentication-registration-profileredirectenabled.ymlyaml
id: <UUID from generate-uuid.js>
name: Authentication/Registration/ProfileRedirectEnabled
value: false站点需要将设置为,以防止Power Pages在用户登录后重定向到个人资料页面(代码站点中不存在此页面)。
Authentication/Registration/ProfileRedirectEnabledfalse检查是否存在。若存在,创建站点设置文件:
.powerpages-site/site-settings/powershell
node "${CLAUDE_PLUGIN_ROOT}/scripts/generate-uuid.js"创建:
.powerpages-site/site-settings/authentication-registration-profileredirectenabled.ymlyaml
id: <UUID from generate-uuid.js>
name: Authentication/Registration/ProfileRedirectEnabled
value: false8.2 Record Skill Usage
8.2 记录技能使用情况
Reference:${CLAUDE_PLUGIN_ROOT}/references/skill-tracking-reference.md
Follow the skill tracking instructions in the reference to record this skill's usage. Use .
--skillName "SetupAuth"参考:${CLAUDE_PLUGIN_ROOT}/references/skill-tracking-reference.md
按照参考文档中的技能跟踪说明记录此技能的使用情况。使用。
--skillName "SetupAuth"8.3 Present Summary
8.3 展示总结
Present a summary of everything created:
| Component | File(s) | Status |
|---|---|---|
| Type Declarations | | Created |
| Auth Service | | Created |
| Auth Hook/Composable | | Created |
| Authorization Utils | | Created |
| Auth Components | | Created |
| Auth Button | | Created |
| Site Setting | | Created |
展示所有已创建内容的总结:
| 组件 | 文件 | 状态 |
|---|---|---|
| 类型声明 | | 已创建 |
| 认证服务 | | 已创建 |
| 认证钩子/组合式函数 | | 已创建 |
| 授权工具 | | 已创建 |
| 授权组件 | | 已创建 |
| 认证按钮 | | 已创建 |
| 站点设置 | | 已创建 |
8.4 Ask to Deploy
8.4 询问是否部署
Use :
AskUserQuestion| Question | Options |
|---|---|
| Authentication and authorization are configured. To make login work, the site needs to be deployed. Would you like to deploy now? | Yes, deploy now (Recommended), No, I'll deploy later |
If "Yes, deploy now": Invoke .
/deploy-siteIf "No": Remind the user:
"Remember to deploy your site usingwhen you're ready. Authentication will not work until the site is deployed with the new site settings."/deploy-site
使用:
AskUserQuestion| 问题 | 选项 |
|---|---|
| 认证与授权已配置完成。要使登录功能生效,需要部署站点。是否现在进行部署? | 是,立即部署(推荐), 否,我稍后自行部署 |
若选择“是,立即部署”:调用。
/deploy-site若选择“否”:提醒用户:
"请记得在准备就绪后通过部署站点。在新站点设置部署完成前,认证功能无法正常工作。"/deploy-site
8.5 Post-Deploy Notes
8.5 部署后注意事项
After deployment (or if skipped), remind the user:
- Test on deployed site: Auth only works on the deployed Power Pages site, not on
localhost - Entra ID configuration: The site's identity provider must be configured in the Power Pages admin center to use Microsoft Entra ID
- Assign web roles: Users must be assigned appropriate web roles in the Power Pages admin center
- Table permissions: Client-side auth checks are for UX only — configure server-side table permissions via for actual data security
/integrate-webapi - Local development: The auth service includes mock data for testing on localhost — remove or disable before production
部署完成后(或用户选择跳过部署),提醒用户:
- 在部署站点上测试:认证仅在已部署的Power Pages站点上生效,环境不生效
localhost - Entra ID配置:必须在Power Pages管理中心配置站点的身份提供商以使用Microsoft Entra ID
- 分配Web角色:必须在Power Pages管理中心为用户分配相应的Web角色
- 表权限:客户端认证检查仅用于用户体验 — 请通过配置服务器端表权限以实现实际的数据安全
/integrate-webapi - 本地开发:认证服务包含用于localhost测试的模拟数据 — 生产环境前请移除或禁用
Output
输出结果
- site setting created
ProfileRedirectEnabled - Full summary presented to user
- Deployment prompted (or skipped with reminder)
- Post-deploy guidance provided
- 已创建站点设置
ProfileRedirectEnabled - 已向用户展示完整总结
- 已提示部署(或用户选择跳过并收到提醒)
- 已提供部署后指导
Important Notes
重要说明
Progress Tracking
进度跟踪
Use at the start to track each phase:
TaskCreate| Task | Description |
|---|---|
| Phase 1 | Check Prerequisites — verify site, framework, deployment, web roles |
| Phase 2 | Plan — gather requirements and get user approval |
| Phase 3 | Create Auth Service — auth service, types, framework hook/composable |
| Phase 4 | Create Authorization Utils — role-checking functions and components |
| Phase 5 | Create Auth UI — AuthButton component and navigation integration |
| Phase 6 | Implement Role-Based UI — apply authorization patterns to components |
| Phase 7 | Verify Auth Setup — validate files exist, build succeeds, auth UI renders |
| Phase 8 | Review & Deploy — site setting, summary, deployment prompt |
Update each task with as phases are completed.
TaskUpdate在开始时使用跟踪每个阶段:
TaskCreate| 任务 | 描述 |
|---|---|
| 阶段1 | 检查前置条件 — 验证站点、框架、部署状态、Web角色 |
| 阶段2 | 规划 — 收集需求并获得用户批准 |
| 阶段3 | 创建认证服务 — 认证服务、类型定义、框架钩子/组合式函数 |
| 阶段4 | 创建授权工具 — 角色检查函数和组件 |
| 阶段5 | 创建认证UI — AuthButton组件和导航集成 |
| 阶段6 | 实现基于角色的UI — 为组件应用授权模式 |
| 阶段7 | 验证认证设置 — 验证文件存在、构建成功、认证UI渲染正常 |
| 阶段8 | 审核与部署 — 站点设置、总结、部署提示 |
在完成各阶段后使用更新任务状态。
TaskUpdateKey Decision Points
关键决策点
- Phase 1.3: Deploy now or stop? (site must be deployed before auth setup)
- Phase 1.4: Create web roles now or skip? (roles needed for authorization)
- Phase 1.5: Overwrite or skip existing auth files?
- Phase 2.1: Which auth features to include? (login/logout, role-based, or both)
- Phase 2.2: Approve plan or request changes?
- Phase 6.1: Which content areas to protect with role-based access?
- Phase 8.3: Deploy now or later?
Begin with Phase 1: Check Prerequisites
- 阶段1.3:立即部署还是终止?(配置认证前必须部署站点)
- 阶段1.4:立即创建Web角色还是跳过?(授权需要角色)
- 阶段1.5:覆盖还是跳过现有认证文件?
- 阶段2.1:需要哪些认证功能?(登录/登出、基于角色的访问控制,或两者兼具)
- 阶段2.2:批准方案还是要求修改?
- 阶段6.1:哪些内容区域需要通过角色访问控制保护?
- 阶段8.3:立即部署还是稍后部署?
从阶段1:检查前置条件开始执行