App.vue 5.4 KB
Newer Older
1
2
<template>
  <div id="app">
3
    <app-header v-bind:modules="modules" v-bind:user="user"></app-header>
4
    <router-view/>
5
6
7
8
9
    <app-footer></app-footer>
  </div>
</template>

<script>
10
11
import 'bootstrap/dist/css/bootstrap.css'
import 'bootstrap-vue/dist/bootstrap-vue.css'
12
import oidc from 'oidc-client'
13
14
import header from './components/Header.vue'
import footer from './components/Footer.vue'
15
import axios from 'axios'
16
17
18
19
20
21
22
23
24

export default {
  name: 'app',
  components: {
    'app-header': header,
    'app-footer': footer
  },
  data () {
    return {
25
26
27
28
29
30
      modules_logged_out: [
        { slug: 'home', title: 'Home' },
        { slug: 'credits', title: 'Credits' },
        { slug: 'debug', title: 'Debug' }
      ],
      modules_logged_in: [
31
        { slug: 'home', title: 'Home' },
32
        { slug: 'shows', title: 'Sendungen verwalten' },
33
34
        { slug: 'files', title: 'Dateien und Playlists' },
        { slug: 'settings', title: 'Settings' },
35
36
        { slug: 'credits', title: 'Credits' },
        { slug: 'debug', title: 'Debug' }
37
      ],
38
39
40
41
      user: {
        name: '',
        email: '',
        access_token: '',
42
        expires_at: 0,
43
44
        logged_in: false,
        steeringUser: null
45
46
      },
      userOIDC: null,
47
      oidcmgr: new oidc.UserManager({
48
        userStore: new oidc.WebStorageStateStore(),
49
        authority: process.env.VUE_APP_API_STEERING_OIDC_URI,
50
        // the client id has to be a string, therefore we add the + ''
51
52
        client_id: process.env.VUE_APP_OIDC_CLIENT_ID,
        redirect_uri: process.env.VUE_APP_API_STEERING_OIDC_REDIRECT_URI,
53
54
        silent_redirect_uri: 'http://localhost:8080/oidc_callback_silentRenew.html',
        popup_redirect_uri: 'http://localhost:8080/oidc_callback_popupRenew.html',
55
        accessTokenExpiringNotificationTime: process.env.VUE_APP_API_STEERING_OIDC_EXPIRE_NOTIFICATION,
56
57
        response_type: 'id_token token',
        scope: 'openid profile email',
58
        post_logout_redirect_uri: process.env.VUE_APP_API_STEERING_OIDC_REDIRECT_URI_POSTLOGOUT,
59
        loadUserInfo: true
60
      })
61
    }
62
63
64
65
66
67
68
69
70
  },
  computed: {
    modules: function () {
      if (this.user.logged_in === true) return this.modules_logged_in
      else return this.modules_logged_out
    }
  },
  methods: {
    signIn () {
71
      this.oidcmgr.signinRedirect().catch(function (err) {
72
73
74
75
76
        console.log(err)
      })
    },
    signOut () {
      let self = this
77
      this.oidcmgr.signoutRedirect().then(function (resp) {
78
79
80
81
82
83
        self.user.logged_in = false
        console.log('signed out', resp)
      }).catch(function (err) {
        console.log(err)
      })
    },
84
    getSteeringUser () {
85
      axios.get(process.env.VUE_APP_API_STEERING + 'users/', {
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
        withCredentials: true,
        headers: { 'Authorization': 'Bearer ' + this.user.access_token }
      }).then(response => {
        if (response.data.length === 0) {
          alert('No user profile data provided by steering backend!')
        } else if (response.data.length === 1) {
          this.user.steeringUser = response.data[0]
        } else {
          // in case we are a superuser, we get all users returned
          // so we have to iterate through the user list to find out own profile
          for (var u in response.data) {
            if (response.data[u].username === this.user.name) {
              this.user.steeringUser = response.data[u]
              break
            }
          }
        }
      }).catch(error => {
        alert('There was an error fetching user data from the steering backend: ' + error)
      })
    },
107
    getOIDCUser () {
108
      let self = this
109
110
      this.oidcmgr.getUser().then(function (user) {
        if (user == null) {
111
112
113
114
115
          self.user.logged_in = false
          self.user.name = ''
          self.user.email = ''
          self.user.access_token = ''
        } else {
116
117
          // TODO: check user.expires_at
          // if token already expired try to get a new one or mark the user as logged out
118
          self.setUserProperties(user)
119
          self.getSteeringUser()
120
121
122
123
        }
      }).catch(function (err) {
        console.log(err)
      })
124
125
126
127
128
129
130
    },
    setUserProperties (user) {
      this.userOIDC = user
      this.user.logged_in = true
      this.user.name = user.profile.nickname
      this.user.email = user.profile.email
      this.user.access_token = user.access_token
131
      // TODO: remove debug info after thorough testing
132
133
134
      console.log(new Date(user.expires_at * 1000).toString())
      console.log(new Date(user.expires_at * 1000).toUTCString())
      console.log(user.access_token)
135
136
137
    }
  },
  mounted () {
138
139
140
    // TODO: remove oidc logging after thorough testing
    oidc.Log.logger = console
    let self = this
141
    this.oidcmgr.events.addAccessTokenExpiring(function () {
142
      console.log('starting silent access_token renewal')
143
144
145
146
147
      self.oidcmgr.signinSilent().then(function (user) {
        self.user.access_token = user.access_token
        console.log(self.user.access_token)
      }).catch(function (err) {
        console.log(err)
148
        alert('Your OpenID access token could not be renewed automatically.\n' +
149
          'You will be logged out in ' + process.env.VUE_APP_API_STEERING_OIDC_EXPIRE_NOTIFICATION + ' seconds.')
150
      })
151
    })
152
    this.oidcmgr.events.addAccessTokenExpired(function () {
153
      console.log('expired!')
154
      self.signOut()
155
    })
156
    this.getOIDCUser()
157
158
159
160
161
162
  }
}
</script>

<style>
#app {
163
  color: #2e3436;
164
}
165
/*
166
167
168
169
170
#app .content-width {
  margin: auto;
  width: 920px;
  max-width: 920px;
}
171
*/
172
</style>