Skip to content
Snippets Groups Projects
Login.vue 8.16 KiB
Newer Older
moon's avatar
moon committed
<template>
  <div>
moon's avatar
moon committed
      <base-spinner />
    </div>
moon's avatar
moon committed
    <div class="flex items-center justify-center">
      <div
        class="
          sm:block
          border border-gray-300
          bg-white bg-opacity-60
          rounded-lg
          mt-6
          w-1/3
        "
      >
moon's avatar
moon committed
        <div class="p-5 mx-auto text-left font-raleway">
          <h1
            class="
              font-bold
              text-left
              font-montserrat
              text-3xl
              sm:text-5xl
              mb-7
            "
          >
moon's avatar
moon committed
            Login to CD-Code
moon's avatar
moon committed
          </h1>
raghosh's avatar
raghosh committed

moon's avatar
moon committed
          <form @submit="login">
            <div class="my-4" :class="{ invalid: !email.isValid }">
              <h1
                class="
                  text-left
                  font-bold
                  mb-2
                  text-xl
                  sm:text-2xl
                  font-montserrat
                "
              >
moon's avatar
moon committed
                Email*
              </h1>
              <input
moon's avatar
moon committed
                type="email"
                class="
                  outline-none
                  bg-transparent
                  border border-gray-400
                  px-4
                  py-5
                  rounded-lg
                  w-full
                  hover:border-blue-700
                  focus:border-blue-700
                "
                placeholder="Enter email."
                @blur="clearValidity('email')"
              />
              <p v-if="!email.isValid" class="text-red-500">
moon's avatar
moon committed
            </div>
            <div class="my-4" :class="{ invalid: !password.isValid }">
              <h1
                class="
                  text-left
                  font-bold
                  mb-2
                  text-xl
                  sm:text-2xl
                  font-montserrat
                "
              >
moon's avatar
moon committed
                Password*
              </h1>
              <input
moon's avatar
moon committed
                type="password"
                class="
                  text-xl
                  outline-none
                  bg-transparent
                  border border-gray-400
                  px-4
                  py-5
                  rounded-lg
                  w-full
                  hover:border-blue-700
                  focus:border-blue-700
                "
                placeholder="Enter password."
                @blur="clearValidity('password')"
              />
              <p v-if="!password.isValid" class="text-red-500">
                Password must not be empty.
              </p>
moon's avatar
moon committed
            </div>
            <p v-show="error" class="text-red-500">
moon's avatar
moon committed
              {{ errorMsg }}
            </p>
moon's avatar
moon committed
            <button
              type="submit"
              class="
                bg-blue-500
                my-4
                text-2xl
                w-full
                font-bold
                rounded-lg
                p-5
                text-white
              "
moon's avatar
moon committed
            >
moon's avatar
moon committed
            </button>
                class="text-pink-base hover:text-pink-base text-2xl"
moon's avatar
moon committed
                Forgot Password?
              </router-link>
moon's avatar
moon committed
            </p>
            <h4>
              Don't have an account yet?
              <router-link
                class="text-pink-base hover:text-pink-base text-2xl font-bold"
moon's avatar
moon committed
                Join CD-CODE as a contributor.
moon's avatar
moon committed
              </router-link>
moon's avatar
moon committed
          </form>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
let host = require("@/components/js/const").apiHost;
moon's avatar
moon committed

export default {
  name: "Login",
moon's avatar
moon committed

  data() {
    return {
        val: "",
        val: "",
moon's avatar
moon committed
      error: false,
      isDev: process.env.NODE_ENV === "development",
  computed: {
    userData: function () {
      return this.$store.getters["User/userData"];
moon's avatar
moon committed
  mounted: function () {
    const vm = this;
    if (vm.userData !== null) {
      this.$router.push("/profile");
moon's avatar
moon committed
    }
  },
moon's avatar
moon committed
  methods: {
    clearValidity(input) {
      this[input].isValid = true;
    },
    validateForm() {
      this.formIsValid = true;
      if (this.email.val === "") {
        this.email.isValid = false;
        this.formIsValid = false;
      }
      if (this.password.val === "") {
        this.password.isValid = false;
        this.formIsValid = false;
      }
    },
moon's avatar
moon committed
    async login(e) {

      const vm = this;
      vm.validateForm();
      if (!vm.formIsValid) {
        return;
      }
      if (vm.isDev) {
        host = require("@/components/js/const").devApiHost;
      console.log("url is", host);
raghosh's avatar
raghosh committed
      try {
        const res = await fetch(`${host}/api/auth/local`, {
          method: "POST",
          mode: "cors", // no-cors, *cors, same-origin
          cache: "no-cache", // *default, no-cache, reload, force-cache,
raghosh's avatar
raghosh committed
          headers: {
            "Content-Type": "application/json",
raghosh's avatar
raghosh committed
            // 'Content-Type': 'application/x-www-form-urlencoded',
          },
          body: JSON.stringify({
            identifier: this.email.val,
            password: this.password.val,
          }),
        });
raghosh's avatar
raghosh committed
        if (!res.ok) {
          this.isLoading = false;
          this.error = true;
          this.errorMsg = `${res.status} Internal Error, please write a mail to CDCode Admin`;
raghosh's avatar
raghosh committed
          return;
        }

        if (!res.ok && res.status === 500) {
raghosh's avatar
raghosh committed
          this.isLoading = false;
          this.error = true;
          this.errorMsg = `${res.status} Internal Error, please write a mail to CDCode Admin`;
raghosh's avatar
raghosh committed
          return;
        }
        if (!res.ok && res.status === 405) {
          this.isLoading = false;
          this.error = true;
          this.errorMsg = `${res.status} Internal Error, please write a mail to CDCode Admin`;
raghosh's avatar
raghosh committed
          return;
        }
raghosh's avatar
raghosh committed
        const response = await res.json();
        if (
          !res.ok &&
          response.error.status === 400 &&
          response.error.message === "Invalid identifier or password"
raghosh's avatar
raghosh committed
        ) {
          this.isLoading = false;
          this.error = true;
          this.errorMsg = "Email Address & Password mismatch.";
          this.password.val = "";
raghosh's avatar
raghosh committed
          return;
        }

        const { jwt, user } = response;
        window.localStorage.setItem("jwt", jwt);
        window.localStorage.setItem("userData", JSON.stringify(user));
        this.$store.dispatch("User/setJwt", jwt);
        this.$store.dispatch("User/setUserData", JSON.stringify(user));
raghosh's avatar
raghosh committed
        this.isLoading = false;
        this.error = false;
        this.$router.push("/profile");
raghosh's avatar
raghosh committed
      } catch (err) {
        console.log("catch error", err);
        this.isLoading = false;
        this.error = true;
moon's avatar
moon committed
        this.errorMsg = `${err.message} Internal Error, please write a mail to CDCode Admin`;

      // const res = await this.axios.post(`${host}/api/auth/local`, {
      //   identifier: this.email.val,
      //   password: this.password.val
      // });
      // console.log("error is ", res);
      // const { jwt, user } = res.data
      // console.log(user)
      // window.localStorage.setItem('jwt', jwt)
      // window.localStorage.setItem('userData', JSON.stringify(user))

      // this.$store.dispatch('User/setJwt', jwt)
      // this.$store.dispatch('User/setUserData', user)
      // this.isLoading= false;
      // this.error= false;
      // this.$router.push('/profile')

      // console.log(error.response.data.error.message)
      // console.log("error",e.data);
      // this.isLoading= false;
      // this.error = true
      // this.password.val = ''
      // this.isLoading= false
moon's avatar
moon committed
</script>
@import url("~@/assets/bootstrap.css");
  color: #ef0087 !important;
.invalid h1 {
  color: red;
}
.invalid input {
  border: 1px solid red;
}
moon's avatar
moon committed
</style>