-
Notifications
You must be signed in to change notification settings - Fork 0
/
useLogin.ts
46 lines (43 loc) · 1.17 KB
/
useLogin.ts
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
import { ref } from "vue"
import { useRouter } from 'vue-router';
import { useAuthStore } from '@/modules/Auth/stores'
import { getError } from "@/utils/helpers";
import * as AuthService from "@/modules/Auth/services";
import type { FormLogin, StandaloneLogin } from '@/modules/Auth/types/Auth'
export function useLogin(): StandaloneLogin {
const router = useRouter();
const auth = useAuthStore()
const error = ref(null)
const sending = ref(false)
const login = async (form: FormLogin) => {
const payload = {
email: form.email,
password: form.password,
}
error.value = null;
try {
sending.value = true;
await AuthService.login(payload);
const authUser = await auth.getAuthUser();
if (authUser) {
auth.setGuest({ value: "isNotGuest" });
await router.push("/dashboard");
} else {
const err = Error(
"Unable to fetch user after login, check your API settings."
);
err.name = "Fetch User";
throw err;
}
} catch (err) {
error.value = getError(err);
} finally {
sending.value = false;
}
}
return {
login,
sending,
error
}
}