Tmeister / wp-api-jwt-auth

A simple plugin to add JSON Web Token (JWT) Authentication to WP REST API
GNU General Public License v2.0
558 stars 161 forks source link

Route not found when Authorization Bearer not present #145

Closed NoMan2000 closed 5 years ago

NoMan2000 commented 5 years ago
screen shot 2019-02-22 at 8 21 45 am screen shot 2019-02-22 at 8 21 55 am screen shot 2019-02-22 at 8 22 16 am

Details shown above in the network capture. When I don't put in the Authorization Bearer token, it says rest no route found. When I add it to the request, then I get a 403 because the bearer token doesn't match. The route is correct.

I'm using axios as the connecting library.

import axiosLib from 'axios';

const axios = axiosLib.create();

axios.interceptors.request.use(
  async config => {
    const localHeaders = {
      'X-WP-Nonce': CelestialSettings.nonce
    };
    let jwtObj = {};
    const jwt = localStorage.getItem('jwt') || '';
    if (jwt) {
      jwtObj = { Authorization: `Bearer ${jwt}` };
    }
    return {
      ...config,
      headers: {
        ...config.headers,
        ...localHeaders,
        ...jwtObj
      }
    };
  },
  error => Promise.reject(error)
);

export const createCancelSource = () => axiosLib.CancelToken.source();

export default axios;

And in the submit (React) I have this:

submitLogin = (e: SyntheticEvent<>) => {
    e.preventDefault();
    const {username, password} = this.state;
    axios.post(`${api}jwt-auth/v1/token`, {
      username, password
    }).then((res) => {
      const {data} = res;
      localStorage.setItem('jwt', data.token);
      return data;
    }).catch((err) => {
      console.error(err);
    });
  };
jon1wt commented 5 years ago

I think the route is:

axios.post(`${api}jwt-auth/v1/token?username=${username}&password=${password}`)