Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ import { useQuery } from '@tanstack/vue-query'
import { useClipboard } from '@vueuse/core'
import { computed, ref } from 'vue'

import { runPrivileged } from '~/composables/reauth.ts'
import { ensurePrivileged, runPrivileged } from '~/composables/reauth.ts'

defineOptions({ inheritAttrs: false })

Expand Down Expand Up @@ -94,6 +94,11 @@ provideUserProfile({
const editModal = ref<InstanceType<typeof EditUserModal>>()
const detailsModal = ref<InstanceType<typeof NewModal>>()

async function openEditUser() {
if (!(await ensurePrivileged())) return
editModal.value?.show()
}

const menuOptions = computed<ButtonMenuOption[]>(() => [
{
id: 'copy-id',
Expand Down Expand Up @@ -150,7 +155,9 @@ const menuOptions = computed<ButtonMenuOption[]>(() => [
id: 'edit-user',
label: 'Edit user',
icon: EditIcon,
action: () => editModal.value?.show(),
action: () => {
void openEditUser()
},
tone: 'default',
shown: isAdmin.value,
disabled: !userQuery.data.value || userQuery.isFetching.value,
Expand Down
15 changes: 8 additions & 7 deletions apps/frontend/src/components/ui/auth/PasskeySettings.vue
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,7 @@
<IconButton
v-tooltip="formatMessage(messages.deletePasskeyButton)"
:label="formatMessage(messages.deletePasskeyButton)"
@click="
() => {
passkeyToRemove = passkey
removePasskeyModal?.show()
}
"
@click="confirmRemovePasskey(passkey)"
>
<TrashIcon />
</IconButton>
Expand Down Expand Up @@ -218,7 +213,7 @@ import {
} from '@modrinth/ui'
import { computed, ref, useTemplateRef } from 'vue'

import { isReauthCancelled, runPrivileged } from '~/composables/reauth.ts'
import { ensurePrivileged, isReauthCancelled, runPrivileged } from '~/composables/reauth.ts'
import { createPasskeyCredential } from '~/helpers/passkey.ts'

const { labrinth } = injectModrinthClient()
Expand Down Expand Up @@ -418,6 +413,12 @@ async function renamePasskey() {
stopLoading()
}

async function confirmRemovePasskey(passkey: Labrinth.Auth.v2.Passkey) {
if (!(await ensurePrivileged())) return
passkeyToRemove.value = passkey
removePasskeyModal.value?.show()
}

async function removePasskey() {
if (!passkeyToRemove.value) return
const { id } = passkeyToRemove.value
Expand Down
19 changes: 19 additions & 0 deletions apps/frontend/src/composables/reauth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,25 @@ async function openReauth(): Promise<void> {
}
}

/**
* Reauthenticate before a privileged flow collects input.
* A missing session age counts as not privileged, so save does not discover that after the form is filled.
* Returns false when reauthentication is cancelled.
*/
export async function ensurePrivileged(required = true): Promise<boolean> {
try {
if (required && isSessionYoung(useAuthState().value.token) !== true) {
await requestReauth()
Comment thread
Prospector marked this conversation as resolved.
Resolved
}
return true
} catch (error) {
if (isReauthCancelled(error)) {
return false
}
throw error
}
}

export async function runPrivileged(action: () => Promise<void>, precheck = true): Promise<void> {
if (precheck && isSessionYoung(useAuthState().value.token) === false) {
await requestReauth()
Expand Down
1 change: 1 addition & 0 deletions apps/frontend/src/composables/sign-in.ts
Original file line number Diff line number Diff line change
Expand Up @@ -589,6 +589,7 @@ export function provideSignIn() {
code,
verifier,
})
rememberSessionCreated(session.session, session.created)
await finishSignIn(session.session, session.app_session, readPendingAuthMethod())
} catch (error) {
console.error(error)
Expand Down
12 changes: 10 additions & 2 deletions apps/frontend/src/pages/[type]/[project]/settings/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@
type="colored"
color="red"
:disabled="!hasDeletePermission"
@click="$refs.modal_confirm.show()"
@click="confirmDeleteProject"
>
<TrashIcon aria-hidden="true" />
{{ formatMessage(messages.deleteProjectButton) }}
Expand Down Expand Up @@ -382,7 +382,7 @@ import {
useProjectSlugSuggestions,
useSlugSuggestionVisibility,
} from '~/composables/project-slug-suggestions'
import { isReauthCancelled, runPrivileged } from '~/composables/reauth.ts'
import { ensurePrivileged, isReauthCancelled, runPrivileged } from '~/composables/reauth.ts'
import { fileDeclaresAi } from '~/helpers/c2pa'
import { getProjectTypeForUrl } from '~/helpers/projects.js'

Expand Down Expand Up @@ -776,6 +776,14 @@ const deleteBanner = async () => {
}
}

const modal_confirm = ref(null)

const confirmDeleteProject = async () => {
const requiresPrivileged = tags.value.approvedStatuses.includes(project.value.status)
if (!(await ensurePrivileged(requiresPrivileged))) return
modal_confirm.value?.show()
}

const deleteProject = async () => {
try {
await runPrivileged(
Expand Down
2 changes: 2 additions & 0 deletions apps/frontend/src/pages/dashboard/revenue/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,7 @@ import { useQuery } from '@tanstack/vue-query'
import dayjs from 'dayjs'

import { useUserCountry } from '@/composables/country.ts'
import { ensurePrivileged } from '@/composables/reauth.ts'
import CreatorWithdrawModal from '~/components/ui/dashboard/CreatorWithdrawModal.vue'
import RevenueTransaction from '~/components/ui/dashboard/RevenueTransaction.vue'

Expand All @@ -297,6 +298,7 @@ const hoveredSeg = ref<string | null>(null)

const withdrawModal = ref<InstanceType<typeof CreatorWithdrawModal>>()
async function openWithdrawModal() {
if (!(await ensurePrivileged())) return
withdrawModal.value?.show?.()
}

Expand Down
54 changes: 39 additions & 15 deletions apps/frontend/src/pages/settings/account.vue
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,7 @@
}}</span>
</label>
<div>
<Button @click="$refs.changeEmailModal.show()">
<Button @click="openChangeEmail">
<template v-if="auth.user.email">
<EditIcon />
{{ formatMessage(messages.changeEmailButton) }}
Expand All @@ -443,17 +443,7 @@
</span>
</label>
<div>
<Button
@click="
() => {
oldPassword = ''
newPassword = ''
confirmNewPassword = ''
removePasswordMode = false
$refs.managePasswordModal.show()
}
"
>
<Button @click="openPasswordModal">
<KeyIcon />
<template v-if="auth.user.has_password">{{
formatMessage(messages.changePasswordButton)
Expand Down Expand Up @@ -488,7 +478,7 @@
}}</span>
</label>
<div>
<Button @click="$refs.manageProvidersModal.show()">
<Button @click="openManageProviders">
<SettingsIcon /> {{ formatMessage(messages.manageProvidersButton) }}
</Button>
</div>
Expand Down Expand Up @@ -516,7 +506,7 @@
<section id="delete-account" class="universal-card">
<h2>{{ formatMessage(messages.deleteAccountSectionTitle) }}</h2>
<p>{{ formatMessage(messages.deleteAccountSectionDescription) }}</p>
<Button type="colored" color="red" native-type="button" @click="$refs.modal_confirm.show()">
<Button type="colored" color="red" native-type="button" @click="openDeleteAccount">
<TrashIcon />
{{ formatMessage(messages.deleteAccountButton) }}
</Button>
Expand Down Expand Up @@ -569,7 +559,12 @@ import TwoFactorAuthCodeInput from '~/components/ui/auth/TwoFactorAuthCodeInput.
import { forgetStoredAccount, isSessionYoung } from '~/composables/accounts.ts'
import { getAuthUrl, removeAuthProvider } from '~/composables/auth.ts'
import { useAuthCookie } from '~/composables/auth-cookie.ts'
import { isReauthCancelled, requestReauth, runPrivileged } from '~/composables/reauth.ts'
import {
ensurePrivileged,
isReauthCancelled,
requestReauth,
runPrivileged,
} from '~/composables/reauth.ts'

definePageMeta({
middleware: 'auth',
Expand Down Expand Up @@ -881,7 +876,25 @@ const messages = defineMessages({
})

const changeEmailModal = ref()
const manageProvidersModal = ref()
const modal_confirm = ref()
const email = ref(auth.value.user.email)

async function openChangeEmail() {
if (!(await ensurePrivileged(Boolean(auth.value.user.email)))) return
changeEmailModal.value.show()
}

async function openManageProviders() {
if (!(await ensurePrivileged())) return
manageProvidersModal.value.show()
}

async function openDeleteAccount() {
if (!(await ensurePrivileged())) return
modal_confirm.value.show()
}

async function saveEmail() {
if (!email.value) {
return
Expand Down Expand Up @@ -942,6 +955,16 @@ const removePasswordMode = ref(false)
const oldPassword = ref('')
const newPassword = ref('')
const confirmNewPassword = ref('')

async function openPasswordModal() {
if (!(await ensurePrivileged())) return
oldPassword.value = ''
newPassword.value = ''
confirmNewPassword.value = ''
removePasswordMode.value = false
managePasswordModal.value.show()
}

async function savePassword() {
if (newPassword.value !== confirmNewPassword.value) {
return
Expand Down Expand Up @@ -980,6 +1003,7 @@ const twoFactorCodeInput = ref()
const removeTwoFactorCodeInput = ref()
const twoFactorSubmitting = ref(false)
async function showTwoFactorModal() {
if (!(await ensurePrivileged())) return
twoFactorStep.value = 0
twoFactorCode.value = ''
twoFactorIncorrect.value = false
Expand Down
5 changes: 3 additions & 2 deletions apps/frontend/src/pages/settings/applications.vue
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ import {
toggleScope,
useScopes,
} from '~/composables/auth/scopes.ts'
import { isReauthCancelled, runPrivileged } from '~/composables/reauth.ts'
import { ensurePrivileged, isReauthCancelled, runPrivileged } from '~/composables/reauth.ts'

const { data: globals } = useGlobals()

Expand Down Expand Up @@ -594,7 +594,8 @@ const {
enabled: computed(() => !!auth.value?.user?.id),
})

function openCreateApp() {
async function openCreateApp() {
if (!(await ensurePrivileged())) return
name.value = null
icon.value = null
scopesVal.value = BigInt(0)
Expand Down
11 changes: 7 additions & 4 deletions apps/frontend/src/pages/settings/pats.vue
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ import {
toggleScope,
useScopes,
} from '~/composables/auth/scopes.ts'
import { isReauthCancelled, runPrivileged } from '~/composables/reauth.ts'
import { ensurePrivileged, isReauthCancelled, runPrivileged } from '~/composables/reauth.ts'

const client = injectModrinthClient()
const queryClient = useQueryClient()
Expand Down Expand Up @@ -548,23 +548,26 @@ const scopeCategories = computed(() => {
return categories.filter((c) => c.scopes.length > 0)
})

function openCreatePat() {
async function openCreatePat() {
if (!(await ensurePrivileged())) return
name.value = null
scopesVal.value = BigInt(0)
expires.value = null
editPatId.value = null
patModal.value.show()
}

function openEditPat(pat) {
async function openEditPat(pat) {
if (!(await ensurePrivileged())) return
editPatId.value = pat.id
name.value = pat.name
scopesVal.value = BigInt(pat.scopes)
expires.value = data.$dayjs(pat.expires).format('YYYY-MM-DD')
patModal.value.show()
}

function confirmRemovePat(id) {
async function confirmRemovePat(id) {
if (!(await ensurePrivileged())) return
deletePatIndex.value = id
modal_confirm.value.show()
}
Expand Down
7 changes: 6 additions & 1 deletion apps/frontend/src/pages/settings/profile.vue
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
:change-avatar="changeAvatar"
:delete-avatar="deleteAvatar"
:get-authenticated-user="getAuthenticatedUser"
:prepare-username-change="prepareUsernameChange"
disclaimer-position="bottom"
/>
<UnsavedChangesPopup
Expand All @@ -30,7 +31,7 @@ import {
} from '@modrinth/ui'

import RestrictedActionAlert from '~/components/ui/create/RestrictedActionAlert.vue'
import { runPrivileged } from '~/composables/reauth.ts'
import { ensurePrivileged, runPrivileged } from '~/composables/reauth.ts'

definePageMeta({
middleware: 'auth',
Expand All @@ -46,6 +47,10 @@ const emptyProfileState = {
avatarChanged: false,
}

function prepareUsernameChange(): Promise<boolean> {
return ensurePrivileged()
}

function patchUser(
userId: string,
patch: Partial<Pick<Labrinth.Users.v2.User, 'bio' | 'username'>>,
Expand Down
1 change: 1 addition & 0 deletions packages/ui/src/components/base/inputs/Input.vue
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ const showClearButton = computed(

defineExpose({
focus: () => inputRef.value?.focus(),
blur: () => inputRef.value?.blur(),
select: () => inputRef.value?.select(),
})

Expand Down
Loading
Loading