The code in this example imports the axios module and exports two functions: register and login.
The register function takes an input of a username and email address and stores these values in local storage.
The login function takes an input of a username and password and stores these values in local storage.
The logout function removes the value from local storage of the user.
import axios from 'axios';
export const register = (username, email) => {
return axios
.post('/api/register', {
username,
email,
})
.then((response) => {
console.log('Registered');
});
};
export const login = (username, password) => {
return axios
.post('/api/login', {
username,
password,
})
.then((response) => {
if (response.data.accessToken) {
localStorage.setItem('user', JSON.stringify(response.data));
}
return response.data;
});
};
export const logout = () => {
localStorage.removeItem('user');
};