diff --git a/src/features/auth/composables/useLogin.ts b/src/features/auth/composables/useLogin.ts new file mode 100644 index 0000000..8d635e5 --- /dev/null +++ b/src/features/auth/composables/useLogin.ts @@ -0,0 +1,20 @@ +import axios from "axios"; +import { useAuth } from "@/features/auth/stores/useAuth"; +import type { + LoginCredential, + LoginResponse, +} from "@/features/auth/types/login.type"; + +export function useLogin() { + const { auth } = useAuth(); + + async function login(credential: LoginCredential) { + const response = await axios.post( + "/api/auth/login", + credential, + ); + auth.auth = response.data.data || null; + } + + return { login }; +} diff --git a/src/features/auth/composables/useLogout.ts b/src/features/auth/composables/useLogout.ts new file mode 100644 index 0000000..6b88d0a --- /dev/null +++ b/src/features/auth/composables/useLogout.ts @@ -0,0 +1,26 @@ +import axios from "axios"; +import { useAuth } from "@/features/auth/stores/useAuth"; + +export function useLogout() { + const { auth } = useAuth(); + + async function logout() { + const response = await axios.post<{ success: boolean }>( + "/api/auth/logout", + null, + { + headers: { + Authorization: `Bearer ${auth.auth?.token}`, + }, + }, + ); + + if (!response.data.success) { + throw new Error(); + } + + auth.auth = null; + } + + return { logout }; +} diff --git a/src/features/auth/composables/useToken.ts b/src/features/auth/composables/useToken.ts new file mode 100644 index 0000000..e26e735 --- /dev/null +++ b/src/features/auth/composables/useToken.ts @@ -0,0 +1,7 @@ +import { useAuth } from "@/features/auth/stores/useAuth"; + +export function useToken() { + const { auth } = useAuth(); + + return { token: auth.auth?.token }; +} diff --git a/src/features/auth/stores/useAuth.ts b/src/features/auth/stores/useAuth.ts new file mode 100644 index 0000000..f4b2a3e --- /dev/null +++ b/src/features/auth/stores/useAuth.ts @@ -0,0 +1,14 @@ +import { computed } from "vue"; +import { defineStore } from "pinia"; +import { useLocalStorage } from "@vueuse/core"; +import type { UserAuth } from "@/features/auth/types/auth.type"; + +export const useAuth = defineStore("auth", () => { + const auth = useLocalStorage<{ auth: UserAuth | null }>("asiagolf:auth", { + auth: null, + }); + + const isAuthenticated = computed(() => !!auth.value.auth); + + return { auth, isAuthenticated }; +}); diff --git a/src/features/auth/types/auth.type.ts b/src/features/auth/types/auth.type.ts new file mode 100644 index 0000000..023e24a --- /dev/null +++ b/src/features/auth/types/auth.type.ts @@ -0,0 +1,38 @@ +export type UserAuth = { + token: string; + user: User; + employee: Employee; + affiliator: any; +}; + +export type User = { + id: number; + name: string; + email: string; + photo: string; + role: UserRole; +}; + +export type UserRole = { + id: number; + name: string; + permissions: Array<{ + id: number; + name: string; + code: string; + }>; +}; + +export type Employee = { + id: number; + name: string; + email: string; + departement: { + id: number; + name: string; + }; + location: { + id: number; + name: string; + }; +}; diff --git a/src/features/auth/types/login.type.ts b/src/features/auth/types/login.type.ts new file mode 100644 index 0000000..01b91d5 --- /dev/null +++ b/src/features/auth/types/login.type.ts @@ -0,0 +1,10 @@ +import type { UserAuth } from "@/features/auth/types/auth.type"; + +export type LoginCredential = { + email: string; + password: string; +}; + +export type LoginResponse = { + data: UserAuth; +}; diff --git a/src/helpers/sleep.ts b/src/helpers/sleep.ts new file mode 100644 index 0000000..cd115e8 --- /dev/null +++ b/src/helpers/sleep.ts @@ -0,0 +1,8 @@ +export async function sleep(ms: number) { + return new Promise((res) => { + const timeout = setTimeout(() => { + res(); + clearTimeout(timeout); + }, ms); + }); +}