aboutsummaryrefslogtreecommitdiff
path: root/src/views/LogIn.vue
blob: 77999f20bbd14fdd8caf8f41f90cacd278e81441 (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
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
<script setup lang="ts">
import { ref, toRaw } from 'vue'
import { RouterLink, useRouter } from 'vue-router'
import axios from 'axios'
import { useToast } from 'vue-toast-notification'

const toast = useToast({
  position: 'top-right'
})

const router = useRouter()

const isLoading = ref(false)
const email = ref('')
const password = ref('')

const login = async (e) => {
  e.preventDefault()
  isLoading.value = true

  try {
    const res = await axios.post('/auth/signin', {
      accountname: toRaw(email.value),
      password: toRaw(password.value),
      method: 'email'
    })

    localStorage.setItem('authToken', res.data.auth_token)
    localStorage.setItem('refToken', res.data.refresh_token)
    toast.default(`Welcome, ${res.data.data.Username}`)
    router.push({ path: '/' })
  } catch (err) {
    const statusCode = err.request.status

    switch (statusCode) {
      case 401:
        toast.error('Password is incorrect')
        break
      case 404:
        toast.error('User does not exist')
        break
      default:
        toast.error('An unhandled exception occoured. Please check logs')
    }
  }

  isLoading.value = false
}
</script>

<template>
  <div id="login-form" class="mx-auto mt-5">
    <ul class="nav nav-pills nav-justified mb-3" id="ex1" role="tablist">
      <li class="nav-item" role="presentation">
        <RouterLink
          class="nav-link active"
          id="tab-login"
          data-mdb-toggle="pill"
          to="/login"
          role="tab"
          aria-controls="pills-login"
          aria-selected="true"
          >Login</RouterLink
        >
      </li>
      <li class="nav-item" role="presentation">
        <RouterLink
          class="nav-link"
          id="tab-register"
          data-mdb-toggle="pill"
          to="/register"
          role="tab"
          aria-controls="pills-register"
          aria-selected="false"
          >Register</RouterLink
        >
      </li>
    </ul>

    <div>
      <form v-on:submit="login">
        <div class="form-floating mb-4">
          <input
            id="login-email-input"
            type="text"
            class="form-control"
            placeholder=""
            v-model="email"
          />
          <label for="login-email-input">E-Mail</label>
        </div>

        <div class="form-floating mb-4">
          <input
            id="login-password-input"
            type="password"
            class="form-control"
            placeholder=""
            v-model="password"
          />
          <label for="login-password-input">Password</label>
        </div>

        <!--
        <div class="row mb-4">
          <div class="col-md-6 d-flex justify-content-center">
            <div class="form-check mb-3 mb-md-0">
              <input class="form-check-input" type="checkbox" value="" id="loginCheck" checked />
              <label class="form-check-label" for="loginCheck"> Remember me </label>
            </div>
          </div>

          <div class="col-md-6 d-flex justify-content-center">
            <a href="#!">Forgot password?</a>
          </div>
        </div>
        -->

        <button
          type="submit"
          class="btn btn-primary btn-block mb-4"
          :class="{ disabled: isLoading }"
        >
          Sign In
          <div v-if="isLoading" class="spinner-border spinner-border-sm ms-1" role="status">
            <span class="sr-only"></span>
          </div>
        </button>

        <div class="text-center">
          <p>Not a member? <RouterLink to="/register">Register</RouterLink></p>
        </div>
      </form>
    </div>
  </div>
</template>

<style>
#login-form {
  max-width: 30rem;
  width: 100%;
}
</style>