Auth Worker Entrypoint
The Auth Worker provides authentication functionality, including session verification and OAuth integration. This worker streamlines the process of verifying user authentication and managing user sessions.
AuthWorker Entrypoint
verifyAuth(oauthId, properties = null)
Verifies a user's authentication session and checks optional user properties.
Parameters:
oauthId: The OAuth ID associated with the user session. Ifnullor invalid, the user is considered unauthenticated.properties(optional): A query object specifying conditions to validate against the user's data (e.g., role, permissions). Uses a MongoDB-like query syntax.
Returns: An object containing the following keys:
auth: A boolean indicating whether the user is authenticated.session: A boolean indicating whether the session exists.data: (optional) The user's data if authentication is successful.
Details:
- Checks if the provided
oauthIdcorresponds to an active session in the AUTH_SESSIONS KV namespace. - Retrieves the user data associated with the session from the AUTH_USERS KV namespace.
- If
propertiesis provided, validates the user data against the specified conditions using the Utils Worker'sevaluateQueryfunction. - Returns a result object indicating the authentication status and user data (if authenticated).
Usage Example:
const oauthId = "exampleOAuthId12345";
const requiredProperties = { role: { $eq: "admin" } };
const authResult = await AuthWorker.verifyAuth(oauthId, requiredProperties);
if (authResult.auth) {
console.log("User is authenticated:", authResult.data);
} else if (authResult.session) {
console.log("Session exists, but user does not meet property conditions.");
} else {
console.log("User is not authenticated.");
}
Important Notes:
- Sessions are stored with a time-to-live (TTL) in the AUTH_SESSIONS namespace and expire automatically after the designated period.