Home > front end >  res cookie doesnt update cookies in the browser
res cookie doesnt update cookies in the browser

Time:11-20

I have been trying to set cookies in the browser from nodejs backend trough API with React and it doesn't want to set them. It's not returning response and it doesn't give me any errors. Does this client.verifytoken function cause the issue? Can you please help?

Nodejs

export const googleAuth = async (req, res) => {
    const {tokenId} = req.body
    client.verifyIdToken({idToken: tokenId, audience: process.env.GOOGLE_CLIENT_ID}).then((response) => {
        const {email_verified, name, email} = response.payload
        console.log(response.payload)
        if (email_verified) {
            Users.findOne({where: {email: email}}).then(user => {
                if (user) {
                    try {
                        const userId = user.id
                        console.log('user id', userId)
                        const refreshToken = jwt.sign({userId}, process.env.REFRESH_TOKEN_SECRET, {expiresIn: '1d'})
                        Users.update({refreshToken: refreshToken}, {where: {id: userId}})
                        res.cookie('refreshToken', refreshToken, {
                            httpOnly: false,
                            maxAge: 24 * 60 * 60 * 1000,
                        });

                    } catch (err) {
                        console.log(err)
                    }

                } else {
                    try {
                        const salt = bcrypt.genSaltSync(2);
                        const hashPassword = bcrypt.hashSync(email   process.env.ACCESS_TOKEN_SECRET, salt);
                        const refreshToken = jwt.sign({email}, process.env.REFRESH_TOKEN_SECRET, {expiresIn: '1d'})
                        console.log('refresh token', refreshToken)
                        Users.create({
                            name: name,
                            email: email,
                            password: hashPassword,
                            refresh_token: refreshToken,
                            verified: true
                        })
                        res.cookie('refreshToken', refreshToken, {
                            httpOnly: true,
                            maxAge: 24 * 60 * 60 * 1000,
                        });

                    } catch (err) {
                        console.log(err)
                    }


                }
            })
        }
    })

}

Reactjs

const responseSuccessGoogle = async (response) => {
        try {
            console.log(response)
            let result = await axios.post('http://localhost:5000/google-login', {tokenId: response.tokenId},{withCredentials:true})
            setAuth(result.data != null)
            navigate('/profile')
            console.log(result.data)
        } catch (error) {
            console.log(error)
        }

    }

CodePudding user response:

res.cookie() doesn't send the response, but only sets the cookie in response causing halt state in your case. You need to send response back either via res.send() or res.end(). You should also send a proper response with error code back to client instead of logging it only, as this would also halt the request. Following code should send response with empty body and send response with error code 500 in case of error.

export const googleAuth = async (req, res) => {
    const {tokenId} = req.body
    client.verifyIdToken({idToken: tokenId, audience: process.env.GOOGLE_CLIENT_ID}).then((response) => {
        const {email_verified, name, email} = response.payload
        console.log(response.payload)
        if (email_verified) {
            Users.findOne({where: {email: email}}).then(user => {
                if (user) {
                    try {
                        const userId = user.id
                        console.log('user id', userId)
                        const refreshToken = jwt.sign({userId}, process.env.REFRESH_TOKEN_SECRET, {expiresIn: '1d'})
                        Users.update({refreshToken: refreshToken}, {where: {id: userId}})
                        res.cookie('refreshToken', refreshToken, {
                            httpOnly: false,
                            maxAge: 24 * 60 * 60 * 1000,
                        });
                        res.send();

                    } catch (err) {
                        console.log(err)
                        res.status(500).send()
                    }

                } else {
                    try {
                        const salt = bcrypt.genSaltSync(2);
                        const hashPassword = bcrypt.hashSync(email   process.env.ACCESS_TOKEN_SECRET, salt);
                        const refreshToken = jwt.sign({email}, process.env.REFRESH_TOKEN_SECRET, {expiresIn: '1d'})
                        console.log('refresh token', refreshToken)
                        Users.create({
                            name: name,
                            email: email,
                            password: hashPassword,
                            refresh_token: refreshToken,
                            verified: true
                        })
                        res.cookie('refreshToken', refreshToken, {
                            httpOnly: true,
                            maxAge: 24 * 60 * 60 * 1000,
                        });
                        res.send();

                    } catch (err) {
                        console.log(err)
                        res.status(500).send()
                    }


                }
            })
        }
    })

}
  • Related