|
| 1 | +'use server' |
| 2 | + |
| 3 | +import { TextQueryMethod } from '@zitadel/proto/zitadel/object/v2/object_pb' |
| 4 | +import { getBaseUrl } from 'src/app/portal/_common/lib/get-base-url' |
| 5 | +import { getAdminStyroClient } from 'src/app/portal/_common/lib/styro/styro-client' |
| 6 | +import { getUserServiceClient } from 'src/app/portal/_common/lib/zitadel-client' |
| 7 | +import { forgotPasswordFormSchema } from './forgot-password-form-schema' |
| 8 | + |
| 9 | +export type FormState = |
| 10 | + | { |
| 11 | + error: string |
| 12 | + } |
| 13 | + | { |
| 14 | + error: string |
| 15 | + field: keyof (typeof forgotPasswordFormSchema)['_output'] |
| 16 | + } |
| 17 | + | { |
| 18 | + success: true |
| 19 | + } |
| 20 | + |
| 21 | +export async function forgotPasswordAction(data: FormData): Promise<FormState> { |
| 22 | + const formData = Object.fromEntries(data.entries()) |
| 23 | + const result = forgotPasswordFormSchema.safeParse(formData) |
| 24 | + |
| 25 | + if (!result.success) { |
| 26 | + return { error: 'Invalid form data' } |
| 27 | + } |
| 28 | + |
| 29 | + const userServiceClient = getUserServiceClient() |
| 30 | + let userResponse |
| 31 | + try { |
| 32 | + userResponse = await userServiceClient.listUsers({ |
| 33 | + queries: [ |
| 34 | + { |
| 35 | + $typeName: 'zitadel.user.v2.SearchQuery', |
| 36 | + query: { |
| 37 | + case: 'emailQuery', |
| 38 | + value: { |
| 39 | + $typeName: 'zitadel.user.v2.EmailQuery', |
| 40 | + emailAddress: result.data.email, |
| 41 | + method: TextQueryMethod.CONTAINS_IGNORE_CASE, |
| 42 | + }, |
| 43 | + }, |
| 44 | + }, |
| 45 | + ], |
| 46 | + query: { |
| 47 | + limit: 1, |
| 48 | + }, |
| 49 | + }) |
| 50 | + } catch (e) { |
| 51 | + console.error(e) |
| 52 | + return { error: 'Failed to reset password' } |
| 53 | + } |
| 54 | + |
| 55 | + if (userResponse.result.length === 0) { |
| 56 | + return { |
| 57 | + field: 'email', |
| 58 | + error: |
| 59 | + "We couldn't find an account with that email address. Please try again or register an account.", |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + let resetResponse |
| 64 | + try { |
| 65 | + resetResponse = await userServiceClient.passwordReset({ |
| 66 | + $typeName: 'zitadel.user.v2.PasswordResetRequest', |
| 67 | + medium: { |
| 68 | + case: 'returnCode', |
| 69 | + value: { |
| 70 | + $typeName: 'zitadel.user.v2.ReturnPasswordResetCode', |
| 71 | + }, |
| 72 | + }, |
| 73 | + userId: userResponse.result[0].userId, |
| 74 | + }) |
| 75 | + } catch (e) { |
| 76 | + console.error(e) |
| 77 | + return { error: 'Failed to reset password' } |
| 78 | + } |
| 79 | + |
| 80 | + if (!resetResponse.verificationCode) { |
| 81 | + return { error: 'Failed to reset password' } |
| 82 | + } |
| 83 | + |
| 84 | + const client = getAdminStyroClient() |
| 85 | + try { |
| 86 | + await client.postAdminFrontendEmail({ |
| 87 | + postAdminFrontendEmailRequest: { |
| 88 | + email: { |
| 89 | + type: 'reset_password', |
| 90 | + frontend: { |
| 91 | + baseUrl: await getBaseUrl(), |
| 92 | + }, |
| 93 | + reset: { |
| 94 | + email: result.data.email, |
| 95 | + userId: userResponse.result[0].userId, |
| 96 | + code: resetResponse.verificationCode, |
| 97 | + }, |
| 98 | + }, |
| 99 | + }, |
| 100 | + }) |
| 101 | + } catch (e) { |
| 102 | + console.error(e) |
| 103 | + return { error: 'Failed to send email' } |
| 104 | + } |
| 105 | + |
| 106 | + return { success: true } |
| 107 | +} |
0 commit comments