aboutsummaryrefslogtreecommitdiff
path: root/src/components/login_form/login_form.js
blob: 0097e18aa9311dccf3b6afd89936264db6040727 (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
import oauthApi from '../../services/new_api/oauth.js'
const LoginForm = {
  data: () => ({
    user: {},
    authError: false
  }),
  computed: {
    loginMethod () { return this.$store.state.instance.loginMethod },
    loggingIn () { return this.$store.state.users.loggingIn },
    registrationOpen () { return this.$store.state.instance.registrationOpen }
  },
  methods: {
    oAuthLogin () {
      const data = {
        ...this.$store.state.oauth,
        instance: this.$store.state.instance.server,
        commit: this.$store.commit
      }

      oauthApi.getOrCreateApp(data)
        .then((app) => { oauthApi.login({ ...app, ...data }) })
    },
    submit () {
      const data = {
        ...this.$store.state.oauth,
        instance: this.$store.state.instance.server,
        commit: this.$store.commit
      }
      this.clearError()
      oauthApi.getOrCreateApp(data).then((app) => {
        oauthApi.getTokenWithCredentials(
          {
            ...app,
            instance: data.instance,
            username: this.user.username,
            password: this.user.password
          }
        ).then(async (result) => {
          if (result.error) {
            this.authError = result.error
            this.user.password = ''
            return
          }
          this.$store.commit('setToken', result.access_token)
          try {
            await this.$store.dispatch('loginUser', result.access_token)
            this.$router.push({name: 'friends'})
          } catch (e) {
            console.log(e)
          }
        })
      })
    },
    clearError () {
      this.authError = false
    }
  }
}

export default LoginForm