All files / src/views SignupPage.vue

97.33% Statements 73/75
91.46% Branches 75/82
94.11% Functions 16/17
97.29% Lines 72/74

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286  33x       168x         168x         17x     1x           12x                   12x                                 10x                               1x     12x                                             4x 1x                                                       6x                                                                                           33x 33x 33x 33x       13x 13x     33x 33x 33x 33x 33x 33x 33x 33x 33x 33x     33x 33x 33x       2x 1x   1x     33x   33x 6x 6x 6x         11x 11x   11x 11x           11x 11x     9x   1x 1x 1x     8x     2x   2x   11x         6x 6x   6x 6x       5x 3x 3x 3x   2x     1x   6x         2x 2x 2x   2x 2x 2x       2x            
<template>
  <AuthLayout
    :presentation-title="t('auth.presentationTitleSignup')"
    :presentation-subtitle="t('auth.presentationSubtitleSignup')"
  >
    <span>{{ t('auth.alreadyHaveAccount') }}</span>
    <router-link
      :to="{ path: '/login', query: redirectQuery(route.query) }"
      class="btn btn-secondary shadow p-2 my-3 w-100"
    >
      {{ t('auth.signIn') }}
    </router-link>
 
    <form
      class="signin-form"
      @submit.prevent="needsConfirmation ? handleConfirm() : handleSignup()"
    >
      <hr class="w-100" />
      <span class="mb-2">{{ t('auth.createFreeAccount') }}</span>
 
      <template v-if="!needsConfirmation">
        <label for="name" class="form-label">{{ t('auth.fullName') }}</label>
        <input
          id="name"
          v-model="name"
          class="form-control"
          type="text"
          name="name"
          autocomplete="name"
          required
        />
        <label for="email" class="form-label">{{ t('auth.email') }}</label>
        <input
          id="email"
          v-model="email"
          class="form-control"
          type="email"
          name="email"
          autocomplete="email"
          required
        />
        <label for="nickname" class="form-label">{{ t('auth.nickname') }}</label>
        <input
          id="nickname"
          class="form-control"
          type="text"
          name="nickname"
          autocomplete="off"
          maxlength="20"
          required
          :value="nickname"
          @input="onNicknameInput"
        />
        <div v-if="nicknameStatus === 'checking'" class="form-text">
          {{ t('auth.nicknameChecking') }}
        </div>
        <div v-else-if="nicknameStatus === 'available'" class="form-text text-success">
          {{ t('auth.nicknameAvailable') }}
        </div>
        <div v-else-if="nicknameStatus === 'taken'" class="form-text text-danger">
          {{ t('auth.nicknameTaken') }}
        </div>
        <div v-else-if="nicknameStatus === 'invalid'" class="form-text text-danger">
          {{ t('auth.nicknameInvalid') }}
        </div>
        <div v-else class="form-text">{{ t('auth.nicknameHint') }}</div>
        <label for="password" class="form-label">{{ t('auth.password') }}</label>
        <div class="password-wrapper">
          <input
            id="password"
            v-model="password"
            class="form-control pe-5"
            :type="showPassword ? 'text' : 'password'"
            name="password"
            autocomplete="new-password"
            required
            minlength="8"
          />
          <button
            type="button"
            class="password-toggle"
            :aria-label="showPassword ? t('auth.hidePassword') : t('auth.showPassword')"
            @click="showPassword = !showPassword"
          >
            <FontAwesomeIcon :icon="showPassword ? 'eye-slash' : 'eye'" />
          </button>
        </div>
        <TurnstileWidget
          v-if="turnstileSiteKey"
          ref="captcha"
          :site-key="turnstileSiteKey"
          action="signup"
          :language="uiLanguage"
          @token="captchaToken = $event"
          @error="error = t('auth.captchaUnavailable')"
        />
        <button
          class="btn btn-success shadow p-2 my-3"
          :disabled="
            loading ||
            nicknameStatus === 'invalid' ||
            nicknameStatus === 'taken' ||
            (!!turnstileSiteKey && !captchaToken)
          "
        >
          <span
            v-if="loading"
            class="spinner-border spinner-border-sm me-2"
            role="status"
            aria-hidden="true"
          />
          {{ t('auth.signUp') }}
        </button>
      </template>
 
      <template v-else>
        <div class="alert alert-info" role="alert">
          {{ t('auth.pleaseCheckEmail') }}
        </div>
        <label for="code" class="form-label">{{ t('auth.confirmationCode') }}</label>
        <input
          id="code"
          v-model="confirmationCode"
          class="form-control"
          type="text"
          name="code"
          required
        />
        <button class="btn btn-success shadow p-2 my-3" :disabled="loading">
          <span
            v-if="loading"
            class="spinner-border spinner-border-sm me-2"
            role="status"
            aria-hidden="true"
          />
          {{ t('auth.confirm') }}
        </button>
        <div v-if="resendSuccess" class="alert alert-success" role="alert">
          {{ t('auth.confirmationCodeResent') }}
        </div>
        <button
          type="button"
          class="btn btn-link p-0 mt-1"
          :disabled="loading"
          @click.prevent="handleResend"
        >
          {{ t('auth.resendConfirmationCode') }}
        </button>
      </template>
 
      <div v-if="error" class="alert alert-danger" role="alert">
        {{ error }}
      </div>
    </form>
  </AuthLayout>
</template>
 
<script setup lang="ts">
// @implements UC-AUTH-001.1, UC-AUTH-009.1, UC-SPACE-004.1, UC-SPACE-008.1
import { ref, onMounted } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { useAuthStore } from '@/stores/auth'
import { useUILanguage } from '@/composables/useUILanguage'
import { useNicknameAvailability } from '@/composables/useNicknameAvailability'
import AuthLayout from '@/components/AuthLayout.vue'
import TurnstileWidget from '@/components/TurnstileWidget.vue'
import { authRedirect, joinSlug, redirectQuery } from '@/utils/authRedirect'
 
const router = useRouter()
const route = useRoute()
const authStore = useAuthStore()
const { t, uiLanguage } = useUILanguage()
 
/** For the verification email: its language, and the flow joined if any */
function signUpMetadata(): Record<string, string> {
  const slug = joinSlug(route.query)
  return { lang: uiLanguage.value, ...(slug && { joinSlug: slug }) }
}
 
const name = ref('')
const email = ref('')
const password = ref('')
const { nickname, status: nicknameStatus, onNicknameInput } = useNicknameAvailability()
const showPassword = ref(false)
const confirmationCode = ref('')
const needsConfirmation = ref(false)
const loading = ref(false)
const error = ref('')
const resendSuccess = ref(false)
 
/** The sign-up CAPTCHA, where the API requires it (flows-api captchaEnabled) */
const turnstileSiteKey = import.meta.env.VITE_TURNSTILE_SITE_KEY || ''
const captcha = ref<InstanceType<typeof TurnstileWidget> | null>(null)
const captchaToken = ref<string | null>(null)
 
/** Cognito's message for a refused Pre Sign-up: "PreSignUp failed with error CAPTCHA_FAILED." */
function signupErrorMessage(err: any): string {
  if (typeof err?.message === 'string' && err.message.includes('CAPTCHA_FAILED')) {
    return t('auth.captchaFailed')
  }
  return err?.message || 'Signup failed'
}
 
onMounted(() => {
  // Redirected from login page when account is not confirmed
  if (route.query.email && route.query.confirm === 'true') {
    email.value = route.query.email as string
    needsConfirmation.value = true
    router.replace({ query: redirectQuery(route.query) })
  }
})
 
async function handleSignup() {
  loading.value = true
  error.value = ''
 
  try {
    const signUp: Parameters<typeof authStore.register> = [
      email.value,
      password.value,
      name.value,
      signUpMetadata()
    ]
    if (captchaToken.value) signUp.push({ turnstileToken: captchaToken.value })
    const result = await authStore.register(...signUp)
 
    // Check if user was auto-confirmed (e.g., e2e test accounts)
    if (result.isSignUpComplete) {
      // Auto-confirmed: login directly
      await authStore.login(email.value, password.value)
      await authStore.claimNickname(nickname.value)
      await router.push(authRedirect(route.query) ?? '/')
    } else {
      // Needs email confirmation
      needsConfirmation.value = true
    }
  } catch (err: any) {
    error.value = signupErrorMessage(err)
    // The token was spent: a retry needs a new one
    captcha.value?.reset()
  } finally {
    loading.value = false
  }
}
 
async function handleConfirm() {
  loading.value = true
  error.value = ''
 
  try {
    await authStore.confirm(email.value, confirmationCode.value)
    // If redirected from login page, password may be empty — go to login page.
    // The nickname can only be claimed once authenticated, so it's skipped here;
    // the user picks one later from the Profile page.
    if (password.value) {
      await authStore.login(email.value, password.value)
      await authStore.claimNickname(nickname.value)
      await router.push(authRedirect(route.query) ?? '/')
    } else {
      await router.push({ path: '/login', query: redirectQuery(route.query) })
    }
  } catch (err: any) {
    error.value = err.message || 'Confirmation failed'
  } finally {
    loading.value = false
  }
}
 
async function handleResend() {
  loading.value = true
  error.value = ''
  resendSuccess.value = false
 
  try {
    await authStore.resendConfirmation(email.value, signUpMetadata())
    resendSuccess.value = true
  } catch (err: any) {
    error.value = err.message || 'Failed to resend confirmation code'
  } finally {
    loading.value = false
  }
}
</script>
 
<style scoped></style>