add change password modal window
This commit is contained in:
+112
-35
@@ -9,19 +9,14 @@ export async function authorization(
|
||||
previousState: {
|
||||
email: string;
|
||||
}
|
||||
error: Record<string, string>
|
||||
error: Record<string, string>;
|
||||
requirePasswordChange?: boolean;
|
||||
userData?: { email: string; id: number };
|
||||
} | undefined,
|
||||
formData: FormData,
|
||||
) {
|
||||
const email = formData.get('email') as string || '';
|
||||
const password = formData.get('password');
|
||||
|
||||
/* для теста */
|
||||
if (email === 'test' && password === 'test') {
|
||||
/* await createSession('1111', 'test', 0); */
|
||||
/* redirect('/pages/'); */
|
||||
}
|
||||
/* для теста */
|
||||
const password = formData.get('password') as string || '';
|
||||
|
||||
const validatedFields = loginFormSchema.safeParse({
|
||||
email,
|
||||
@@ -67,9 +62,27 @@ export async function authorization(
|
||||
'Accept': 'application/json'
|
||||
}
|
||||
});
|
||||
|
||||
if (response.ok) {
|
||||
let parsed = await response.json();
|
||||
if (parsed.message_code === 0) {
|
||||
if (!parsed.message_body.admin.active_password) {
|
||||
const errors: Record<string, string> = {}
|
||||
return {
|
||||
previousState: {
|
||||
email
|
||||
},
|
||||
error: errors,
|
||||
requirePasswordChange: true,
|
||||
userData: {
|
||||
email: parsed.message_body.admin.email,
|
||||
id: parsed.message_body.admin.id,
|
||||
password: password,
|
||||
token: parsed.message_body.token
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
await createSession(parsed.message_body.token, email, parsed.message_body.admin.id);
|
||||
} else {
|
||||
throw (`${parsed.message_code}`);
|
||||
@@ -95,16 +108,80 @@ export async function authorization(
|
||||
break;
|
||||
}
|
||||
|
||||
if (error) {
|
||||
return {
|
||||
previousState: {
|
||||
email,
|
||||
password
|
||||
},
|
||||
error: errors
|
||||
};
|
||||
}
|
||||
|
||||
redirect('/pages/');
|
||||
}
|
||||
|
||||
export async function updatePasswordAction(
|
||||
id: number,
|
||||
oldPassword: string | null,
|
||||
newPassword: string,
|
||||
email: string,
|
||||
token: string
|
||||
) {
|
||||
try {
|
||||
const requestBody: any = {
|
||||
version: 1,
|
||||
msg_id: 100,
|
||||
message_body: {
|
||||
action: "update",
|
||||
admin_id: id,
|
||||
new_password: newPassword
|
||||
}
|
||||
};
|
||||
|
||||
if (oldPassword) {
|
||||
requestBody.message_body.old_password = oldPassword;
|
||||
}
|
||||
|
||||
const response = await fetch(`${API_BASE_URL}/api/admin`, {
|
||||
method: 'POST',
|
||||
body: JSON.stringify(requestBody),
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'Accept': 'application/json',
|
||||
'Authorization': `Bearer ${token}`
|
||||
}
|
||||
});
|
||||
|
||||
if (response.ok) {
|
||||
const parsed = await response.json();
|
||||
|
||||
if (parsed.message_code === 0) {
|
||||
console.log(parsed);
|
||||
if (parsed.message_desc === 'Success') {
|
||||
console.log('Success');
|
||||
await createSession(token, email, id);
|
||||
return { success: true };
|
||||
} else {
|
||||
return { success: false };
|
||||
}
|
||||
} else {
|
||||
return {
|
||||
success: false,
|
||||
error: `password-update-failed-code-${parsed.message_code}`
|
||||
};
|
||||
}
|
||||
} else {
|
||||
return {
|
||||
previousState: {
|
||||
email,
|
||||
password
|
||||
},
|
||||
error: errors
|
||||
success: false,
|
||||
error: 'request-ended-with-an-error'
|
||||
};
|
||||
}
|
||||
throw error;
|
||||
} catch (error) {
|
||||
console.error('Password update error:', error);
|
||||
return {
|
||||
success: false,
|
||||
error: 'password-update-failed'
|
||||
};
|
||||
}
|
||||
|
||||
redirect('/pages/');
|
||||
@@ -114,25 +191,25 @@ export async function logout() {
|
||||
const token = await getSessionData('token');
|
||||
|
||||
try {
|
||||
const response = await fetch(`${API_BASE_URL}/api/admin`, {
|
||||
method: 'POST',
|
||||
body: JSON.stringify({
|
||||
version: 1,
|
||||
msg_id: 100,
|
||||
token: token,
|
||||
message_body: {
|
||||
action: "logout"
|
||||
}
|
||||
}),
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'Accept': 'application/json'
|
||||
}
|
||||
});
|
||||
|
||||
if (response.ok) {
|
||||
let parsed = await response.json();
|
||||
}
|
||||
/* const response = await fetch(`${API_BASE_URL}/api/admin`, {
|
||||
method: 'POST',
|
||||
body: JSON.stringify({
|
||||
version: 1,
|
||||
msg_id: 100,
|
||||
token: token,
|
||||
message_body: {
|
||||
action: "logout"
|
||||
}
|
||||
}),
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'Accept': 'application/json'
|
||||
}
|
||||
});
|
||||
|
||||
if (response.ok) {
|
||||
let parsed = await response.json();
|
||||
} */
|
||||
} catch (error) {
|
||||
throw error;
|
||||
} finally {
|
||||
|
||||
@@ -121,7 +121,7 @@ export async function getSessionData(type: 'token' | 'email' | 'employeId'): Pro
|
||||
|
||||
|
||||
export async function deleteSession() {
|
||||
console.log('delete session')
|
||||
|
||||
try {
|
||||
const cookieStore = await cookies()
|
||||
cookieStore.delete('session_nc_ap');
|
||||
|
||||
@@ -4,6 +4,7 @@ import { API_BASE_URL } from '@/app/actions/definitions';
|
||||
import { getSessionData } from '@/app/actions/session';
|
||||
|
||||
export async function fetchTariffStatistic() {
|
||||
const token = await getSessionData('token');
|
||||
|
||||
try {
|
||||
const response = await fetch(`${API_BASE_URL}/api/admin/info`, {
|
||||
@@ -17,7 +18,8 @@ export async function fetchTariffStatistic() {
|
||||
}),
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'Accept': 'application/json'
|
||||
'Accept': 'application/json',
|
||||
'Authorization': `Bearer ${token}`
|
||||
}
|
||||
});
|
||||
|
||||
@@ -39,6 +41,7 @@ export async function fetchTariffStatistic() {
|
||||
}
|
||||
|
||||
export async function fetchUserFilesTopStatistic() {
|
||||
const token = await getSessionData('token');
|
||||
|
||||
try {
|
||||
const response = await fetch(`${API_BASE_URL}/api/admin/info`, {
|
||||
@@ -52,7 +55,8 @@ export async function fetchUserFilesTopStatistic() {
|
||||
}),
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'Accept': 'application/json'
|
||||
'Accept': 'application/json',
|
||||
'Authorization': `Bearer ${token}`
|
||||
}
|
||||
});
|
||||
|
||||
@@ -74,6 +78,7 @@ export async function fetchUserFilesTopStatistic() {
|
||||
}
|
||||
|
||||
export async function fetchUserGeneralStatistic() {
|
||||
const token = await getSessionData('token');
|
||||
|
||||
try {
|
||||
const response = await fetch(`${API_BASE_URL}/api/admin/info`, {
|
||||
@@ -87,7 +92,8 @@ export async function fetchUserGeneralStatistic() {
|
||||
}),
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'Accept': 'application/json'
|
||||
'Accept': 'application/json',
|
||||
'Authorization': `Bearer ${token}`
|
||||
}
|
||||
});
|
||||
|
||||
@@ -109,6 +115,7 @@ export async function fetchUserGeneralStatistic() {
|
||||
}
|
||||
|
||||
export async function fetchSubscribeGeneralStatistic() {
|
||||
const token = await getSessionData('token');
|
||||
|
||||
try {
|
||||
const response = await fetch(`${API_BASE_URL}/api/admin/info`, {
|
||||
@@ -122,7 +129,8 @@ export async function fetchSubscribeGeneralStatistic() {
|
||||
}),
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'Accept': 'application/json'
|
||||
'Accept': 'application/json',
|
||||
'Authorization': `Bearer ${token}`
|
||||
}
|
||||
});
|
||||
|
||||
@@ -144,6 +152,7 @@ export async function fetchSubscribeGeneralStatistic() {
|
||||
}
|
||||
|
||||
export async function fetchProtectedContentGeneralStatistic() {
|
||||
const token = await getSessionData('token');
|
||||
|
||||
try {
|
||||
const response = await fetch(`${API_BASE_URL}/api/admin/info`, {
|
||||
@@ -157,7 +166,8 @@ export async function fetchProtectedContentGeneralStatistic() {
|
||||
}),
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'Accept': 'application/json'
|
||||
'Accept': 'application/json',
|
||||
'Authorization': `Bearer ${token}`
|
||||
}
|
||||
});
|
||||
|
||||
@@ -179,6 +189,7 @@ export async function fetchProtectedContentGeneralStatistic() {
|
||||
}
|
||||
|
||||
export async function fetchViolationGeneralStatistic() {
|
||||
const token = await getSessionData('token');
|
||||
|
||||
try {
|
||||
const response = await fetch(`${API_BASE_URL}/api/admin/info`, {
|
||||
@@ -192,7 +203,8 @@ export async function fetchViolationGeneralStatistic() {
|
||||
}),
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'Accept': 'application/json'
|
||||
'Accept': 'application/json',
|
||||
'Authorization': `Bearer ${token}`
|
||||
}
|
||||
});
|
||||
|
||||
@@ -214,6 +226,7 @@ export async function fetchViolationGeneralStatistic() {
|
||||
}
|
||||
|
||||
export async function fetchTokensGeneralStatistic() {
|
||||
const token = await getSessionData('token');
|
||||
|
||||
try {
|
||||
const response = await fetch(`${API_BASE_URL}/api/admin/info`, {
|
||||
@@ -227,7 +240,8 @@ export async function fetchTokensGeneralStatistic() {
|
||||
}),
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'Accept': 'application/json'
|
||||
'Accept': 'application/json',
|
||||
'Authorization': `Bearer ${token}`
|
||||
}
|
||||
});
|
||||
|
||||
@@ -249,6 +263,7 @@ export async function fetchTokensGeneralStatistic() {
|
||||
}
|
||||
|
||||
export async function fetchIncomeGeneralStatistic() {
|
||||
const token = await getSessionData('token');
|
||||
|
||||
try {
|
||||
const response = await fetch(`${API_BASE_URL}/api/admin/info`, {
|
||||
@@ -262,7 +277,8 @@ export async function fetchIncomeGeneralStatistic() {
|
||||
}),
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'Accept': 'application/json'
|
||||
'Accept': 'application/json',
|
||||
'Authorization': `Bearer ${token}`
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
@@ -12,7 +12,6 @@ export async function fetchStuffList(page?: number, size?: number, sortBy?: stri
|
||||
body: JSON.stringify({
|
||||
version: 1,
|
||||
msg_id: 100,
|
||||
token: token,
|
||||
message_body: {
|
||||
action: 'get_all',
|
||||
page: page,
|
||||
@@ -24,7 +23,8 @@ export async function fetchStuffList(page?: number, size?: number, sortBy?: stri
|
||||
}),
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'Accept': 'application/json'
|
||||
'Accept': 'application/json',
|
||||
'Authorization': `Bearer ${token}`
|
||||
}
|
||||
});
|
||||
|
||||
@@ -83,7 +83,8 @@ export async function createStuff(fullName: string, email: string, password: str
|
||||
}),
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'Accept': 'application/json'
|
||||
'Accept': 'application/json',
|
||||
'Authorization': `Bearer ${token}`
|
||||
}
|
||||
});
|
||||
|
||||
@@ -124,7 +125,8 @@ export async function removeStuff(id: number) {
|
||||
}),
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'Accept': 'application/json'
|
||||
'Accept': 'application/json',
|
||||
'Authorization': `Bearer ${token}`
|
||||
}
|
||||
});
|
||||
|
||||
@@ -166,7 +168,8 @@ export async function updateStuffPermissions(id: number, permissions: Permission
|
||||
}),
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'Accept': 'application/json'
|
||||
'Accept': 'application/json',
|
||||
'Authorization': `Bearer ${token}`
|
||||
}
|
||||
});
|
||||
|
||||
@@ -204,7 +207,8 @@ export async function fetchEmployeInfo() {
|
||||
}),
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'Accept': 'application/json'
|
||||
'Accept': 'application/json',
|
||||
'Authorization': `Bearer ${token}`
|
||||
}
|
||||
});
|
||||
|
||||
@@ -240,7 +244,8 @@ export async function fetchStuffTemplates() {
|
||||
}),
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'Accept': 'application/json'
|
||||
'Accept': 'application/json',
|
||||
'Authorization': `Bearer ${token}`
|
||||
}
|
||||
});
|
||||
|
||||
@@ -279,7 +284,8 @@ export async function createStuffTemplates(name: string, changePassword: boolean
|
||||
}),
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'Accept': 'application/json'
|
||||
'Accept': 'application/json',
|
||||
'Authorization': `Bearer ${token}`
|
||||
}
|
||||
});
|
||||
|
||||
@@ -316,7 +322,8 @@ export async function removeStuffTemplates(id: number,) {
|
||||
}),
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'Accept': 'application/json'
|
||||
'Accept': 'application/json',
|
||||
'Authorization': `Bearer ${token}`
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
@@ -13,7 +13,6 @@ export async function fetchUsesData(page: number, size: number, sortBy?: string,
|
||||
version: 1,
|
||||
msg_id: 30014,
|
||||
message_body: {
|
||||
/* token: token, */
|
||||
action: 'getAllWithPagination',
|
||||
page: page,
|
||||
size: size,
|
||||
@@ -24,7 +23,8 @@ export async function fetchUsesData(page: number, size: number, sortBy?: string,
|
||||
}),
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'Accept': 'application/json'
|
||||
'Accept': 'application/json',
|
||||
'Authorization': `Bearer ${token}`
|
||||
}
|
||||
});
|
||||
|
||||
@@ -46,6 +46,7 @@ export async function fetchUsesData(page: number, size: number, sortBy?: string,
|
||||
}
|
||||
|
||||
export async function fetchUsesInfo(userInfo: number) {
|
||||
const token = await getSessionData('token');
|
||||
|
||||
try {
|
||||
const response = await fetch(`${API_BASE_URL}/api/admin/info`, {
|
||||
@@ -60,7 +61,8 @@ export async function fetchUsesInfo(userInfo: number) {
|
||||
}),
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'Accept': 'application/json'
|
||||
'Accept': 'application/json',
|
||||
'Authorization': `Bearer ${token}`
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user