Skip to content
Snippets Groups Projects
RegisterForm.vue 9.06 KiB
Newer Older
    class="w-full max-w-md mx-auto mb-auto md:ring-1 ring-gray-300 overflow-hidden rounded-xl p-4"
Titus Netland's avatar
Titus Netland committed
    <div
Titus Netland's avatar
Titus Netland committed
      class="text-xl md:text-2xl font-medium text-center text-primary-light mt-4 mb-8"
henrikburmann's avatar
henrikburmann committed
      id="registerLabel"
Gilgard's avatar
Gilgard committed
    >
Titus Netland's avatar
Titus Netland committed
      Opprett ny konto
    </div>

    <form @submit.prevent>
Gilgard's avatar
Gilgard committed
        <div>
          <input
            class="block w-full px-4 py-2 mt-2 text-gray-700 placeholder-gray-500 bg-white border rounded-md dark:bg-gray-800 dark:border-gray-600 dark:placeholder-gray-400 focus:border-primary-light dark:focus:border-primary-light focus:ring-opacity-40 focus:outline-none focus:ring focus:ring-primary-light"
            v-model="email"
            id="email"
            type="email"
            placeholder="E-post adresse"
Gilgard's avatar
Gilgard committed
          />
          <!-- error message -->
          <div
            class="text-error-medium text-sm"
Gilgard's avatar
Gilgard committed
            v-for="(error, index) of v$.email.$errors"
            :key="index"
            <div class="text-error-medium text-sm" v-show="showError">
              {{ error.$message }}
            </div>
          </div>
          <div class="text-error-medium text-sm" v-show="errorMessage">
            {{ errorMessage }}
          </div>
Gilgard's avatar
Gilgard committed
          <input
            class="block w-full px-4 py-2 mt-2 text-gray-700 placeholder-gray-500 bg-white border rounded-md dark:bg-gray-800 dark:border-gray-600 dark:placeholder-gray-400 focus:border-primary-light dark:focus:border-primary-light focus:ring-opacity-40 focus:outline-none focus:ring focus:ring-primary-light"
            v-model="password"
            id="password"
            type="password"
            placeholder="Passord"
Gilgard's avatar
Gilgard committed
          />
          <!-- error message -->
          <div
            class="text-error-medium text-sm"
Gilgard's avatar
Gilgard committed
            v-for="(error, index) of v$.password.$errors"
            :key="index"
              class="text-error-medium text-sm"
Gilgard's avatar
Gilgard committed
              v-show="showError"
              id="passwordErrorId"
            >
              {{ error.$message }}
            </div>
          </div>
Gilgard's avatar
Gilgard committed
          <input
            class="block w-full px-4 py-2 mt-2 text-gray-700 placeholder-gray-500 bg-white border rounded-md dark:bg-gray-800 dark:border-gray-600 dark:placeholder-gray-400 focus:border-primary-light dark:focus:border-primary-light focus:ring-opacity-40 focus:outline-none focus:ring focus:ring-primary-light"
            v-model="confirmPassword"
            id="confirmPassword"
            type="password"
            placeholder="Bekreft passord"
Gilgard's avatar
Gilgard committed
          />
          <!-- error message -->
          <div
            class="text-error-medium text-sm"
Gilgard's avatar
Gilgard committed
            v-for="(error, index) of v$.confirmPassword.$errors"
            :key="index"
              class="text-error-medium text-sm"
Gilgard's avatar
Gilgard committed
              v-show="showError"
              id="confirmPasswordErrorId"
            >
              {{ error.$message }}
            </div>
          </div>
Gilgard's avatar
Gilgard committed
          <input
            class="block w-full px-4 py-2 mt-2 text-gray-700 placeholder-gray-500 bg-white border rounded-md dark:bg-gray-800 dark:border-gray-600 dark:placeholder-gray-400 focus:border-primary-light dark:focus:border-primary-light focus:ring-opacity-40 focus:outline-none focus:ring focus:ring-primary-light"
            data-test="firstNameTest"
            v-model="firstName"
            id="firstName"
            type="text"
            placeholder="Fornavn"
Gilgard's avatar
Gilgard committed
          />
          <!-- error message -->
          <div
            class="text-error-medium text-sm"
Gilgard's avatar
Gilgard committed
            v-for="(error, index) of v$.firstName.$errors"
            :key="index"
              class="text-error-medium text-sm"
Gilgard's avatar
Gilgard committed
              v-show="showError"
              id="firstNameErrorId"
            >
              {{ error.$message }}
            </div>
          </div>
Gilgard's avatar
Gilgard committed
          <input
            class="block w-full px-4 py-2 mt-2 text-gray-700 placeholder-gray-500 bg-white border rounded-md dark:bg-gray-800 dark:border-gray-600 dark:placeholder-gray-400 focus:border-primary-light dark:focus:border-primary-light focus:ring-opacity-40 focus:outline-none focus:ring focus:ring-primary-light"
            v-model="lastName"
            id="lastName"
            type="text"
            placeholder="Etternavn"
Gilgard's avatar
Gilgard committed
          />
          <!-- error message -->
          <div
            class="text-error-medium text-sm"
Gilgard's avatar
Gilgard committed
            v-for="(error, index) of v$.lastName.$errors"
            :key="index"
              class="text-error-medium text-sm"
Gilgard's avatar
Gilgard committed
              v-show="showError"
              id="lastNameErrorId"
            >
              {{ error.$message }}
            </div>
          </div>
Gilgard's avatar
Gilgard committed
          <input
            class="block w-full px-4 py-2 mt-2 text-gray-700 placeholder-gray-500 bg-white border rounded-md dark:bg-gray-800 dark:border-gray-600 dark:placeholder-gray-400 focus:border-primary-light dark:focus:border-primary-light focus:ring-opacity-40 focus:outline-none focus:ring focus:ring-primary-light"
            v-model="address"
            id="address"
            type="text"
            placeholder="Adresse"
Gilgard's avatar
Gilgard committed
          />
          <!-- error message -->
          <div
            class="text-error-medium text-sm"
Gilgard's avatar
Gilgard committed
            v-for="(error, index) of v$.address.$errors"
            :key="index"
              class="text-error-medium text-sm"
Gilgard's avatar
Gilgard committed
              v-show="showError"
              id="addressErrorId"
            >
              {{ error.$message }}
            </div>
          </div>
        </div>
      </div>

      <div class="flex justify-end mt-6">
Titus Netland's avatar
Titus Netland committed
        <Button @click="submit" :text="'Opprett'"></Button>
      </div>
    </form>
import useVuelidate from "@vuelidate/core";
import { doLogin } from "@/utils/apiutil";
Gilgard's avatar
Gilgard committed
import {
  required,
  email,
  minLength,
  sameAs,
  helpers,
} from "@vuelidate/validators";
import Button from "@/components/BaseComponents/ColoredButton";
import UserService from "@/services/user.service";
// const isEmailTaken = (value) =>
// fetch(`/api/unique/${value}`).then((r) => r.json()); // check the email in the server
export default {
  components: {
    Button,
  },
  setup: () => ({ v$: useVuelidate() }),
  data() {
    return {
      showError: false,
      errorMessage: "",
      email: "",
      password: "",
      confirmPassword: "",
      firstName: "",
      lastName: "",
      address: "",
    };
  },
  validations() {
    return {
      email: {
        required: helpers.withMessage(`Feltet må være utfylt`, required),
Gilgard's avatar
Gilgard committed
        email: helpers.withMessage("E-posten er ugyldig", email),
        // isUnique: helpers.withAsync(isEmailTaken),
      },
        required: helpers.withMessage(`Feltet må være utfylt`, required),
Gilgard's avatar
Gilgard committed
        minLength: helpers.withMessage(
          "Passordet må være minst 8 karakterer lang",
          minLength(8)
        ),
      confirmPassword: {
Gilgard's avatar
Gilgard committed
        sameAs: helpers.withMessage(
          "Passordene må være like",
          sameAs(this.password)
        ),
        required: helpers.withMessage(`Feltet må være utfylt`, required),
      },
      firstName: {
        required: helpers.withMessage(`Feltet må være utfylt`, required),
      },
      lastName: {
        required: helpers.withMessage(`Feltet må være utfylt`, required),
      },
      address: {
        required: helpers.withMessage(`Feltet må være utfylt`, required),
    async submit() {
      //Display loading symbol
      this.showError = true;
      //Validate form
      const result = await this.v$.$validate();
      if (!result) {
        this.loading = false;
        return;
      }

      //Send a request to create a user and save success as a bool
      const userCreated = await this.sendRegisterRequest();

      //If a user is created succsessfully, try to login
      //If we get this far, we will be pushed anyway so there is no point updating "loading"
      if (!userCreated) return;

      const loginRequest = {
        email: this.email,
        password: this.password,
      };

      const loginResponse = await doLogin(loginRequest);

Titus Netland's avatar
Titus Netland committed
      if (loginResponse.isLoggedIn === false) {
        this.errorMessage = "Failed to log in with new user";
        this.$store.commit("logout");
Titus Netland's avatar
Titus Netland committed
        await this.$router.push("/login");
Titus Netland's avatar
Titus Netland committed
      this.$store.commit("saveToken", loginResponse.token);
      const adminList = await UserService.getAdminList();
      this.$store.commit("addAdminList", adminList);
Titus Netland's avatar
Titus Netland committed
      await this.$router.push("/");
    },
    async sendRegisterRequest() {
      const userInfo = {
        email: this.email,
        firstName: this.firstName,
        lastName: this.lastName,
        password: this.password,
        address: this.address,
      };
      const res = await UserService.registerUser(userInfo);
      this.errorMessage = res;
      return res.status === 200;