232 lines
7.9 KiB
Plaintext
232 lines
7.9 KiB
Plaintext
---
|
|
import Layout from '../../layouts/Layout.astro';
|
|
import NavBar from '../../components/NavBar.astro';
|
|
import { getUserByAccessToken, getUserByNickOrEmail } from '../../lib/db/users';
|
|
import { getHighestWeightedLanguage, getLocales, getName } from '../../lib/utils/langDriver';
|
|
import ResourceBar from '../../components/ResourceBar.astro';
|
|
import format from '../../lib/utils/format';
|
|
|
|
const loggedToken = Astro.cookies.get('sessionToken')?.value ?? null;
|
|
const username = Astro.cookies.get('username')?.value ?? "";
|
|
if(loggedToken === null || username === "") return Astro.redirect('/logout');
|
|
|
|
const checkUser = await getUserByAccessToken(loggedToken);
|
|
if(checkUser === null || checkUser.username !== username) return Astro.redirect('/logout');
|
|
|
|
const locale = await getHighestWeightedLanguage(Astro.request.headers.get('accept-language'));
|
|
|
|
const user = await getUserByNickOrEmail(username);
|
|
|
|
const lang = await getLocales(locale);
|
|
---
|
|
|
|
<Layout title="Profile">
|
|
<NavBar loggedIn="true" active="profile" />
|
|
<ResourceBar loggedIn="true" />
|
|
|
|
<div class="wrapper">
|
|
<h3>{format(getName(lang, 'general', 'user-creation-date'), user?.createdAt.toISOString().slice(0, 19).replace(/-/g, "/").replace("T", " ").toString() ?? "")}</h3>
|
|
<a href="/logout" class="a-button">{getName(lang, 'general', 'nav-logout')}</a>
|
|
</div>
|
|
<form id="changeUsernameForm" class="data-form">
|
|
<input class="data-form-input" type="text" name="username" placeholder={getName(lang, 'general', 'input-placeholder-new-username')} />
|
|
<input class="data-form-input" type="password" name="password" placeholder={getName(lang, 'general', 'input-placeholder-password')} />
|
|
<input class="data-form-button" type="button" value={getName(lang, 'general', 'change-username')} />
|
|
</form>
|
|
<form id="changeEmailForm" class="data-form">
|
|
<input class="data-form-input" type="email" name="email" placeholder={getName(lang, 'general', 'input-placeholder-new-email')} />
|
|
<input class="data-form-input" type="password" name="password" placeholder={getName(lang, 'general', 'input-placeholder-password')} />
|
|
<input class="data-form-button" type="button" value={getName(lang, 'general', 'change-email')} />
|
|
</form>
|
|
<form id="changePasswordForm" class="data-form">
|
|
<input class="data-form-input" type="password" name="oldPassword" placeholder={getName(lang, 'general', 'input-placeholder-old-password')} />
|
|
<input class="data-form-input" type="password" name="password1" placeholder={getName(lang, 'general', 'input-placeholder-new-password')} />
|
|
<input class="data-form-input" type="password" name="password2" placeholder={getName(lang, 'general', 'input-placeholder-new-password-verify')} />
|
|
<input class="data-form-button" type="button" value={getName(lang, 'general', 'change-password')} />
|
|
</form>
|
|
</Layout>
|
|
|
|
<style>
|
|
* {
|
|
color: white;
|
|
}
|
|
|
|
.data-form {
|
|
display: flex;
|
|
flex-direction: column;
|
|
align-items: center;
|
|
justify-content: center;
|
|
margin-top: 2rem;
|
|
}
|
|
|
|
.data-form-input {
|
|
color: black;
|
|
}
|
|
|
|
.data-form-button {
|
|
color: red;
|
|
font-weight: 700;
|
|
}
|
|
|
|
.wrapper {
|
|
display: flex;
|
|
flex-direction: column;
|
|
align-items: center;
|
|
justify-content: center;
|
|
margin-top: 2rem;
|
|
color: white;
|
|
}
|
|
|
|
main {
|
|
margin: auto;
|
|
padding: 1rem;
|
|
width: 800px;
|
|
max-width: calc(100% - 2rem);
|
|
color: white;
|
|
font-size: 20px;
|
|
line-height: 1.6;
|
|
}
|
|
|
|
.astro-a {
|
|
position: absolute;
|
|
top: -32px;
|
|
left: 50%;
|
|
transform: translatex(-50%);
|
|
width: 220px;
|
|
height: auto;
|
|
z-index: -1;
|
|
}
|
|
|
|
h3 {
|
|
font-size: 2rem;
|
|
font-weight: 700;
|
|
line-height: 1;
|
|
text-align: center;
|
|
margin-bottom: 1em;
|
|
}
|
|
|
|
.text-gradient {
|
|
background-image: var(--accent-gradient);
|
|
-webkit-background-clip: text;
|
|
-webkit-text-fill-color: transparent;
|
|
background-size: 400%;
|
|
background-position: 0%;
|
|
}
|
|
|
|
.instructions {
|
|
margin-bottom: 2rem;
|
|
border: 1px solid rgba(var(--accent-light), 25%);
|
|
background: linear-gradient(rgba(var(--accent-dark), 66%), rgba(var(--accent-dark), 33%));
|
|
padding: 1.5rem;
|
|
border-radius: 8px;
|
|
}
|
|
|
|
.instructions code {
|
|
font-size: 0.8em;
|
|
font-weight: bold;
|
|
background: rgba(var(--accent-light), 12%);
|
|
color: rgb(var(--accent-light));
|
|
border-radius: 4px;
|
|
padding: 0.3em 0.4em;
|
|
}
|
|
|
|
.instructions strong {
|
|
color: rgb(var(--accent-light));
|
|
}
|
|
|
|
.link-card-grid {
|
|
display: grid;
|
|
grid-template-columns: repeat(auto-fit, minmax(24ch, 1fr));
|
|
gap: 2rem;
|
|
padding: 0;
|
|
}
|
|
|
|
.a-button {
|
|
text-decoration: none;
|
|
color: green;
|
|
}
|
|
|
|
.a-button:hover {
|
|
color: lime;
|
|
}
|
|
</style>
|
|
<script>
|
|
const changeUsernameForm = document.getElementById('changeUsernameForm');
|
|
if(changeUsernameForm === null) console.error("changeUsernameForm is null");
|
|
else {
|
|
changeUsernameForm.querySelector('input[type="button"]')?.addEventListener("click", async () => {
|
|
const newUsername = (changeUsernameForm.querySelector('input[name="username"]') as HTMLInputElement).value;
|
|
const password = (changeUsernameForm.querySelector('input[name="password"]') as HTMLInputElement).value;
|
|
|
|
const request = await fetch('/api/auth/changeUserData/username', {
|
|
method: 'PATCH',
|
|
headers: {
|
|
'Content-Type': 'application/json'
|
|
},
|
|
body: JSON.stringify({
|
|
newUsername,
|
|
password
|
|
})
|
|
});
|
|
|
|
const response = await request.json();
|
|
console.log(response);
|
|
|
|
window.location.reload();
|
|
});
|
|
}
|
|
|
|
const changeEmailForm = document.getElementById('changeEmailForm');
|
|
if(changeEmailForm === null) console.error("changeEmailForm is null");
|
|
else {
|
|
changeEmailForm.querySelector('input[type="button"]')?.addEventListener("click", async () => {
|
|
const newEmail = (changeEmailForm.querySelector('input[name="email"]') as HTMLInputElement).value;
|
|
const password = (changeEmailForm.querySelector('input[name="password"]') as HTMLInputElement).value;
|
|
|
|
const request = await fetch('/api/auth/changeUserData/email', {
|
|
method: 'PATCH',
|
|
headers: {
|
|
'Content-Type': 'application/json'
|
|
},
|
|
body: JSON.stringify({
|
|
newEmail,
|
|
password
|
|
})
|
|
});
|
|
|
|
const response = await request.json();
|
|
console.log(response);
|
|
|
|
window.location.reload();
|
|
});
|
|
}
|
|
|
|
const changePasswordForm = document.getElementById('changePasswordForm');
|
|
if(changePasswordForm === null) console.error("changePasswordForm is null");
|
|
else {
|
|
changePasswordForm.querySelector('input[type="button"]')?.addEventListener("click", async () => {
|
|
const oldPassword = (changePasswordForm.querySelector('input[name="oldPassword"]') as HTMLInputElement).value;
|
|
const password1 = (changePasswordForm.querySelector('input[name="password1"]') as HTMLInputElement).value;
|
|
const password2 = (changePasswordForm.querySelector('input[name="password2"]') as HTMLInputElement).value;
|
|
|
|
if(password1 !== password2) return alert("Passwords don't match");
|
|
else {
|
|
const request = await fetch('/api/auth/changeUserData/password', {
|
|
method: 'PATCH',
|
|
headers: {
|
|
'Content-Type': 'application/json'
|
|
},
|
|
body: JSON.stringify({
|
|
password: oldPassword,
|
|
newPassword: password1
|
|
})
|
|
});
|
|
|
|
const response = await request.json();
|
|
console.log(response);
|
|
|
|
window.location.reload();
|
|
}
|
|
});
|
|
}
|
|
</script> |