Skip to main content

OAuth Client Implementation

This guide shows how to implement OAuth 2.0 client functionality with Ory. You'll learn how to integrate OpenID Connect (OIDC) to authenticate users through social sign-in providers.

Setting Up OAuth Clients

Before using OAuth/OIDC, you need to create an OAuth client in your Ory project:

  1. Create an OAuth client in Ory Console

    Configure redirect URLs and scopes for your application

  2. Configure OAuth provider

    Set up your chosen identity provider (Google, GitHub, etc.)

  3. Implement OAuth flow in your application

    Add login buttons and handle OAuth redirects

Create OAuth Client in Ory Console

  1. Go to the Ory Console
  2. Navigate to your project
  3. Go to Social Sign-In under Authentication
  4. Click on Add Provider
  5. Select your desired provider (Google, GitHub, etc.)
  6. Enter the required credentials and configure the provider

Implementing Social Sign-In

Add social sign-in buttons to your authentication flows:

// Add this to your login page
app.get("/login", async (req, res) => {
try {
// Initialize login flow
const { data: flow } = await ory.createBrowserLoginFlow({
returnTo: req.query.return_to || "/",
})

// Check for available social providers
const socialProviders = flow.ui.nodes
.filter((node) => node.group === "oidc")
.map((node) => {
// Extract provider details from node attributes
const providerName = node.attributes.value
const providerLabel = node.meta.label?.text || providerName

return {
name: providerName,
label: providerLabel,
url: `${flow.ui.action}?flow=${flow.id}&method=oidc&provider=${providerName}`,
}
})

res.render("login", {
flow,
socialProviders,
csrfToken: flow.ui.nodes.find((n) => n.attributes.name === "csrf_token")?.attributes.value,
})
} catch (err) {
res.status(500).send("Something went wrong")
}
})

Handling OAuth Callback

When a user clicks a social sign-in button, they'll be redirected to the provider's login page and then back to your application. Ory handles the callback automatically, but you need to configure your application to receive the authenticated user:

// No special callback handling is needed - Ory will redirect back to the return_to URL
// Just make sure to check for a valid session

app.get("/oauth-callback", async (req, res) => {
try {
// Verify session after OAuth login
const { data: session } = await ory.toSession({
cookie: req.header("cookie"),
})

// User is authenticated via OAuth
console.log("Successfully authenticated via OAuth. User ID:", session.identity.id)

// Redirect to the original destination or default
const returnTo = req.query.return_to || "/dashboard"
res.redirect(returnTo)
} catch (err) {
// Authentication failed
res.redirect("/login?error=oauth_failed")
}
})

Accessing User Information from OAuth Providers

After authentication, you can access user information provided by the OAuth provider. This information is available in the user's identity traits:

app.get("/profile", requireAuth, (req, res) => {
// req.session was set in the requireAuth middleware
const user = req.session.identity

// Access standard traits
const email = user.traits.email

// Access provider-specific data
// The structure depends on how you've mapped provider data in Ory Console
const providerData = user.metadata_public?.providers || {}

res.render("profile", {
user,
email,
providerData,
})
})

Account Linking

You can enable users to link multiple social accounts to their main account by configuring account linking in Ory Console:

  1. Go to the Ory Console
  2. Navigate to your project settings
  3. Under Social Sign-In, enable Account Linking
  4. Configure the account matching strategy (by email or by provider identifiers)

Best Practices for OAuth Implementation

  1. Always Use HTTPS: OAuth flows require secure connections
  2. State Parameter: Ensure your OAuth flow uses state parameters to prevent CSRF attacks (Ory handles this automatically)
  3. Scopes: Request only the scopes you need for your application
  4. Error Handling: Implement comprehensive error handling for OAuth failures
  5. Account Linking: Consider enabling account linking to allow users to connect multiple providers
  6. Testing: Test the OAuth flow thoroughly, including edge cases and error scenarios

Next Steps

Now that you've implemented OAuth client functionality, you can:

  1. Add more social sign-in providers
  2. Customize identity traits schema
  3. Implement MFA with social sign-in
  4. Configure sign-in with Ory