The architecture of my app is currently set up with functions that are scoped to specific modules, such as "Auth" for instance:
async function authenticate(email) {
let authenticated = false;
try {
if (email) {
if (!validEmail(email)) {
console.error('Invalid email');
} else {
await APIAuthenticate(email);
authenticated = true;
}
}
} catch (error) {
console.error(error)
}
return authenticated;
}
One inconvenience I face is the need for the authenticated
variable. It's necessary because I want to use the authenticate
function in a separate view file to redirect the user once authentication is successful without directly handling routing logic within authenticate
.
Currently, I have to manually track the authenticated
status like this:
// MyView.js
const isLoggedIn = await authenticate("<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="c6b5a9aba386a3aba7afaae8a5a9ab">[email protected]</a>");
if (isLoggedIn) {
router.push("/dashboard")
}
While the current setup works, I'm wondering if there is a better solution that doesn't require me to manually manage the authenticated
status within the authenticate()
function?