Skip to main content

Session management

After a user has logged in, Ory creates a session cookie that your application can use to verify the user's authentication status. This guide shows how to work with sessions in your application.

Checking session status

You'll need to verify if a user is authenticated before allowing access to protected resources. Here's how to implement session verification:

  1. Verify the session

    Check if the user has a valid session cookie

  2. Access identity information

    Retrieve user details from the session

  3. Handle unauthenticated users

    Redirect to login if no valid session exists

Session verification with express.js

// Using the Ory SDK instance from the sign-in guide

// Middleware to verify sessions
const requireAuth = async (req, res, next) => {
try {
const { data: session } = await ory.toSession({
cookie: req.header("cookie"),
})

// Store session in request for use in route handlers
req.session = session
next()
} catch (error) {
// No valid session, redirect to Ory login UI
res.redirect(`${basePath}/ui/login`)
}
}

Protecting routes

Common patterns for protecting routes in your application:

// Using the requireAuth middleware defined above

// Apply the middleware to routes that need protection
app.get("/dashboard", requireAuth, (req, res) => {
// Access user data from the session
const user = req.session.identity
res.render("dashboard", { user })
})

app.get("/settings", requireAuth, (req, res) => {
res.render("settings", { user: req.session.identity })
})

Configuring session settings in Ory Console

You can configure various session-related settings through the Ory Console. To access these settings:

  1. Log in to your Ory Console
  2. Select your workspace and project
  3. Navigate to the Authentication tab
  4. Click on Sessions in the sidebar

Session lifespan

Session lifespan defines how long a user will remain authenticated after they sign in.

Session lifespan settings

By default, sessions expire after 24 hours (72h0m0s). You can adjust this value based on your security requirements:

  • Shorter lifespans (e.g., 1-4 hours) provide higher security but require users to log in more frequently
  • Longer lifespans (e.g., 30 days) improve user experience but may increase security risks

Privileged sessions

Privileged sessions allow users to change sensitive settings (like passwords, adding a second factor, or changing email) in their profile. This setting controls how long a session is considered privileged after a user signs in or completes a secondary authentication challenge.

Privileged sessions settings

Privileged sessions typically have a shorter lifespan than regular sessions. By default, a session is considered privileged for 15 minutes after login. When a user attempts to perform a sensitive action after this period, they'll be prompted to re-authenticate.

These settings control how session cookies are handled in the browser.

Session cookie settings

  • Persist sessions: If enabled, the session cookie will be persisted across browser restarts
  • Same Site: Controls the same-site attribute of session cookies, which affects how cookies are sent in cross-site requests

Session properties

Ory sessions have several important time properties:

PropertyDescription
issued_atWhen the session was created
authenticated_atWhen the user was authenticated
expires_atWhen the session will expire
activeWhether the session is still active

When checking sessions in your application code, you can use these properties to implement additional security measures, such as forcing re-authentication for sensitive operations if the session is old.

Next steps

Now that you've learned how to manage user sessions, you can:

  1. Implement Multi-factor Authentication
  2. Add Password Reset Flows
  3. Set Up Email Verification
  4. Explore OpenID Connect Integration