res.cookie("access", value) Cookie is not storing on browser

29 Views Asked by At

enter image description hereI was trying to store jwt token as Cookie in the browser but it is not storing it in browser, But in the postman it's is working fine, in the browser I am getting Set-Cookie=value in response-headers even tried to store using libraries like js-cookie it stored at first time, but when I tries again it's not storing it

this is Login function I am using to store it

export const userLogin = async (request, response, next) => {
    const { email, password } = request.body;

    try {

        const isUserAvailable = await UserModel.findOne({ email });

        if (!isUserAvailable) {
            return response.status(NOT_ACCEPTABLE).json({ error: true, statusCode: NOT_ACCEPTABLE, message: `User Not Found !` })
    }

        const isPasswordCorrect = await isPasswordValid(password, isUserAvailable.password);

        if (isPasswordCorrect) {
            const expiryDate = new Date();
            expiryDate.setDate(expiryDate.getDate() + 1);
            const accessToken = await generateToken(isUserAvailable._id,    isUserAvailable.username);
            console.log("access controller", accessToken)


            response.cookie('access_token', accessToken, {
                httpOnly: true, path: "/",
            }).status(OK)
                .json({
                    error: false, statusCode: OK, message: `Login Successful`, accessToken, data: {
                        uid: isUserAvailable._id, fullName: isUserAvailable.fullName, username:   isUserAvailable.username, email: isUserAvailable.email,
                        avatar: isUserAvailable.avatar, mobile: isUserAvailable.mobile
                    }
                })
        } else {
            response.status(FORBIDDEN).json({ error: true, statusCode: FORBIDDEN, message:   `Invalid Password` })
        }

    } catch (error) {
        next(error)
    }
}

in the below function I am trying to retrieve it
import Jwt from "jsonwebtoken";

    export const userAuthByToken = (request, response, next) => {
        try {

            const token = request.cookies.access_token;
   
            console.log("token auth", request.cookies_access_token)
 
            if (!token) {
                return response.status(401).json({ error: true, message: 'User is Un-Authorized' });
            }

            Jwt.verify(token.accessToken, process.env.JWT_SECRET_KEY, (error, user) => {
                 if (error) return response.status(401).json({ error: true, message: "Forbidden" });
                request.user = user;
                next()
            })
        } catch (error) {
    console.log(error)
    }
}

but it's not working and when I console.log I am getting null object

1

There are 1 best solutions below

0
Manu H N On

well I got answer for my question I have changed in response like below

response.cookie('access_token', token, {
            httpOnly: true,
            domain: "localhost",
            origin: "http://localhost:1234",
            path: "/api/estates",
            sameSite: "None",
            expires: expiryDate,
            secure: true,
        }).status(OK)
            .json({
                error: false, statusCode: OK, message: `Login Successful`, access_token: token, data: {
                    uid: isUserAvailable._id, fullName: isUserAvailable.fullName, username: isUserAvailable.username, email: isUserAvailable.email,
                    avatar: isUserAvailable.avatar, mobile: isUserAvailable.mobile
                }
            })

I have provided path, domain, origin and secure flag now the token is getting stored in Cookies section in browser