aboutsummaryrefslogtreecommitdiffstats
path: root/src/pages/FetchToken.vue
blob: d6f0064ae805e2d51ec3c47a006c69fd580d0b1b (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
<template>
  <div>
    <div v-if="!tokenValid">
      <h6 class="text-negative">Received an invalid token!</h6>
      <p>{{ tokenData.message }}</p>
    </div>
    <div v-else>
      <h6 class="text-success">you have successfully logged in. You can close this window now!</h6>
    </div>
  </div>
</template>

<script>
  import jwtDecode from 'jwt-decode'

  export default {
    components: {
    },
    data () {
      return {
        tokenValid: false,
      }
    },
    computed: {
      routeToken () {
        return this.$route.params.token
      },
      tokenData () {
        try {
          return jwtDecode(this.routeToken)
        }
        catch (e) {
          return {
            invalid: true,
            message: e.message
          }
        }
      },
    },
    watch: {
      tokenData: {
        handler: function (val, oldVal) {
          if (val !== oldVal) {
            if (val.invalid || !val.username) {
              this.tokenValid = false
            }
            else {
              let that = this
              this.$http.get('/api/auth', {headers: {Authorization: `JWT ${this.routeToken}`}}).then(response => {
                that.tokenValid = true
                that.$store.commit('user/setAuthToken', { authToken: that.routeToken })
                window.close()
              }).catch(error => {
                console.warn(error)
                that.tokenValid = false
              })
            }
          }
        },
        immediate: true,
      },
    }
  }
</script>

<style lang="styl" type="text/stylus" scoped>
</style>