Auth0 Home Blog Docs

Mobile + api: React Native implementation

Hello everybody, I’m trying to follow mobile + api tutorial but I have a problem with the api part because it always gives me this error: “UnauthorizedError: jwt malformed”
This is the login part:
const auth0 = new Auth0({ domain: ‘.auth0.com’, clientId: ‘’ });
auth0
.webAuth
.authorize({scope: ‘openid profile email offline_access’, audience: ‘’})
.then(credentials => {…}).catch(…);

This is the part where I call my api:
AsyncStorage.getItem(‘accessToken’).then((ACCESS_TOKEN) => {
fetch(’’, {
method: ‘POST’,
headers: {
Accept: ‘application/json’,
‘Authorization’: 'Bearer '+ACCESS_TOKEN.token,
‘Content-Type’: ‘application/json’,
},
body: JSON.stringify({
data: data,
}),
}).then(responseJson => {
console.log(responseJson)
}).catch(error =>
console.log(error)
);
});
This is my auth module:
const jwt = require(‘express-jwt’);
const jwksRsa = require(‘jwks-rsa’);

const checkJwt = jwt({
    secret: jwksRsa.expressJwtSecret({
      cache: true,
      rateLimit: true,
      jwksRequestsPerMinute: 5,
      jwksUri: 'https://<my auth0 domain>.auth0.com/.well-known/jwks.json'
    }),
    audience: "<api audience>",
    issuer: 'https://<my auth0 domain>.auth0.com/',
    algorithms: ['RS256']
  });

module.exports = checkJwt;

This are my endpoints:
const jwtAuthz = require(‘express-jwt-authz’);
const checkJwt = require(’…/…/auth’);
const v0 = require(‘express’).Router();
const <endpoint 1> = require(’./<endpoint 1>’);
const <endpoint 2> = require(’./<endpoint 2>’);
const <endpoint 3> = require(’./<endpoint 3>’);

v0.post('/<endpoint 1>', checkJwt, jwtAuthz(['<scope 1>']), <endpoint 1>);
v0.post('/<endpoint 2>', checkJwt, /*jwtAuthz(['<scope 2>']),*/ <endpoint 2>);
v0.post('/<endpoint 3> checkJwt, jwtAuthz(['<scope 3>']), <endpoint 3>);

v0.get('/', (req, res) => res.status(200).json({message: 'working!'}))

module.exports = v0;

Thank you in advance for your attention.
Emanuele

1 Like