summaryrefslogtreecommitdiff
path: root/app/src/views/Sign.vue
blob: dc6fa3fcbdad4e0d6b78dcf75dd538c4c2b89e63 (plain)
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
<template>
  <ion-page>
    <m6-header />
    <ion-content :fullscreen="true">
      <form id="login">
        <h2>Sign in</h2>
        <div class="item">
          <input placeholder="username" type="text" v-model="form.username" />
        </div>
        <div class="item">
          <input
            placeholder="password"
            type="password"
            v-model="form.password"
          />
        </div>
        <ion-button color="primary" id="log" @click="handleLogin()">
          Submit
        </ion-button>
      </form>
    </ion-content>
    <m6-footer />
  </ion-page>
</template>

<script lang="ts">
import { defineComponent } from "vue";
import { IonPage, IonContent } from "@ionic/vue";
import Header from "@/components/Header.vue";
import Footer from "@/components/Footer.vue";

import { mapGetters, mapActions } from "vuex";

export default defineComponent({
  name: "Sign",
  components: {
    IonContent,
    IonPage,
    "m6-header": Header,
    "m6-footer": Footer,
  },
  data() {
    return {
      form: { username: "", password: "" },
    };
  },
  computed: {
    ...mapGetters("auth", ["isLogged"]),
  },
  created() {
    if (this.isLogged) window.location.href = "/me";
  },
  methods: {
    handleLogin() {
      if (this.form.username == "" || this.form.password == "") {
        this.toast({
          header: "All fields are required",
          text: "",
          color: "danger",
        });
        return;
      }
      this.login(this.form).then((response) => {
        if (response.status != 200) {
          this.toast({
            header: response.data.error,
            text: "",
            color: "danger",
          });
        } else {
          this.toast({
            header: "Logged successfully",
            text: "",
            color: "success",
          });
          setTimeout(() => {
            window.location.href = "/me";
          }, 2000);
        }
      });
    },
    ...mapActions(["toast"]),
    ...mapActions("auth", ["login"]),
  },
});
</script>