Single Sign-On (SSO)
Learn how to implement custom Single Sign-On providers in VitNode
Custom SSO Provider
This guide explains how to create and implement custom SSO providers in VitNode applications.
Create plugin function
Start by creating a new file with a function that implements the SSOApiPlugin
type, which requires id
and name properties at minimum.
getUrl
method
Implement the getUrl
method inside your function to generate the OAuth authorization URL for your SSO provider.
This URL should include appropriate scope
parameters to request access to the user's id
, email
, and username
(if available).
State Parameter Security
Always include the state
parameter to prevent CSRF attacks. VitNode handles
this for you when you use the provided parameters.
fetchToken
method
Next, implement the fetchToken
method to exchange the authorization code for an access token:
fetchUser
method
Finally, implement the fetchUser
method to retrieve the user's profile information using the access token:
Email Verification
Some OAuth2 providers return email addresses that may not be verified. If the
API provides verification status (like verified_email
), add it to your
schema validation to ensure you're only accepting verified emails.
Register the SSO Plugin
The final step is to register your SSO plugin in the API route configuration:
Environment Variables
Remember to add the required environment variables (DISCORD_CLIENT_ID
and
DISCORD_CLIENT_SECRET
) to your project configuration.