All files / src/views SignupPage.vue

96.49% Statements 55/57
83.33% Branches 35/42
91.66% Functions 11/12
96.49% Lines 55/57

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  16x       61x   17x         9x     1x           6x                   6x               1x     6x                                                         3x                                                                         16x 16x 16x 16x   16x 16x 16x 16x 16x 16x 16x 16x 16x   16x   16x 4x 4x 4x         6x 6x   6x 6x     5x   1x 1x     4x     1x   6x         3x 3x   3x 3x   2x 1x 1x   1x     1x   3x         1x 1x 1x   1x 1x 1x       1x            
<template>
  <AuthLayout
    :presentation-title="t('auth.presentationTitleSignup')"
    :presentation-subtitle="t('auth.presentationSubtitleSignup')"
  >
    <span>{{ t('auth.alreadyHaveAccount') }}</span>
    <router-link to="/login" 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="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>
        <button class="btn btn-success shadow p-2 my-3" :disabled="loading">
          {{ 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">
          {{ 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
import { ref, onMounted } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { useAuthStore } from '@/stores/auth'
import { useUILanguage } from '@/composables/useUILanguage'
import AuthLayout from '@/components/AuthLayout.vue'
 
const router = useRouter()
const route = useRoute()
const authStore = useAuthStore()
const { t } = useUILanguage()
 
const name = ref('')
const email = ref('')
const password = ref('')
const showPassword = ref(false)
const confirmationCode = ref('')
const needsConfirmation = ref(false)
const loading = ref(false)
const error = ref('')
const resendSuccess = ref(false)
 
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: {} })
  }
})
 
async function handleSignup() {
  loading.value = true
  error.value = ''
 
  try {
    const result = await authStore.register(email.value, password.value, name.value)
 
    // Check if user was auto-confirmed (e.g., @browsway.com emails)
    if (result.isSignUpComplete) {
      // Auto-confirmed: login directly
      await authStore.login(email.value, password.value)
      router.push('/')
    } else {
      // Needs email confirmation
      needsConfirmation.value = true
    }
  } catch (err: any) {
    error.value = err.message || 'Signup failed'
  } 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
    if (password.value) {
      await authStore.login(email.value, password.value)
      router.push('/')
    } else {
      router.push('/login')
    }
  } 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)
    resendSuccess.value = true
  } catch (err: any) {
    error.value = err.message || 'Failed to resend confirmation code'
  } finally {
    loading.value = false
  }
}
</script>
 
<style scoped></style>