Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 | 15x 15x 15x 15x 15x 15x 15x 4x 3x 3x 3x 3x 4x 2x 36x 36x | import axios from "axios";
// adapted from https://www.bezkoder.com/react-jwt-auth/
const API_URL = "/api/auth/";
const csrf_meta = document.querySelector("meta[name='_csrf']");
const csrf_header_meta = document.querySelector("meta[name='_csrf_header']");
Eif (csrf_meta !== null && csrf_header_meta !== null) {
// @ts-ignore
const csrf_token = csrf_meta.content;
// @ts-ignore
const csrf_header = csrf_header_meta.content;
// @ts-ignore
axios.defaults.headers.post[csrf_header] = csrf_token;
}
class AuthService {
login(username: string, password: string) {
return axios
.post(API_URL + "login", {
username,
password
})
.then(response => {
// @ts-ignore
Eif (response.data.accessToken) {
localStorage.setItem("user", JSON.stringify(response.data));
}
return response;
});
}
logout() {
localStorage.removeItem("user");
}
register(username: string, email: string, password: string) {
return axios.post(API_URL + "register", {
username,
email,
password
})
// also login on success
.then(() => this.login(username, password));
}
getCurrentUser() {
const savedUser = localStorage.getItem('user')
return savedUser && JSON.parse(savedUser);
}
}
export default new AuthService();
|