Created
November 11, 2020 23:43
-
-
Save blogcacanid/a4d92a3e4bf623a4f0d5c3a2bf8b130c to your computer and use it in GitHub Desktop.
auth.service.js Authentication JWT React JS Lumen 7
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import axios from "axios"; | |
// For Lumen 7 back-end | |
const API_URL = "http://localhost:8000/api/auth/"; | |
// for Node.js Express back-end | |
// const API_URL = "http://localhost:9090/api/auth/"; | |
const register = (username, email, password) => { | |
return axios.post(API_URL + "register", { | |
username, | |
email, | |
password, | |
}); | |
}; | |
const login = (username, password) => { | |
return axios | |
.post(API_URL + "login", { | |
username, | |
password, | |
}) | |
.then((response) => { | |
if (response.data.accessToken) { | |
localStorage.setItem("user", JSON.stringify(response.data)); | |
} | |
return response.data; | |
}); | |
}; | |
const logout = () => { | |
localStorage.removeItem("user"); | |
}; | |
export default { | |
register, | |
login, | |
logout, | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment