Skip to main content

Implementing user login

The login flow authenticates existing users in your application. This guide shows how to implement a secure login process that authenticates users and creates sessions.

Login Flow

const requireAuth = async (req, res, next) => {
try {
const { data: session } = await ory.toSession({ cookie: req.header("cookie") })
req.session = session
next()
} catch (error) {
// No valid session, redirect to login
res.redirect(`${basePath}/ui/login`)
}
}

After successful login

After a successful login, Ory:

  1. Creates a session for the user
  2. Sets a secure session cookie in the browser
  3. Redirects the user to the specified return URL or default location

Your application should then check for the presence of this session cookie to determine if a user is authenticated.

Next steps

Now that you have implemented login functionality, you should:

  1. Add session management
  2. Implement logout functionality
  3. Add password reset capabilities
  4. Explore social login options