generateur_v3/frontend/src/requests/auth.request.ts

107 lines
2.8 KiB
TypeScript

import axios from 'axios';
import {authInstance} from '../apis/auth.api';
import { env } from "$env/dynamic/public";
export const loginRequest = (data: { username: string; password: string }) => {
return authInstance
.request({
url: '/login',
method: 'POST',
data,
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}
})
.then((r) => r.data as { access_token: string; refresh_token: string; token_type: string })
.catch((e) => {
throw e;
});
};
export const registerRequest = (data: {
username: string;
password: string;
password_confirm: string;
}) => {
return authInstance
.request({
url: '/register',
method: 'POST',
data,
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}
})
.then((r) => r.data as { access_token: string; refresh_token: string; token_type: string })
.catch((e) => {
throw e;
});
};
export const refreshRequest = (token: string) => {
return axios
.request({
url: `${env.PUBLIC_API_BASE}/refresh`,
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
Authorization: `Bearer ${token}`
}
})
.then((r) => r.data as { access_token: string })
.catch((e) => {
throw e;
});
};
export const logoutRequest = () => {
return authInstance
.request({
url: '/logout',
method: 'POST'
})
.then((r) => r.data as { access_token: string })
.catch((e) => {
throw e;
});
};
export const dashBoardRequest = () => {
return authInstance
.request({
url: '/user',
method: 'GET'
})
.then((r) => r.data)
.catch(console.log);
}
export const updateUserRequest = (data: { username: string, email: string | null, firstname: string | null, name: string | null }) => {
return authInstance
.request({
url: '/user',
method: 'PUT',
data,
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}
})
.then((r) => r.data)
.catch((r)=>{throw r});
}
export const updatePassword = (data: {
old_password: string, password: string,
password_confirm: string
}) => {
return authInstance
.request({
url: '/user/password',
method: 'PUT',
data,
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}
})
.then((r) => r.data)
.catch((r)=>{throw r});
}