the user should be auto sign in upon launching the app. Why does it say token expired.
// backend/controllers/authController.js

const jwt = require('jsonwebtoken');
const User = require('../models/User');

const generateToken = (user) => {
    return jwt.sign(
        { 
            userId: user._id,
            name: user.username, // Assuming 'username' is the user's display name
            email: user.email,
        },
        process.env.JWT_SECRET,
        { expiresIn: '1h' }
    );
};

/**
 * @desc    Register a new user
 * @route   POST /api/auth/register
 * @access  Public
 */
const register = async (req, res) => {
    const { username, email, password, desiredPhysique, fitnessGoals, dietaryPreferences, dietaryRestrictions } = req.body;

    try {
        // Validate required fields
        if (!username || !email || !password || !desiredPhysique) {
            return res.status(400).json({ message: 'Please provide username, email, password, and desired physique' });
        }

        // Check if username already exists
        let existingUser = await User.findOne({ username });
        if (existingUser) {
            return res.status(400).json({ message: 'Username already exists' });
        }

        // Check if email already exists
        existingUser = await User.findOne({ email });
        if (existingUser) {
            return res.status(400).json({ message: 'Email already in use' });
        }

        // Create new user
        const newUser = new User({
            username,
            email,
            password,
            desiredPhysique,
            fitnessGoals: fitnessGoals || [],
            dietaryPreferences: dietaryPreferences || [],
            dietaryRestrictions: dietaryRestrictions || [],
        });

        // Save user to database
        await newUser.save();

        // Generate JWT
        const token = generateToken(newUser);

        // Respond with token and success message
        res.status(201).json({ token, message: 'User registered successfully' });
    } catch (err) {
        console.error('Registration error:', err);

        // Handle Mongoose validation errors
        if (err.name === 'ValidationError') {
            const messages = Object.values(err.errors).map(val => val.message);
            return res.status(400).json({ message: messages });
        }

        // Generic server error
        res.status(500).json({ message: 'Server error' });
    }
};

/**
 * @desc    Authenticate user and get token
 * @route   POST /api/auth/login
 * @access  Public
 */
const login = async (req, res) => {
    const { email, password } = req.body; // Using email for login

    try {
        // Validate input
        if (!email || !password) {
            return res.status(400).json({ message: 'Please provide email and password' });
        }

        // Find user by email
        const user = await User.findOne({ email });
        if (!user) {
            return res.status(400).json({ message: 'Invalid credentials' });
        }

        // Compare passwords
        const isMatch = await user.comparePassword(password);
        if (!isMatch) {
            return res.status(400).json({ message: 'Invalid credentials' });
        }

        // Generate JWT
        const token = generateToken(user);

        // Respond with token and success message
        res.json({ token, message: 'Login successful' });
    } catch (err) {
        console.error('Login error:', err);
        res.status(500).json({ message: 'Server error' });
    }
};

/**
 * @desc    Get current user
 * @route   GET /api/auth/me
 * @access  Private
 */
const getCurrentUser = async (req, res) => {
    try {
        const userId = req.user; // Obtained from authMiddleware
        const user = await User.findById(userId).select('-password');
        if (!user) {
            return res.status(404).json({ message: 'User not found' });
        }
        res.json(user);
    } catch (err) {
        console.error('Get current user error:', err);
        res.status(500).json({ message: 'Server error' });
    }
};

module.exports = { register, login, getCurrentUser };