Securing Node.js REST APIs with OAuth2
Instruction: Describe the process of securing a Node.js REST API using OAuth2, including the flow and necessary components.
Context: Candidates should distinguish OAuth clients, authorization servers and resource servers, explain a suitable modern flow, and enforce token validation plus route and object authorization in a Node.js API.
Updated
Official answer available
Read the opening below, then unlock the full answer and practical guidance.
Identify the roles first. The authorization server issues tokens, the client obtains and presents them, and the Node API commonly acts as the resource server. The API does not normally need to implement its own authorization server merely to accept an established provider's access tokens.
For a user-facing client, use the provider's supported authorization-code flow with PKCE and the required redirect/state protections. Do not recommend the implicit or resource-owner-password flow as a default for new systems...