Files
pendaftaran/store/login.ts
T
2024-12-27 16:46:43 +07:00

55 lines
1.6 KiB
TypeScript

import {defineStore} from "pinia";
import {ref} from "vue";
import {useCookie} from "#app";
// ___________________AUTH_________________________
export const useAuthentication = defineStore("Authentication", () => {
// ________________AUTH____________________________
const resAuth = ref<any[]>([]);
const auth = async (body: Record<string, any>) => {
try {
resAuth.value = await $fetch("http://127.0.0.1:8000/api/login/", {
method: "POST",
headers: {'Content-Type': 'application/json'},
body: JSON.stringify(body)
});
// resultAuth.value = resAuth._rawValue;
// console.log(resAuth._rawValue.token);
useCookie('token').value = resAuth._rawValue.token;
} catch (err) {
throw createError({
statusCode: 400,
statusMessage: "Failed to fetch data from Authentication API",
});
}
};
// __________________USER AUTH_______________________________________
const user = ref<any[]>([]);
const userAuth = async (body: Record<string, any>) => {
console.log(body.token)
try {
user.value = await $fetch("http://127.0.0.1:8000/api/userAuth/", {
method: "GET",
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${useCookie('token').value}`,
'X-Requested-With': 'XMLHttpRequest',
},
})
console.log(user.value)
console.log(user)
} catch (err) {
navigateTo({
path: '/login',
});
throw createError({
statusCode: 400,
statusMessage: "Failed to fetch data from User Auth API",
});
}
}
return {auth, resAuth, userAuth, user}
})