2024-07-08 11:44:39 -04:00
|
|
|
import { fail, redirect, type Actions } from '@sveltejs/kit';
|
|
|
|
import type { PageServerLoad } from '../$types';
|
|
|
|
const PUBLIC_SERVER_URL = process.env['PUBLIC_SERVER_URL'];
|
2025-01-02 13:34:51 -05:00
|
|
|
import type { ImmichIntegration, User } from '$lib/types';
|
2024-11-29 18:20:51 -05:00
|
|
|
import { fetchCSRFToken } from '$lib/index.server';
|
2024-07-08 11:44:39 -04:00
|
|
|
const endpoint = PUBLIC_SERVER_URL || 'http://localhost:8000';
|
|
|
|
|
2024-12-12 19:20:58 -05:00
|
|
|
type MFAAuthenticatorResponse = {
|
|
|
|
status: number;
|
|
|
|
data: {
|
|
|
|
type: string;
|
|
|
|
created_at: number;
|
|
|
|
last_used_at: number | null;
|
|
|
|
total_code_count?: number;
|
|
|
|
unused_code_count?: number;
|
|
|
|
}[];
|
|
|
|
};
|
|
|
|
|
2024-07-08 11:44:39 -04:00
|
|
|
export const load: PageServerLoad = async (event) => {
|
|
|
|
if (!event.locals.user) {
|
|
|
|
return redirect(302, '/');
|
|
|
|
}
|
2024-11-29 18:20:51 -05:00
|
|
|
let sessionId = event.cookies.get('sessionid');
|
|
|
|
if (!sessionId) {
|
2024-07-08 11:44:39 -04:00
|
|
|
return redirect(302, '/');
|
|
|
|
}
|
2024-11-30 10:24:27 -05:00
|
|
|
let res = await fetch(`${endpoint}/auth/user-metadata/`, {
|
2024-07-08 11:44:39 -04:00
|
|
|
headers: {
|
2024-11-29 18:20:51 -05:00
|
|
|
Cookie: `sessionid=${sessionId}`
|
2024-07-08 11:44:39 -04:00
|
|
|
}
|
|
|
|
});
|
|
|
|
let user = (await res.json()) as User;
|
|
|
|
|
2025-02-23 17:04:20 -05:00
|
|
|
let emailFetch = await fetch(`${endpoint}/auth/browser/v1/account/email`, {
|
2024-12-07 16:15:41 -05:00
|
|
|
headers: {
|
|
|
|
Cookie: `sessionid=${sessionId}`
|
|
|
|
}
|
|
|
|
});
|
|
|
|
let emailResponse = (await emailFetch.json()) as {
|
|
|
|
status: number;
|
|
|
|
data: { email: string; verified: boolean; primary: boolean }[];
|
|
|
|
};
|
|
|
|
let emails = emailResponse.data;
|
|
|
|
if (!res.ok || !emailFetch.ok) {
|
2024-07-08 11:44:39 -04:00
|
|
|
return redirect(302, '/');
|
|
|
|
}
|
|
|
|
|
2025-02-23 17:04:20 -05:00
|
|
|
let mfaAuthenticatorFetch = await fetch(`${endpoint}/auth/browser/v1/account/authenticators`, {
|
|
|
|
headers: {
|
|
|
|
Cookie: `sessionid=${sessionId}`
|
2024-12-12 19:20:58 -05:00
|
|
|
}
|
2025-02-23 17:04:20 -05:00
|
|
|
});
|
2024-12-12 19:20:58 -05:00
|
|
|
let mfaAuthenticatorResponse = (await mfaAuthenticatorFetch.json()) as MFAAuthenticatorResponse;
|
2024-12-13 10:48:18 -05:00
|
|
|
let authenticators = (mfaAuthenticatorResponse.data.length > 0) as boolean;
|
2024-12-12 19:20:58 -05:00
|
|
|
|
2025-01-02 13:34:51 -05:00
|
|
|
let immichIntegration: ImmichIntegration | null = null;
|
|
|
|
let immichIntegrationsFetch = await fetch(`${endpoint}/api/integrations/immich/`, {
|
|
|
|
headers: {
|
|
|
|
Cookie: `sessionid=${sessionId}`
|
|
|
|
}
|
|
|
|
});
|
|
|
|
if (immichIntegrationsFetch.ok) {
|
|
|
|
immichIntegration = await immichIntegrationsFetch.json();
|
|
|
|
}
|
|
|
|
|
2025-03-17 10:38:41 -04:00
|
|
|
let socialProvidersFetch = await fetch(`${endpoint}/auth/social-providers`, {
|
|
|
|
headers: {
|
|
|
|
Cookie: `sessionid=${sessionId}`
|
|
|
|
}
|
|
|
|
});
|
|
|
|
let socialProviders = await socialProvidersFetch.json();
|
|
|
|
|
2025-01-06 18:53:08 -05:00
|
|
|
let publicUrlFetch = await fetch(`${endpoint}/public-url/`);
|
|
|
|
let publicUrl = '';
|
|
|
|
if (!publicUrlFetch.ok) {
|
|
|
|
return redirect(302, '/');
|
|
|
|
} else {
|
|
|
|
let publicUrlJson = await publicUrlFetch.json();
|
|
|
|
publicUrl = publicUrlJson.PUBLIC_URL;
|
|
|
|
}
|
|
|
|
|
2024-07-08 11:44:39 -04:00
|
|
|
return {
|
|
|
|
props: {
|
2024-12-07 16:15:41 -05:00
|
|
|
user,
|
2024-12-12 19:20:58 -05:00
|
|
|
emails,
|
2025-01-02 13:34:51 -05:00
|
|
|
authenticators,
|
2025-01-06 18:53:08 -05:00
|
|
|
immichIntegration,
|
2025-03-17 10:38:41 -04:00
|
|
|
publicUrl,
|
|
|
|
socialProviders
|
2024-07-08 11:44:39 -04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
export const actions: Actions = {
|
|
|
|
changeDetails: async (event) => {
|
|
|
|
if (!event.locals.user) {
|
|
|
|
return redirect(302, '/');
|
|
|
|
}
|
2024-11-29 18:20:51 -05:00
|
|
|
let sessionId = event.cookies.get('sessionid');
|
|
|
|
if (!sessionId) {
|
2024-07-08 11:44:39 -04:00
|
|
|
return redirect(302, '/');
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
const formData = await event.request.formData();
|
|
|
|
|
|
|
|
let username = formData.get('username') as string | null | undefined;
|
|
|
|
let first_name = formData.get('first_name') as string | null | undefined;
|
|
|
|
let last_name = formData.get('last_name') as string | null | undefined;
|
|
|
|
let profile_pic = formData.get('profile_pic') as File | null | undefined;
|
2024-09-08 13:53:50 -04:00
|
|
|
let public_profile = formData.get('public_profile') as string | null | undefined | boolean;
|
2024-07-08 11:44:39 -04:00
|
|
|
|
2024-11-30 10:24:27 -05:00
|
|
|
const resCurrent = await fetch(`${endpoint}/auth/user-metadata/`, {
|
2024-07-08 11:44:39 -04:00
|
|
|
headers: {
|
2025-01-17 20:20:56 -05:00
|
|
|
Cookie: `sessionid=${sessionId}`,
|
|
|
|
Referer: event.url.origin // Include Referer header
|
2024-07-08 11:44:39 -04:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!resCurrent.ok) {
|
|
|
|
return fail(resCurrent.status, await resCurrent.json());
|
|
|
|
}
|
|
|
|
|
2024-11-30 10:24:27 -05:00
|
|
|
// Gets the boolean value of the public_profile input
|
2024-09-08 13:53:50 -04:00
|
|
|
if (public_profile === 'on') {
|
|
|
|
public_profile = true;
|
|
|
|
} else {
|
|
|
|
public_profile = false;
|
|
|
|
}
|
|
|
|
|
2024-07-08 11:44:39 -04:00
|
|
|
let currentUser = (await resCurrent.json()) as User;
|
|
|
|
|
2024-07-09 11:30:05 -04:00
|
|
|
if (username === currentUser.username || !username) {
|
2024-07-08 11:44:39 -04:00
|
|
|
username = undefined;
|
|
|
|
}
|
2024-07-09 11:30:05 -04:00
|
|
|
if (first_name === currentUser.first_name || !first_name) {
|
2024-07-08 11:44:39 -04:00
|
|
|
first_name = undefined;
|
|
|
|
}
|
2024-07-09 11:30:05 -04:00
|
|
|
if (last_name === currentUser.last_name || !last_name) {
|
2024-07-08 11:44:39 -04:00
|
|
|
last_name = undefined;
|
|
|
|
}
|
2024-07-09 11:30:05 -04:00
|
|
|
if (currentUser.profile_pic && profile_pic?.size === 0) {
|
2024-07-08 11:44:39 -04:00
|
|
|
profile_pic = undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
let formDataToSend = new FormData();
|
2024-11-30 10:24:27 -05:00
|
|
|
|
2024-07-08 11:44:39 -04:00
|
|
|
if (username) {
|
|
|
|
formDataToSend.append('username', username);
|
|
|
|
}
|
|
|
|
if (first_name) {
|
|
|
|
formDataToSend.append('first_name', first_name);
|
|
|
|
}
|
|
|
|
if (last_name) {
|
|
|
|
formDataToSend.append('last_name', last_name);
|
|
|
|
}
|
|
|
|
if (profile_pic) {
|
|
|
|
formDataToSend.append('profile_pic', profile_pic);
|
|
|
|
}
|
2024-09-08 13:53:50 -04:00
|
|
|
formDataToSend.append('public_profile', public_profile.toString());
|
2024-07-08 11:44:39 -04:00
|
|
|
|
2024-11-29 18:20:51 -05:00
|
|
|
let csrfToken = await fetchCSRFToken();
|
|
|
|
|
2024-11-30 10:24:27 -05:00
|
|
|
let res = await fetch(`${endpoint}/auth/update-user/`, {
|
2024-07-08 11:44:39 -04:00
|
|
|
method: 'PATCH',
|
|
|
|
headers: {
|
2025-01-17 20:20:56 -05:00
|
|
|
Referer: event.url.origin, // Include Referer header
|
2024-11-29 18:20:51 -05:00
|
|
|
Cookie: `sessionid=${sessionId}; csrftoken=${csrfToken}`,
|
|
|
|
'X-CSRFToken': csrfToken
|
2024-07-08 11:44:39 -04:00
|
|
|
},
|
|
|
|
body: formDataToSend
|
|
|
|
});
|
|
|
|
|
|
|
|
let response = await res.json();
|
|
|
|
|
|
|
|
if (!res.ok) {
|
|
|
|
return fail(res.status, response);
|
|
|
|
}
|
|
|
|
|
|
|
|
return { success: true };
|
|
|
|
} catch (error) {
|
|
|
|
console.error('Error:', error);
|
2024-12-12 19:20:58 -05:00
|
|
|
return { error: 'settings.generic_error' };
|
2024-07-08 11:44:39 -04:00
|
|
|
}
|
|
|
|
},
|
|
|
|
changePassword: async (event) => {
|
|
|
|
if (!event.locals.user) {
|
|
|
|
return redirect(302, '/');
|
|
|
|
}
|
2024-11-29 18:20:51 -05:00
|
|
|
let sessionId = event.cookies.get('sessionid');
|
|
|
|
if (!sessionId) {
|
2024-07-08 11:44:39 -04:00
|
|
|
return redirect(302, '/');
|
|
|
|
}
|
2024-11-30 10:24:27 -05:00
|
|
|
|
2024-07-08 11:44:39 -04:00
|
|
|
const formData = await event.request.formData();
|
|
|
|
|
|
|
|
const password1 = formData.get('password1') as string | null | undefined;
|
|
|
|
const password2 = formData.get('password2') as string | null | undefined;
|
2025-01-06 18:53:08 -05:00
|
|
|
let current_password = formData.get('current_password') as string | null | undefined;
|
2024-07-08 11:44:39 -04:00
|
|
|
|
|
|
|
if (password1 !== password2) {
|
2024-12-12 19:20:58 -05:00
|
|
|
return fail(400, { message: 'settings.password_does_not_match' });
|
2024-07-08 11:44:39 -04:00
|
|
|
}
|
2025-01-09 18:25:51 -05:00
|
|
|
|
2024-11-30 10:24:27 -05:00
|
|
|
if (!current_password) {
|
2025-01-06 18:53:08 -05:00
|
|
|
current_password = null;
|
2024-11-30 10:24:27 -05:00
|
|
|
}
|
2024-07-08 11:44:39 -04:00
|
|
|
|
2025-01-09 18:25:51 -05:00
|
|
|
if (password1 && password1?.length < 6) {
|
|
|
|
return fail(400, { message: 'settings.password_too_short' });
|
|
|
|
}
|
|
|
|
|
2024-11-29 18:20:51 -05:00
|
|
|
let csrfToken = await fetchCSRFToken();
|
|
|
|
|
2025-01-06 18:53:08 -05:00
|
|
|
if (current_password) {
|
2025-02-23 17:04:20 -05:00
|
|
|
let res = await fetch(`${endpoint}/auth/browser/v1/account/password/change`, {
|
2025-01-06 18:53:08 -05:00
|
|
|
method: 'POST',
|
|
|
|
headers: {
|
2025-01-17 20:20:56 -05:00
|
|
|
Referer: event.url.origin, // Include Referer header
|
2025-01-06 18:53:08 -05:00
|
|
|
Cookie: `sessionid=${sessionId}; csrftoken=${csrfToken}`,
|
|
|
|
'X-CSRFToken': csrfToken,
|
|
|
|
'Content-Type': 'application/json'
|
|
|
|
},
|
|
|
|
body: JSON.stringify({
|
|
|
|
current_password,
|
|
|
|
new_password: password1
|
|
|
|
})
|
|
|
|
});
|
|
|
|
if (!res.ok) {
|
|
|
|
return fail(res.status, { message: 'settings.error_change_password' });
|
|
|
|
}
|
|
|
|
return { success: true };
|
|
|
|
} else {
|
2025-02-23 17:04:20 -05:00
|
|
|
let res = await fetch(`${endpoint}/auth/browser/v1/account/password/change`, {
|
2025-01-06 18:53:08 -05:00
|
|
|
method: 'POST',
|
|
|
|
headers: {
|
2025-01-17 20:20:56 -05:00
|
|
|
Referer: event.url.origin, // Include Referer header
|
2025-01-06 18:53:08 -05:00
|
|
|
Cookie: `sessionid=${sessionId}; csrftoken=${csrfToken}`,
|
|
|
|
'X-CSRFToken': csrfToken,
|
|
|
|
'Content-Type': 'application/json'
|
|
|
|
},
|
|
|
|
body: JSON.stringify({
|
|
|
|
new_password: password1
|
|
|
|
})
|
|
|
|
});
|
|
|
|
if (!res.ok) {
|
|
|
|
console.log('Error:', await res.json());
|
|
|
|
return fail(res.status, { message: 'settings.error_change_password' });
|
|
|
|
}
|
|
|
|
return { success: true };
|
|
|
|
}
|
2024-07-09 11:39:07 -04:00
|
|
|
},
|
|
|
|
changeEmail: async (event) => {
|
|
|
|
if (!event.locals.user) {
|
|
|
|
return redirect(302, '/');
|
|
|
|
}
|
2024-11-29 18:20:51 -05:00
|
|
|
let sessionId = event.cookies.get('sessionid');
|
|
|
|
if (!sessionId) {
|
2024-07-09 11:39:07 -04:00
|
|
|
return redirect(302, '/');
|
|
|
|
}
|
|
|
|
const formData = await event.request.formData();
|
|
|
|
const new_email = formData.get('new_email') as string | null | undefined;
|
|
|
|
if (!new_email) {
|
2024-12-12 19:20:58 -05:00
|
|
|
return fail(400, { message: 'auth.email_required' });
|
2024-07-09 11:39:07 -04:00
|
|
|
} else {
|
2024-11-29 18:20:51 -05:00
|
|
|
let csrfToken = await fetchCSRFToken();
|
2024-07-09 11:39:07 -04:00
|
|
|
let res = await fetch(`${endpoint}/auth/change-email/`, {
|
|
|
|
method: 'POST',
|
|
|
|
headers: {
|
2025-01-17 20:20:56 -05:00
|
|
|
Referer: event.url.origin, // Include Referer header
|
2024-11-29 18:20:51 -05:00
|
|
|
Cookie: `sessionid=${sessionId}; csrftoken=${csrfToken}`,
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
'X-CSRFToken': csrfToken
|
2024-07-09 11:39:07 -04:00
|
|
|
},
|
|
|
|
body: JSON.stringify({
|
|
|
|
new_email
|
|
|
|
})
|
|
|
|
});
|
|
|
|
if (!res.ok) {
|
|
|
|
return fail(res.status, await res.json());
|
|
|
|
}
|
|
|
|
return { success: true };
|
|
|
|
}
|
2024-07-08 11:44:39 -04:00
|
|
|
}
|
|
|
|
};
|