When I try to register a new user I get an error back with axios.
This is my axios configuration and code where I want to use it:
Axios.js
import axios from "axios";
import store from "./store";
const axiosClient = axios.create({
baseURL: `http://localhost:5173/`,
});
axiosClient.interceptors.request.use((config) => {
config.headers.Authorization = `Bearer ${store.state.user.token}`;
return config;
});
export default axiosClient;
Store/index.js
import { createStore } from "vuex";
import axiosClient from "../axios";
const store = createStore({
state: {
user: {
data: {},
token: sessionStorage.getItem("TOKEN"),
},
},
getters: {},
actions: {
register({ commit }, user) {
return axiosClient.post("/register", user)
.then(({ data }) => {
commit("setUser", data);
return data;
});
},
login({ commit }, user) {
return axiosClient.post("/login", user)
.then(({ data }) => {
commit("setUser", data);
return data;
});
},
},
Also I don't know why, but by deafult I have # in my page url:
http://localhost:5173/#/register

Since a user that has not yet been authenticated in your Login and Register endpoints can send a request, you should not send the
Authorizationparameter in the header for requests made to them.Here's how you can update your
axios.jsfile with and without auth for to divide in two requests can be post:Now you can use either one to fulfill your requests in
store/index.js. For example:Finally run:
Just in case, delete your old compiled files (chunks) before running them.
If you need to clear the cache on the server side, don't forget:
Basically, determining whether the routes you are accessing requires auth and sending parameters in the header accordingly will solve your problem.