Multiple database validation in single application login

I have a application with two different custom database connections.

Each database has different source of users.

I need to connect to database-1, check the user and if user not found, move to database-2, check database-2 to validate the user. If user not found then it can return user not found. if user is found in either of database, it proceeds to login.

I would like to know how can we achieve this flow?