aboutsummaryrefslogtreecommitdiff
path: root/src/components/password_reset/password_reset.js
blob: 3d94f5e7253d0a28970348a19bc63323adec9f38 (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
import { mapState } from 'vuex'
import passwordResetApi from '../../services/new_api/password_reset.js'
import { library } from '@fortawesome/fontawesome-svg-core'
import {
  faTimes
} from '@fortawesome/free-solid-svg-icons'

library.add(
  faTimes
)

const passwordReset = {
  data: () => ({
    user: {
      email: ''
    },
    isPending: false,
    success: false,
    throttled: false,
    error: null
  }),
  computed: {
    ...mapState({
      signedIn: (state) => !!state.users.currentUser,
      instance: state => state.instance
    }),
    mailerEnabled () {
      return this.instance.mailerEnabled
    }
  },
  created () {
    if (this.signedIn) {
      this.$router.push({ name: 'root' })
    }
  },
  props: {
    passwordResetRequested: {
      default: false,
      type: Boolean
    }
  },
  methods: {
    dismissError () {
      this.error = null
    },
    submit () {
      this.isPending = true
      const email = this.user.email
      const instance = this.instance.server

      passwordResetApi({ instance, email }).then(({ status }) => {
        this.isPending = false
        this.user.email = ''

        if (status === 204) {
          this.success = true
          this.error = null
        } else if (status === 429) {
          this.throttled = true
          this.error = this.$t('password_reset.too_many_requests')
        }
      }).catch(() => {
        this.isPending = false
        this.user.email = ''
        this.error = this.$t('general.generic_error')
      })
    }
  }
}

export default passwordReset