Fix router initialization in eventhandler
useRouter has to be called in setup or returns undefined otherwise. See https://github.com/vuejs/vue-router/issues/3379
This commit is contained in:
parent
4085868cd7
commit
e9d4ed1b3e
@ -5,6 +5,7 @@ import { useSessionStore } from "../stores/session";
|
||||
|
||||
const error = ref("");
|
||||
const login = ref({ user: "", password: "" });
|
||||
const router = useRouter(); // has to be called in setup
|
||||
|
||||
onMounted(() => {
|
||||
useSessionStore().setTitle("Login");
|
||||
@ -15,7 +16,8 @@ function formSubmit(e: MouseEvent) {
|
||||
useSessionStore().login(login.value)
|
||||
.then(x => {
|
||||
error.value = "";
|
||||
useRouter().replace("/dashboard");
|
||||
router.replace("/dashboard");
|
||||
return x;
|
||||
})
|
||||
.catch(x => error.value = "The entered credentials are invalid!");
|
||||
|
||||
|
@ -20,12 +20,12 @@ export interface Budget {
|
||||
|
||||
export const useSessionStore = defineStore('session', {
|
||||
state: () => ({
|
||||
Session: useStorage<Session>('session', null, undefined, { serializer: StorageSerializers.object }),
|
||||
Session: useStorage<Session | null>('session', null, undefined, { serializer: StorageSerializers.object }),
|
||||
Budgets: useStorage<Map<string, Budget>>('budgets', new Map<string, Budget>(), undefined, { serializer: StorageSerializers.map }),
|
||||
}),
|
||||
getters: {
|
||||
BudgetsList: (state) => [ ...state.Budgets.values() ],
|
||||
AuthHeaders: (state) => ({'Authorization': 'Bearer ' + state.Session.Token}),
|
||||
AuthHeaders: (state) => ({'Authorization': 'Bearer ' + state.Session?.Token}),
|
||||
LoggedIn: (state) => state.Session != null,
|
||||
},
|
||||
actions: {
|
||||
@ -42,15 +42,18 @@ export const useSessionStore = defineStore('session', {
|
||||
async login(login: any) {
|
||||
const response = await POST("/user/login", JSON.stringify(login));
|
||||
const result = await response.json();
|
||||
return this.loginSuccess(result);
|
||||
this.loginSuccess(result);
|
||||
return result;
|
||||
},
|
||||
async register(login : any) {
|
||||
const response = await POST("/user/register", JSON.stringify(login));
|
||||
const result = await response.json();
|
||||
return this.loginSuccess(result);
|
||||
this.loginSuccess(result);
|
||||
return result;
|
||||
},
|
||||
logout() {
|
||||
this.$reset()
|
||||
this.Session = null;
|
||||
this.Budgets.clear();
|
||||
},
|
||||
}
|
||||
})
|
Loading…
x
Reference in New Issue
Block a user