So im trying to create refreshtoken hook in react. and nodejs with express as my backend.
my backend code looks like this
exports.refreshToken = (req, res) => { const oldToken = req.headers.authorization.split(" ")[1] if(oldToken == null ) return res.status(500).send({message: "Token is empty"}) console.log(myJwt.refreshSecretKey) console.log(oldToken) jwt.verify(oldToken, myJwt.refreshSecretKey, (err, user) => { if(err) res.status(500).send({ msg: err || "Error on refreshing your token" }) else res.send({ refreshToken: generateRefreshToken() }); }) };
the problem is when i try this endpoint with Postwoman (chrome extension) its WORK
but when i try with React + axios the server return is
{"msg":{"name":"JsonWebTokenError","message":"invalid signature"}}
here is my react code
import axios from '../api/axios' import useAuth from './useAuth' const useRefreshToken = () => { const Auth = useAuth() const refresh = async () => { console.log(Auth.auth.token) const response = await axios.get("user/refresh", { withCredentials: true, headers: { Authorization: `Bearer ` + Auth.auth.token } }) Auth(prev => { console.log(JSON.stringify(prev)) console.log(response?.data?.refreshToken) return {...prev, token: response.data.refreshToken} }) return response.data.refreshToken } return refresh } export default useRefreshToken
Advertisement
Answer
I’m sending the wrong access token.
What I send in react is the first created accessToken. not the refreshAccessToken