协作头脑风暴
brainstorming
在实现前澄清需求、探索设计选择,并形成清晰的开发方向。
auth0-api-skills【开发与工程】Implements Auth0 API functionalities (user CRUD, authentication flows, actions/hooks, organizations/multi-tenancy) for secure identity and access management in web and mobile applications.
独立 GitHub 快照 · 自动分类 · 以原作者文档为准
name: auth0-api-skills description: Implements Auth0 API functionalities (user CRUD, authentication flows, actions/hooks, organizations/multi-tenancy) for secure identity and access management in web and mobile applications. license: MIT compatibility: opencode metadata: version: "1.0.0" domain: security triggers: auth0, authentication, user management, actions, organizations, identity security, auth0 api, jwt tokens archetypes: tactical anti_triggers: - brainstorming - vague ideation response_profile: verbosity: low directive_strength: high abstraction_level: operational role: implementation scope: implementation output-format: code related-skills: auth0-user-management
This skill provides detailed guidance on implementing various functionalities of the Auth0 API, focusing on authentication processes, user management, actions, and organizational structures essential for robust identity and security management.
Initialize Auth0 SDK
Begin by installing the Auth0 SDK in your project. Use npm or yarn:
npm install auth0
Setup Auth0 Configuration
Configure your Auth0 credentials in your application:
const auth0 = new Auth0Client({
domain: 'YOUR_DOMAIN',
client_id: 'YOUR_CLIENT_ID',
redirect_uri: window.location.origin,
});
User Authentication
Create a login function that triggers the Auth0 login dialog:
async function login() {
await auth0.loginWithRedirect();
}
Handle Redirect after Authentication
Once the user is authenticated, handle the redirect and fetch user information:
async function handleRedirect() {
const { appState } = await auth0.handleRedirectCallback();
console.log(appState);
}
Creating a User
Use the following function to create a new user in Auth0:
async function createUser(userData) {
const response = await fetch(`https://YOUR_DOMAIN/api/v2/users`, {
method: 'POST',
headers: {
'Authorization': `Bearer ${YOUR_MANAGEMENT_API_TOKEN}`,
'Content-Type': 'application/json'
},
body: JSON.stringify(userData)
});
return await response.json();
}
Updating a User
Update existing user details as follows:
async function updateUser(userId, updatedData) {
const response = await fetch(`https://YOUR_DOMAIN/api/v2/users/${userId}`, {
method: 'PATCH',
headers: {
'Authorization': `Bearer ${YOUR_MANAGEMENT_API_TOKEN}`,
'Content-Type': 'application/json'
},
body: JSON.stringify(updatedData)
});
return await response.json();
}
Deleting a User
Here’s how to delete a user:
async function deleteUser(userId) {
const response = await fetch(`https://YOUR_DOMAIN/api/v2/users/${userId}`, {
method: 'DELETE',
headers: {
'Authorization': `Bearer ${YOUR_MANAGEMENT_API_TOKEN}`
}
});
return response.status === 204; // No Content
}
Using Auth0 Actions helps automate tasks post-authentication. Here is a simple guide:
Setting Up an Action
Go to Auth0 Dashboard -> Actions -> Library -> Create Action. Name it, and link it to a Trigger.
Writing an Action
Consider a sample action that sends a welcome email:
exports.onExecutePostLogin = async (event, api) => {
await sendWelcomeEmail(event.user.email);
};
Testing the Action
Once created, test the action by logging in to see if the email is sent correctly, and validate through logs.
Organizational management is key for multi-tenant applications. The workflow is as follows:
Create an Organization
Use the API to create an organization:
async function createOrganization(orgData) {
const response = await fetch(`https://YOUR_DOMAIN/api/v2/organizations`, {
method: 'POST',
headers: {
'Authorization': `Bearer ${YOUR_MANAGEMENT_API_TOKEN}`,
'Content-Type': 'application/json'
},
body: JSON.stringify(orgData)
});
return await response.json();
}
Ensure you include necessary details such as name and display_name.
Assign Users to an Organization
Assign users using the following logic:
async function assignUserToOrg(userId, orgId) {
const response = await fetch(`https://YOUR_DOMAIN/api/v2/organizations/${orgId}/members`, {
method: 'POST',
headers: {
'Authorization': `Bearer ${YOUR_MANAGEMENT_API_TOKEN}`,
'Content-Type': 'application/json'
},
body: JSON.stringify({ user_id: userId })
});
return await response.json();
}