In OJS (Open Journal Systems) 3.3, by default, users can only log in using their username, and logging in with an email address is not supported out of the box. However, with a few modifications in the code, you can enable login with either username or email address. This guide will show you how to make these changes step by step (Tested on OJS 3.3.0.19).
Prerequisites
- Access to your OJS installation files via FTP or a control panel file manager.
- Basic knowledge of PHP and HTML.
- Important: Always take a backup of your files before making any changes.
Step 1: Modify the Login Handler (LoginHandler.inc.php
)
- Open the file located at:
lib/pkp/pages/login/LoginHandler.inc.php
- Find the following line of code:
if ($username == null || ($user = $userDao->getByUsername($username)) == null) { $request->redirect(null, null, 'lostPassword'); }
- Replace it with:
if ($username == null) { $request->redirect(null, null, 'lostPassword'); } // Check by username first $user = $userDao->getByUsername($username); // If not found, check by email address if ($user == null) { $user = $userDao->getByEmail($username); } // Redirect to lost password page if user is not found if ($user == null) { $request->redirect(null, null, 'lostPassword'); }
Explanation:
- This change first checks the input as a username.
- If a user is not found, it then checks if the input matches an email address.
- If neither a username nor an email address is found, it redirects to the “lost password” page.
Step 2: Update the Login Form (userLogin.tpl
)
- Open the login form template file at:
lib/pkp/templates/frontend/pages/userLogin.tpl
- Find the following line:
<input type="text" name="username" id="username" value="{$username|escape}" maxlength="32" required aria-required="true">
- Replace it with:
<input type="text" name="username" id="username" value="{$username|escape}" maxlength="255" placeholder="Username / Email" required aria-required="true">
Explanation:
- We increased the
maxlength
to255
to accommodate longer email addresses. - The
placeholder
text was updated to indicate that users can enter either their username or email.
Step 3: Modify the User DAO (UserDAO.inc.php
)
- Open the following file:
lib/pkp/classes/user/UserDAO.inc.php
- Locate the
getByUsername
function, which looks like this:function getByUsername($username, $allowDisabled = true) { $result = $this->retrieve( 'SELECT * FROM users WHERE username = ?' . ($allowDisabled ? '' : ' AND disabled = 0'), [$username] ); $row = (array) $result->current(); return $row ? $this->_returnUserFromRowWithData($row) : null; }
- Replace it with the following code:
function getByUsername($username, $allowDisabled = true) { // Check by username first $result = $this->retrieve( 'SELECT * FROM users WHERE username = ?' . ($allowDisabled ? '' : ' AND disabled = 0'), [$username] ); $row = (array) $result->current(); // If not found, check by email address if (!$row) { $result = $this->retrieve( 'SELECT * FROM users WHERE email = ?' . ($allowDisabled ? '' : ' AND disabled = 0'), [$username] ); $row = (array) $result->current(); } return $row ? $this->_returnUserFromRowWithData($row) : null; }
Explanation:
- The modified function first attempts to retrieve the user by their username.
- If the username check fails, it then tries to find the user by their email address.
- This approach ensures that users can log in with either their username or email.
Final Step: Clear OJS Cache
After making these changes, you need to clear the cache for the modifications to take effect:
- Go to the Admin Panel in OJS.
- Navigate to Site Administration > Clear Data Caches and Clear Template Cache.
- Alternatively, delete the contents of the
/cache
folder in your OJS installation.
Conclusion
By following the steps above, you can now allow your OJS 3.3 users to log in using either their username or email address. This makes the login process more flexible and user-friendly.
Need Help?
If you encounter any issues or would like assistance in implementing these changes, feel free to contact us. We offer professional support and customization services for OJS to help meet your specific needs.
Comments are closed