aboutsummaryrefslogtreecommitdiff
path: root/src/components/registration/registration.js
blob: f7f8a720f1b35b05aed31ca5b58f373eb0eed8c9 (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
import oauthApi from '../../services/new_api/oauth.js'

const registration = {
  data: () => ({
    user: {},
    error: false,
    registering: false
  }),
  created () {
    if ((!this.$store.state.instance.registrationOpen && !this.token) || !!this.$store.state.users.currentUser) {
      this.$router.push('/main/all')
    }
    // Seems like this doesn't work at first page open for some reason
    if (this.$store.state.instance.registrationOpen && this.token) {
      this.$router.push('/registration')
    }
  },
  computed: {
    termsofservice () { return this.$store.state.instance.tos },
    token () { return this.$route.params.token }
  },
  methods: {
    submit () {
      this.registering = true
      this.user.nickname = this.user.username
      this.user.token = this.token
      this.$store.state.api.backendInteractor.register(this.user).then(
        (response) => {
          if (response.ok) {
            const data = {
              oauth: this.$store.state.oauth,
              instance: this.$store.state.instance.server
            }
            oauthApi.getOrCreateApp(data).then((app) => {
              oauthApi.getTokenWithCredentials(
                {
                  app,
                  instance: data.instance,
                  username: this.user.username,
                  password: this.user.password})
                .then((result) => {
                  this.$store.commit('setToken', result.access_token)
                  this.$store.dispatch('loginUser', result.access_token)
                  this.$router.push('/main/friends')
                })
            })
          } else {
            this.registering = false
            response.json().then((data) => {
              this.error = data.error
            })
          }
        }
      )
    }
  }
}

export default registration