src/utils/index.js
// untuk memverifikasi refresh token yang dikirim
untuk memverifikasi refresh token yang dikirim
untuk memverifikasi refresh token yang dikirim
async function verifyRefToken(model, refreshToken) {
  const checkRefTokenInDB = await model.Authentication.findOne({
    where: {
      refreshToken,
    },
  });
  if (!checkRefTokenInDB) {
    const error = new AuthenticationError('Unauthorized');
    throw error;
  }
  return jwt.verify(
    refreshToken,
    process.env.REFRESH_TOKEN_KEY,
    async (err, decode) => {
      if (err) {
        if (err.name !== 'TokenExpiredError') {
          const error = new AuthenticationError(err.message);
          throw error;
        }
        const error = new AuthenticationError('Refresh token has Expired');
        await model.Authentication.destroy({
          where: {
            refreshToken,
          },
        });
        throw error;
      }
      const user = await model.User.findByPk(decode.id);
      if (!user) {
        const error = new AuthenticationError('Unauthorized');
        throw error;
      }
      return user;
    },
  );
}
async function verifyRefToken ( model , refreshToken ) { const checkRefTokenInDB = await model . Authentication . findOne ( { where : { refreshToken , } , } ) ; if ( ! checkRefTokenInDB ) { const error = new AuthenticationError ( 'Unauthorized' ) ; throw error ; } return jwt . verify ( refreshToken , process . env . REFRESH_TOKEN_KEY , async ( err , decode ) => { if ( err ) { if ( err . name !== 'TokenExpiredError' ) { const error = new AuthenticationError ( err . message ) ; throw error ; } const error = new AuthenticationError ( 'Refresh token has Expired' ) ; await model . Authentication . destroy ( { where : { refreshToken , } , } ) ; throw error ; } const user = await model . User . findByPk ( decode . id ) ; if ( ! user ) { const error = new AuthenticationError ( 'Unauthorized' ) ; throw error ; } return user ; } , ) ; }
untuk memverifikasi refresh token yang dikirim
untuk memverifikasi refresh token yang dikirim
// periksa refresh token di dlm DB // verifikasi refresh token berdasarkan key // jika refresh token expired maka refresh token yang di DB dihapus // verifikasi user