Home > Blockchain >  SyntaxError: Identifier 'createToken' has already been declared
SyntaxError: Identifier 'createToken' has already been declared

Time:02-16

Hi I'm making a login server there is an error There's an error when I make a declaration. const createToken what can I do How can I change that code? What's the problem and how to solve and why? can you explain me thank you very much

isRefresh.js

    const jwt = require("jsonwebtoken");
    const { createToken } = require("../utils/jwt");

    const db = require("../database/db");

    const createToken = (payload) => {
    console.log("createToken");
    const token = jwt.sign({ username: payload.toString() }, secretKey, {
    algorithm: "sha256",
    expiresIn: "30m",
    });
    return token;
    };

    module.exports = async (req, res, next) => {
    try {
    const refreshtoken = req.get("r_x_auth");

    if (!refreshtoken) {
      return false;
    }

    const decodedToken = jwt.verify(refreshtoken, R_secretKey);
    const data = [decodedToken.username];

    const sql = "select * from member where user_id = ?";

    const conn = await db.getConnection();
    const [rows] = await conn.query(sql, data);
    if (rows) {
      const newtoken = createToken(rows[0].username);
      res.send(newtoken);
    } else {
      return false;
    }
  } catch (err) {
    next(err);
  }
};

jwt.js

 const jwt = require("jsonwebtoken");

const { ACCESS_KEY, REFRESH_KEY } = process.env;

const createToken = (payload) => {
  const token = jwt.sign({ username: payload.toString() }, ACCESS_KEY, {
    algorithm: "sha256",
    expiresIn: "30m",
  });
  return token;
};

const creatRefreshToken = (payload) => {
  const retoken = jwt.sign({ username: payload.toString() }, REFRESH_KEY, {
    algorithm: "sha256",
    expiresIn: "1d",
  });
  return retoken;
};

module.exports = { createToken, creatRefreshToken };

isAuth.js

    const jwt = require("jsonwebtoken");
require("dotenv").config();
const secretKey = process.env.ACCESS_KEY;
const db = require("../database/db");

module.exports = async (req, res, next) => {
  try {
    const token = req.get("x_auth");
    const decodedToken = jwt.verify(token, secretKey);
    const { username } = decodedToken;
    const data = [username];
    const sql = "select * from member where username = ?";
    const conn = await db.getConnection();
    const [rows] = await conn.query(sql, data);
    if (!rows) {
      return false;
    }
    next();
  } catch (err) {
    next(err);
  }
};

controller.js

    const jwt = require("jsonwebtoken");
require("dotenv").config();
const secretKey = process.env.ACCESS_KEY;
const db = require("../database/db");

module.exports = async (req, res, next) => {
  try {
    const token = req.get("x_auth");
    const decodedToken = jwt.verify(token, secretKey);
    const { username } = decodedToken;
    const data = [username];
    const sql = "select * from member where username = ?";
    const conn = await db.getConnection();
    const [rows] = await conn.query(sql, data);
    if (!rows) {
      return false;
    }
    next();
  } catch (err) {
    next(err);
  }
};

Anyone please help thanks ! please please help me I'm clueless save me

CodePudding user response:

Error message is clean as it should be: you defined createToken twice on same module level.

Rename one of them, for example:

const createTokenJWT = require("../utils/jwt").createToken;

CodePudding user response:

You are declaring createToken twice:

const { createToken } = require("../utils/jwt");

and here:

const createToken = (payload) => {
    console.log("createToken");
    const token = jwt.sign({ username: payload.toString() }, secretKey, {
        algorithm: "sha256",
        expiresIn: "30m",
    });
    return token;
};
  • Related