Quantcast
Channel: Active questions tagged react-native+typescript - Stack Overflow
Viewing all articles
Browse latest Browse all 6581

JWT verification failed: JsonWebTokenError: invalid token

$
0
0

In my React native app i send a request what passed through a JWT verify function to the server (written in Node.js).The request called by UseEffect on first load of the component.The problem is that i recieve this error:"JWT verification failed: JsonWebTokenError: invalid token"But if i send the same request few times so since the second request i get the requested data, how is it possible?

Any help will be very much appreciated!

import { Request, Response, NextFunction } from "express";import jwt, { JwtPayload, Secret } from "jsonwebtoken";import db from "../models";const User = db.User;type UserType = typeof User;interface AuthRequest extends Request {  user?: UserType;}const verifyJWT = async (  req: AuthRequest,  res: Response,  next: NextFunction) => {  const token = req.headers.authorization?.split(" ")[1];  console.log(token);  if (!token) {    res.status(401);    return res.status(401).send({ error: "Not authorized, no token" });  }  try {    const { phoneNumber } = await jwt.verify(      token,      process.env.JWT_SECRET as Secret    ) as JwtPayload;    req.user = await User.findByPk(phoneNumber);    next();  } catch (error) {    console.log("JWT verification failed:", error);    return res.status(403).send({ error: "Not authorized" });  }};export { AuthRequest, verifyJWT };

Viewing all articles
Browse latest Browse all 6581

Latest Images

Trending Articles



Latest Images