arrow_back Back to Tickets

Ticket #e24042

open

Problem

update the return to , if you redirect the user to login before viewing content.

Creator

Marcus

Priority

3

Urgent

not_urgent

Important

important

Project Name

zfrika

Notes

JavaScript function checklogin() { console.log("Link clicked"); const dataToken = sessionStorage.getItem('datatoken'); const username = sessionStorage.getItem('username'); if (dataToken && username) { console.log("Logged in: allowing navigation to href."); return true; // This tells the browser: "Go ahead and follow the href link" } else { console.log("Not logged in: redirecting to login page."); window.location.href = "http://165.232.48.178/zfrikadev/login"; return false; // This tells the browser: "Stop! Do NOT follow the original href" } } Why this works return checklogin(): By adding the word return inside the HTML attribute, you are passing the result of your function (true or false) back to the browser's click event. return true: When the user is logged in, the browser receives true and proceeds to the URL inside your href. return false: When the user is NOT logged in, your function triggers its own redirect to /login and then tells the browser to cancel the original click. Without this, the browser might try to go to both pages at once. 3. A Pro-Tip for Zfrika (Dynamic Redirects) Since you want to convert these users, you should tell the login page where they were trying to go. That way, after they log in, they land right back on the /accountant page. Modified Else Statement: JavaScript else { // Save the current destination so login can send them back here const destination = "<%= BASE_URL_PATH %>/accountant?country=<%= country %>"; window.location.href = "http://165.232.48.178/zfrikadev/login?returnTo=" + encodeURIComponent(destination); return false; } Summary of Changes: Add return inside the onclick="..." attribute. Ensure your function ends with return true for success and return false for failure. Remove the return false; that was hardcoded at the end of your onclick string. Would you like me to show you how to handle the "returnTo" logic in your Login controller so users are automatically sent back to the link they clicked?------ To finish the loop and ensure that the "Login to View" method actually feels seamless for the user, you need to update your Login Controller to handle that returnTo parameter. 1. The Controller Logic (Node.js/Express) When the user submits the login form, your code needs to check if there was a destination saved in the URL. JavaScript // Inside your login POST route exports.postLogin = async (req, res) => { const { username, password } = req.body; const returnTo = req.query.returnTo || '/'; // Default to home if no return path exists try { const user = await User.authenticate(username, password); if (user) { // Log the user in... // Then redirect them back to where they started return res.redirect(returnTo); } } catch (err) { res.render('login', { error: 'Invalid credentials' }); } };

Ticket Information

Ticket ID: 695c6cdfdf2473a6c8e24042
Date Initiated: 1/5/2026, 5:57:00 PM
Status: open
Urgent: not_urgent
Important: important
Created: 1/5/2026, 6:01:03 PM
Last Updated: 1/5/2026, 6:01:03 PM