Skip to main content

Setting up oauth2 client configuration

This guide shows how to initialize your application to work with Ory's OAuth2 server. You'll configure the basic setup required before implementing authentication flows.

1. Install dependencies

npm install express-session openid-client

2. Initialize the OpenID Connect client

This code sets up the OpenID client by discovering the Ory OAuth2 server endpoints and configuring your client credentials.

index.js
const session = require("express-session")
const client = require("openid-client")
const app = express()

// Session setup to store PKCE and state values
app.use(
session({
secret: "your-session-secret",
resave: false,
saveUninitialized: true,
}),
)

// Configure environment variables
const ORY_PROJECT_SLUG = "your-project-slug"
const OAUTH_CLIENT_ID = "your-client-id"
const OAUTH_CLIENT_SECRET = "your-client-secret"

// Initialize OpenID client configuration
let config
;(async () => {
try {
// Create server URL (Ory's issuer URL)
const server = new URL(`https://${ORY_PROJECT_SLUG}.projects.oryapis.com`)

// Use discovery to fetch the server metadata and create a configuration
config = await client.discovery(
server,
OAUTH_CLIENT_ID,
OAUTH_CLIENT_SECRET,
client.ClientSecretBasic(OAUTH_CLIENT_SECRET),
)

console.log("Discovery successful")
} catch (error) {
console.error("Discovery error:", error)
}
})()