auth.js 508 B

1234567891011121314151617181920212223
  1. import jwt from "jsonwebtoken";
  2. const auth = async (req, res, next) => {
  3. try {
  4. const token = req.headers.authorization.split(" ")[1];
  5. const isCustomAuth = token.length < 500;
  6. let decodedData;
  7. if (token && isCustomAuth) {
  8. decodedData = jwt.verify(token, "sometext");
  9. req.userId = decodedData?.id;
  10. } else {
  11. decodedData = jwt.decode(token);
  12. req.userId = decodedData?.sub;
  13. }
  14. next();
  15. } catch (error) {
  16. console.log(error);
  17. }
  18. };
  19. export default auth