-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
hywax
committed
Aug 10, 2024
1 parent
64d2432
commit b828223
Showing
19 changed files
with
600 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
<template> | ||
<div> | ||
<h1 class="text-3xl font-semibold mb-6 text-black dark:text-white text-center"> | ||
{{ $t('auth.reset.title') }} | ||
</h1> | ||
<p class="text-sm mb-6 text-gray-500 dark:text-gray-400 text-center"> | ||
{{ $t('auth.reset.description') }} | ||
</p> | ||
|
||
<UForm | ||
ref="form" | ||
class="space-y-4" | ||
:state="state" | ||
:schema="authResetSchema" | ||
@submit="onSubmit" | ||
> | ||
<UFormGroup :label="$t('auth.form.token.label')" name="token" required> | ||
<UInput v-model="state.token" type="text" size="md" :placeholder="$t('auth.form.token.placeholder')" /> | ||
</UFormGroup> | ||
|
||
<UFormGroup :label="$t('auth.form.password.label')" name="password" required> | ||
<UInput v-model="state.password" type="password" size="md" :placeholder="$t('auth.form.password.placeholder')" /> | ||
</UFormGroup> | ||
|
||
<UButton type="submit" size="md" :loading="status === 'pending'" block> | ||
{{ $t('auth.form.action.reset') }} | ||
</UButton> | ||
</UForm> | ||
</div> | ||
</template> | ||
|
||
<script setup lang="ts"> | ||
import type { Form } from '#ui/types' | ||
import { type AuthResetSchema, authResetSchema } from '#schema' | ||
const route = useRoute() | ||
const form = ref<Form<AuthResetSchema>>() | ||
const state = reactive<AuthResetSchema>({ | ||
token: (route.query?.token as string) || '', | ||
password: '', | ||
}) | ||
const { fetch: refreshSession } = useUserSession() | ||
const { status, execute: onSubmit } = useAPI('/api/auth/reset', { | ||
method: 'post', | ||
body: state, | ||
immediate: false, | ||
watch: false, | ||
onResponse: async ({ response }) => { | ||
if (response.ok) { | ||
await refreshSession() | ||
await navigateTo('/') | ||
} | ||
}, | ||
}) | ||
const { onChangeLocale } = useI18nUtils() | ||
onChangeLocale(() => { | ||
form.value?.clear() | ||
}) | ||
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<template> | ||
<AuthResetForm /> | ||
</template> | ||
|
||
<script setup lang="ts"> | ||
definePageMeta({ | ||
layout: 'auth', | ||
middleware: ['guest'], | ||
}) | ||
useHead({ | ||
title: () => $t('auth.reset.title'), | ||
}) | ||
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,31 @@ | ||
import { ERROR_EMAIL_CREDENTIALS, ERROR_NOT_IMPLEMENTED } from '#constants/errors' | ||
import { ERROR_EMAIL_CREDENTIALS, ERROR_USER_INVALID_DATA, ERROR_USER_NOT_FOUND } from '#constants/errors' | ||
import { createPasswordReset, findUserByEmail } from '#core/services/user' | ||
import { useEmail } from '#core/email' | ||
|
||
export default defineEventHandler(() => { | ||
/** | ||
* This route should email the user's email with a link to the password reset page. | ||
* After clicking the link, the user should enter a new password. | ||
*/ | ||
throw errorResolver({}, { | ||
DEFAULT: ERROR_NOT_IMPLEMENTED, | ||
EMAIL_BAD_CREDENTIALS: ERROR_EMAIL_CREDENTIALS, | ||
}) | ||
export default defineEventHandler(async (event) => { | ||
try { | ||
const config = useRuntimeConfig(event) | ||
const { send } = useEmail() | ||
|
||
// const { send } = useEmail() | ||
// send({ | ||
// to: '', | ||
// subject: 'Change password', | ||
// template: 'change-password', | ||
// params: { | ||
// resetUrl: '', | ||
// emailTo: '', | ||
// }, | ||
// }) | ||
const data = await readBody(event) | ||
const user = await findUserByEmail(data.email) | ||
const passwordReset = await createPasswordReset(user.id) | ||
|
||
await send({ | ||
to: user.email, | ||
subject: 'Change password', | ||
template: 'change-password', | ||
params: { | ||
resetUrl: `${config.baseUrl}/auth/reset?token=${passwordReset.token}`, | ||
emailTo: user.email, | ||
token: passwordReset.token, | ||
}, | ||
}) | ||
} catch (e) { | ||
throw errorResolver(e, { | ||
ERROR_USER_NOT_FOUND, | ||
ZOD: ERROR_USER_INVALID_DATA, | ||
EMAIL_BAD_CREDENTIALS: ERROR_EMAIL_CREDENTIALS, | ||
}) | ||
} | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { ERROR_TOKEN_EXPIRED, ERROR_TOKEN_INVALID_DATA } from '#constants/errors' | ||
import { resetPassword, validateUserPasswordResetToken } from '#core/services/user' | ||
import { getProjectsAvailableList } from '#core/services/project' | ||
|
||
export default defineEventHandler(async (event) => { | ||
try { | ||
const data = await readBody(event) | ||
const user = await validateUserPasswordResetToken(data.token) | ||
|
||
await resetPassword(user.id, data.password) | ||
const projects = await getProjectsAvailableList(user.id) | ||
await setUserSession(event, { user, projects }) | ||
} catch (e) { | ||
throw errorResolver(e, { | ||
ERROR_TOKEN_EXPIRED, | ||
ZOD: ERROR_TOKEN_INVALID_DATA, | ||
}) | ||
} | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
apps/web/server/core/database/migrations/0002_sour_silver_sable.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
CREATE TABLE `password_resets` ( | ||
`id` text PRIMARY KEY NOT NULL, | ||
`user_id` text NOT NULL, | ||
`token` text NOT NULL, | ||
`expires_at` integer NOT NULL, | ||
`created_at` integer DEFAULT (unixepoch()), | ||
FOREIGN KEY (`user_id`) REFERENCES `users`(`id`) ON UPDATE cascade ON DELETE cascade | ||
); |
Oops, something went wrong.