Handle LOGIN via action

This commit is contained in:
Jan Bader 2022-01-29 23:32:11 +00:00
parent 24370c9d32
commit f091ce8945
2 changed files with 4 additions and 8 deletions

View File

@ -1,5 +1,5 @@
<script> <script>
import { LOGIN_SUCCESS, TITLE } from "../store/mutation-types"; import { LOGIN, LOGIN_SUCCESS, TITLE } from "../store/mutation-types";
export default { export default {
data() { data() {
@ -18,11 +18,9 @@ export default {
methods: { methods: {
formSubmit (e) { formSubmit (e) {
e.preventDefault(); e.preventDefault();
fetch("/api/v1/user/login", {method: "POST", body: JSON.stringify(this.$data.login)}) this.$store.dispatch(LOGIN, this.$data.login)
.then(x => x.json())
.then(x => { .then(x => {
this.$data.error = "" this.$data.error = "";
this.$store.commit(LOGIN_SUCCESS, x);
this.$router.replace("/dashboard"); this.$router.replace("/dashboard");
}) })
.catch(x => this.$data.error = ["The entered credentials are invalid!"]); .catch(x => this.$data.error = ["The entered credentials are invalid!"]);

View File

@ -62,11 +62,10 @@ const store = createStore({
}, },
actions: { actions: {
[LOGIN]({state, commit}, login) { [LOGIN]({state, commit}, login) {
fetch("/api/v1/user/login", {method: "POST", body: JSON.stringify(login)}) return fetch("/api/v1/user/login", {method: "POST", body: JSON.stringify(login)})
.then(x => x.json()) .then(x => x.json())
.then(x => { .then(x => {
commit(LOGIN_SUCCESS, x); commit(LOGIN_SUCCESS, x);
this.$router.replace("/dashboard");
}) })
}, },
YNAB({getters, dispatch}, formData) { YNAB({getters, dispatch}, formData) {
@ -148,7 +147,6 @@ const store = createStore({
if(state.CurrentBudgetID == null) if(state.CurrentBudgetID == null)
return {}; return {};
console.log(state.Budgets, state.CurrentBudgetID);
const budgets = state.Budgets.filter(x => x.ID == state.CurrentBudgetID); const budgets = state.Budgets.filter(x => x.ID == state.CurrentBudgetID);
if(budgets.length > 0) if(budgets.length > 0)
return budgets[0]; return budgets[0];