Ways to verify if a user is authenticated without relying on request.session

I am currently developing a web application using Express, Docker, and following a Three-layered architecture. In my app, I store user login information in a session and have blogposts as a key resource. To retrieve the blogpostId from the database in the Data access layer, I use the following query:

const db = require('./db')

exports.getBlogpostId = function(id ,callback){

    const query = "SELECT * FROM blogposts WHERE blogId = ?"
    const value = [id]
    db.query(query, value, function(error, blogpost){
        if(error){
            callback("DatabaseError", null)
        }else{
            callback(null, blogpost)
        }
    })
}

Now in the Business logic layer, I need to verify whether the user is logged in before accessing blog postId data. Here's how I plan to handle it:

const blogRepo = require('../dal/blog-repository')


exports.getBlogpostId = function(id){

    if(/*Check if the user is logged in*/){
        return blogRepo.getBlogpostId(id)
    }else{
        throw "Unauthorized!"
    }
}

I am looking for some guidance on how to implement the check for user login status in this section of the code. Specifically, how can I retrieve the stored session information from when the user initially logged in?

Thank you!

Answer №1

Within the business layer, there is no inherent knowledge of the user's logged-in status. The focus here is solely on business logic, not web-related functions.

If you wish to integrate such state information, it must be explicitly provided as arguments whenever calling the business layer.

You have two options: either allow the business logic to access the session object in its entirety, or selectively pass only the relevant parts of the session object (such as authentication status) that are required by the business logic.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Developing dynamic web applications with Express.js and enhancing database functionalities with MySQL

Currently, I am working on an application using Node.js and Express framework. While there are numerous examples of data modeling with MongoDB available, my project necessitates the use of a SQL database. If anyone could provide a straightforward explanat ...

Adjust Sidebar Height to Match Document Height (using React Pro Sidebar)

Having an issue with the height of a sidebar component in Next.js using React Pro Sidebar. It seems to be a JavaScript, HTML, and CSS related problem. I've tried several suggested solutions from Stack Overflow, but none of them seem to work. Surprisin ...

Steps for deactivating the submit button once confirming the presence of the username and email

When using AJAX to verify the existence of an email or username in the database, I want to deactivate the submit button if either one (or both) already exist. Currently, I have successfully changed the input border color but I am facing a challenge with t ...

Live search bar feature with jQuery更新

I am currently working on creating a dynamic search bar that updates a list of items from the database based on the input value. Below is the code I have developed for this search bar: $(document).ready(function(){ $('#search').keyup(function ...

Guide on setting cookies in the browser using the header from an Express application

app.js const express = require("express") const cors = require("cors") const bodyParser = require("body-parser"); const app = express(); app.use(express.json()) app.use(cors()) app.post('/getcookies', (req, res)= ...

Potential issue with Jhipster: loading of data could be experiencing delays

I've encountered an issue with getting my chart to display properly. I am working on creating a chart using ng2-charts, where I retrieve data from a service and then display it on the chart. The problem arises when the data is not correctly displayed ...

AngularJS: Issue with scope not updating across several views

Having one controller and two views presents a challenge. ClustersController angular.module('app.controllers').controller('ClustersController', [ '$scope', 'ClustersService', function($scope, ClustersService) { ...

An error occurred while trying to access a property that is undefined, resulting in req.body

Struggling to make a simple POST request in express, but my req.body keeps coming back undefined. I've searched online for solutions to similar issues without success. Here is the HTML form: <form method="POST" class="vote"> ...

Unable to view image when using material-ui CardMedia component

Hello, I've encountered a problem with rendering an image in my application. Let me explain the issue in a simplified manner. So, I have a card component (MyCardComponent) where I need to pass a string prop containing the image file location. The goa ...

When Socket.emit is called, it outputs <span> elements within a well-structured message

Currently working on a small chat application using Node.js, Express.js, and Socket.IO. However, I'm encountering an issue with formatting. Instead of displaying the message content within <span> tags as intended, it is printing out the actual t ...

Email address string loses the '+"' when using AJAX

My ajax code has been working well in most cases, but when I tried using it for updating user details on my page, I noticed that the ""+"" symbol was getting lost if used in an email address (such as <a href="/cdn-cgi/l/email-protection" class ...

Kohana ajax causing removal of JQuery Data attributes

Currently, I am developing an application where jquery data is used to pass variables to HTML elements. It has been successful in one part of the site when the data attributes are added to a tr tag. The following code works: <tr class="js-instructions ...

Neither Output nor EventEmitter are transmitting data

I am struggling to pass data from my child component to my parent component using the Output() and EventEmitter methods. Despite the fact that the emitter function in the child component is being called, it seems like no data is actually being sent through ...

What is the method to determine the inviter on Discord.js?

Hi there! I'm currently working on an Invite Logger and could use some assistance. I'm having trouble figuring out how to identify the user who invited another user. client.on('guildMemberAdd', async (member) => { const channel = ...

AngularJS supports asynchronous validation on blur

Using Angular JS version 1.5.6, I am looking to implement asynchronous input validation that occurs only on blur. However, I am unable to use modelOption: {debounce: 500} or modelOption: {updateOn: 'blur'} due to the directive being used with oth ...

Having trouble with JavaScript not working when clicking an image and toggling a div?

Why isn't the onclick image and toggle div functionality working with JavaScript? I made the change from: <input type="button" id="Showdiv1" name="Showdiv1" value="Show Div 1" onclick="showDiv('div1')" /> to: <img src="https://d ...

JQuery Tic Tac Toe Duel: Face Off Against Your Friend in a Thr

Looking for some advice here. I'm new to game development and currently working on a 2 Player Tic Tac Toe game. I need help with implementing the game functionality. Any suggestions? I want to disable the "div" once a player clicks on it, but I' ...

Sequelize assigns alternative names to attributes following a join operation

Following a join operation involving three models, I obtained a valid result. However, I am looking to rename the attributes generated by the join operation in the findAll function. Query: const orchards = await db.Area.findAll({ include: [db.Are ...

Design a table within an mdDialog that allows for editing of data presented in JSON format

I'm attempting to implement a dialog using JSON data. $scope.showAttributeData = function(data) { $scope.feature = data console.log($scope.feature) var that = this; var useFullScreen = ($mdMedia('sm') ...

The nearby diversion inexplicably triggers a phantom GET request

My goal is to build a website with specific functionality: If a user tries to access the /home page without authentication, they should be redirected to the /login page. After successfully logging in on the /login page, the user should receive a session c ...