« first day (4934 days earlier)   

7:16 AM
@ParkingMaster I am trying to handle express-session from express.js app on frontend app
export const login = asyncHandler(
  async (req: Request<ILogin>, res: Response<IResponse>) => {
    const token = await UserService.authenticate(req.body);

    req.session.token = token;

    return res
      .status(200)
      .json({ success: true, message: messages.auth.loggedIn });
  }
);
I have successfully saving jwt token in MongoDB collection (sessions) by using MongoStore
// Configure session
app.use(
  session({
    name: config.session.name,
    secret: config.session.secret as string,
    resave: false,
    saveUninitialized: false,
    store,
    cookie: {
      httpOnly: isProduction,
      secure: isProduction,
      maxAge: +config.session.cookie.expiration
    }
  })
);
after successfully logging in , session for that user is saved in DB, like this

{"cookie":{"originalMaxAge":900000,"expires":"2024-04-19T07:35:27.946Z","secure":true,"httpOnly":true,"path":"/"},"token":"jwt_token_here"}
But how to use that sessionId and cookie on frontend application , to authenticate user on UI , not just on server side ?
 
7:41 AM
@ParkingMaster

this is my auth.middleware from server side app

import { NextFunction, Request, Response } from 'express';
import messages from '../assets/json/messages.json';
import { AuthenticationError } from '../errors';
import config from '../config/env';
import { verifyToken } from '../utils';

const requireAuth = async (req: Request, res: Response, next: NextFunction) => {
  try {
    if (!req.session || !req.session.token) {
      return next(new AuthenticationError(messages.auth.unauthorized));
What should I do on frontend app to use that authentication session successfully ?
 

« first day (4934 days earlier)