OtpLogin.vue 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. <template>
  2. <q-card v-if="!userLogin"
  3. class="col-lg-4 col-md-5 col-sm-6 col-xs-10 login-card my-card shadow-6">
  4. <q-card-section class="row bg-primary text-white justify-between">
  5. <div class="row login-header-right-side justify-center items-center">
  6. <p class="login-entry-title q-ml-md q-mb-none">ورود</p>
  7. </div>
  8. <q-avatar />
  9. </q-card-section>
  10. <q-linear-progress v-if="loadingList"
  11. indeterminate
  12. class="q-mt-sm" />
  13. <q-separator />
  14. <div class="q-pa-lg">
  15. <q-input v-if="otpStep === 'sendOtp'"
  16. ref="userName"
  17. v-model="username"
  18. bottom-slots
  19. color="blue-8"
  20. name="userName"
  21. label="شماره همراه"
  22. @keydown.enter="sendOtp">
  23. <template v-slot:before>
  24. <q-icon name="person" />
  25. </template>
  26. </q-input>
  27. <q-input v-if="otpStep === 'loginByOtp'"
  28. ref="otp"
  29. v-model="otp"
  30. bottom-slots
  31. color="blue-8"
  32. name="otp"
  33. label="کد تایید"
  34. @keydown.enter="loginByOtp">
  35. <template v-slot:before>
  36. <q-icon name="person" />
  37. </template>
  38. </q-input>
  39. <q-card-actions align="left">
  40. <q-btn v-if="otpStep === 'sendOtp'"
  41. color="primary"
  42. label="ارسال کد تایید"
  43. @click="sendOtp" />
  44. <q-btn v-if="otpStep === 'loginByOtp'"
  45. color="primary"
  46. label="ورود"
  47. @click="loginByOtp" />
  48. </q-card-actions>
  49. </div>
  50. </q-card>
  51. </template>
  52. <script>
  53. import { mixinAuth } from 'src/mixin/Mixins'
  54. import API_ADDRESS from 'src/api/Addresses'
  55. export default {
  56. name: 'OTPLogin',
  57. mixins: [mixinAuth],
  58. props: {
  59. redirect: {
  60. type: Boolean,
  61. default: false
  62. }
  63. },
  64. data: () => ({
  65. userLogin: false,
  66. loadingList: false,
  67. otpStep: 'sendOtp',
  68. username: null,
  69. password: null,
  70. otp: null
  71. }),
  72. computed: {
  73. user () {
  74. return this.$store.getters['Auth/user']
  75. }
  76. },
  77. created () {
  78. if (this.getToken()) {
  79. this.getUserData(() => { this.redirectTo() })
  80. }
  81. },
  82. methods: {
  83. getToken () {
  84. return this.$store.getters['Auth/accessToken']
  85. },
  86. getEnter (targetRefKey) {
  87. this.$refs[targetRefKey].focus()
  88. },
  89. redirectTo () {
  90. let redirectTo = window.localStorage.getItem('redirectTo')
  91. if (!redirectTo) {
  92. redirectTo = 'Admin.Content.Index'
  93. }
  94. if (!this.user.isSuperUser()) {
  95. redirectTo = 'Admin.Installment.Index'
  96. }
  97. this.$router.push({ name: redirectTo })
  98. },
  99. handleErr (err) {
  100. this.loadingList = false
  101. const messages = []
  102. for (const key in err.data.errors) {
  103. err.data.errors[key].forEach(message => {
  104. this.$q.notify({
  105. type: 'negative',
  106. message,
  107. position: 'top'
  108. })
  109. })
  110. }
  111. if (!err.data.errors) {
  112. if (err.data.message) messages.push(err.data.message)
  113. else messages.push(err.statusText)
  114. this.$q.notify({
  115. type: 'negative',
  116. message: messages,
  117. position: 'top'
  118. })
  119. }
  120. },
  121. sendOtp () {
  122. this.loadingList = true
  123. this.$axios.post(API_ADDRESS.auth.sendOtp, {
  124. mobile_number: this.username
  125. })
  126. .then(() => {
  127. this.loadingList = false
  128. this.otpStep = 'loginByOtp'
  129. })
  130. .catch(err => {
  131. this.handleErr(err.response)
  132. })
  133. },
  134. loginByOtp () {
  135. this.loadingList = true
  136. this.$store.dispatch('Auth/otpLogin', {
  137. mobile_number: this.username,
  138. otp: this.otp
  139. })
  140. .then(() => {
  141. this.loadingList = false
  142. this.$axios.defaults.headers.common.Authorization = 'Bearer ' + this.$store.getters['Auth/accessToken']
  143. this.$store.commit('AppLayout/showOtpLoginDialog', false)
  144. if (this.redirect) {
  145. this.redirectTo()
  146. }
  147. })
  148. .catch(err => {
  149. this.handleErr(err.response)
  150. })
  151. }
  152. }
  153. }
  154. </script>
  155. <style scoped>
  156. .login-card {
  157. min-width: 300px;
  158. }
  159. </style>