mirror of
https://git.v0id.ovh/n3wt-innov/n3wt-school.git
synced 2026-01-29 16:03:21 +00:00
refactor: refactoring du FRONT page subscribe
This commit is contained in:
67
Front-End/src/app/lib/authAction.js
Normal file
67
Front-End/src/app/lib/authAction.js
Normal file
@ -0,0 +1,67 @@
|
||||
|
||||
import {
|
||||
BE_AUTH_LOGIN_URL,
|
||||
BE_AUTH_PROFILE_URL,
|
||||
FE_USERS_LOGIN_URL ,
|
||||
} from '@/utils/Url';
|
||||
|
||||
import {mockUser} from "@/data/mockUsersData";
|
||||
|
||||
const useFakeData = process.env.NEXT_PUBLIC_USE_FAKE_DATA === 'true';
|
||||
|
||||
|
||||
/**
|
||||
* Disconnects the user after confirming the action.
|
||||
* If `NEXT_PUBLIC_USE_FAKE_DATA` environment variable is set to 'true', it will log a fake disconnect and redirect to the login URL.
|
||||
* Otherwise, it will send a PUT request to the backend to update the user profile and then redirect to the login URL.
|
||||
*
|
||||
* @function
|
||||
* @name disconnect
|
||||
* @returns {void}
|
||||
*/
|
||||
export const disconnect = () => {
|
||||
if (confirm("\nÊtes-vous sûr(e) de vouloir vous déconnecter ?")) {
|
||||
|
||||
if (useFakeData) {
|
||||
console.log('Fake disconnect:', mockUser);
|
||||
router.push(`${FE_USERS_LOGIN_URL}`);
|
||||
} else {
|
||||
console.log('Fake disconnect:', mockUser);
|
||||
router.push(`${FE_USERS_LOGIN_URL}`);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
export const createProfile = (data,csrfToken) => {
|
||||
const request = new Request(
|
||||
`${BE_AUTH_PROFILE_URL}`,
|
||||
{
|
||||
method:'POST',
|
||||
headers: {
|
||||
'Content-Type':'application/json',
|
||||
'X-CSRFToken': csrfToken
|
||||
},
|
||||
credentials: 'include',
|
||||
body: JSON.stringify(data),
|
||||
}
|
||||
);
|
||||
return fetch(request).then(response => response.json())
|
||||
}
|
||||
|
||||
|
||||
export const updateProfile = (id, data, csrfToken) => {
|
||||
const request = new Request(
|
||||
`${BE_AUTH_PROFILE_URL}/${id}`,
|
||||
{
|
||||
method:'PUT',
|
||||
headers: {
|
||||
'Content-Type':'application/json',
|
||||
'X-CSRFToken': csrfToken
|
||||
},
|
||||
credentials: 'include',
|
||||
body: JSON.stringify(data),
|
||||
}
|
||||
);
|
||||
return fetch(request).then(response => response.json())
|
||||
}
|
||||
Reference in New Issue
Block a user