39 lines
1.3 KiB
JavaScript
39 lines
1.3 KiB
JavaScript
import axios from 'axios'
|
|
/*
|
|
export function someMutation (state) {
|
|
}
|
|
*/
|
|
export const evalAuth = (state) => {
|
|
console.log("isAuthenticated()");
|
|
console.log(localStorage.getItem('userToken'));
|
|
if (localStorage.getItem('userToken')) { // TODO hier muss Abfrage mit API, z.B. /api/user?token="ME" stattfinden.
|
|
state.user.isAuthenticated = true;
|
|
} else {
|
|
state.user.isAuthenticated = false;
|
|
}
|
|
};
|
|
export const logout = (state) => {
|
|
console.log("logout()");
|
|
console.log(localStorage.getItem('userToken'));
|
|
localStorage.removeItem('userToken');
|
|
console.log(localStorage.getItem('userToken'));
|
|
state.commit(evalAuth(state));
|
|
};
|
|
export const login = (state) => {
|
|
const data = {
|
|
username: state.user.username,
|
|
password: state.user.password
|
|
};
|
|
console.log("GET http://localhost:8080/api/login/ - json: " + JSON.stringify(data));
|
|
this.state.$axios.post('http://localhost:8080/api/login', data) // TODO muss GET mit AUTH Header werden
|
|
.then((response) => {
|
|
console.log("GET/POST http://localhost:8080/api/login/ - response: " + response.data);
|
|
localStorage.setItem('userToken', JSON.stringify(response));
|
|
state.commit(evalAuth(state));
|
|
})
|
|
.catch((error) => {
|
|
console.log("error: " + error);
|
|
//errorDialog = true;
|
|
})
|
|
};
|