Next.js Discord

Discord Forum

Accessing stores express-session from next apo

Unanswered
NanotechPikachu posted this in #help-forum
Open in Discord
Hi.

I am running an instance of express in VERCEL which stores some session data using express-session package.

When I try to access it from my next app, it always returns false value though it's value is true when i check my express API.

Why is it happening?

My next code
"use client"

import axios from 'axios';
import { useState, useEffect } from 'react';

export default function UserProfile() {
  const [ user, setUser ] = useState(null);


  useEffect(() => {
    const checkAuth = async () => {
    try {
      const log = await axios.get(`https://dash-six-beryl.vercel.app/auth/check`, { withCredentials: true }); console.log(log);
  
        const response = await axios.get(`${process.env.REACT_APP_API_URL}/user`, { withCredentials: true }); 
        setUser(response?.data);
   
    } catch (err) {
      console.error(err);
    }
    };
    checkAuth();
  }, []);
  return (
    <>
    {user ? JSON.stringify(user) : "loading"}
    </>
  )
}


My relevant express app code
app.use(session({
  secret: 'ABSJWTWTAJQFQAFGAH', 
  resave: false,
  saveUninitialized: false,
  cookie: {
    secure: 'auto', 
    httpOnly: true,
    maxAge: 86400000 
  }
}));

app.get('/auth/discord', async (req, res) => {
  const code = req.query.code;
  try {
    const tokenResponse = // Axios get from discord API
    const accessToken = tokenResponse.data.access_token;

    // Store the access token in the session
    req.session.accessToken = accessToken;

    res.redirect('https://dash-discord.vercel.app/user');
  } catch (error) {
    console.error(error);
    res.status(500).json({ message: "Internal server error" });
  }
});

app.get('/auth/check', (req, res) => {
  if (req.session.accessToken) {
    // if logged in
    res.json({ loggedIn: true });
  } else {
    // User is not logged in
    res.json({ loggedIn: false });
  }
});


The above auth/check returns false always. I am hosting both in VERCEL.

Plz help

1 Reply

Help?