App.vue 4.34 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
15
16
17
18
19
20
21
22
23
import header from './components/Header.vue'
import footer from './components/Footer.vue'

export default {
  name: 'app',
  components: {
    'app-header': header,
    'app-footer': footer
  },
  data () {
    return {
24
25
26
27
28
29
      modules_logged_out: [
        { slug: 'home', title: 'Home' },
        { slug: 'credits', title: 'Credits' },
        { slug: 'debug', title: 'Debug' }
      ],
      modules_logged_in: [
30
        { slug: 'home', title: 'Home' },
31
        { slug: 'shows', title: 'Sendungen verwalten' },
32
33
        { slug: 'files', title: 'Dateien und Playlists' },
        { slug: 'settings', title: 'Settings' },
34
35
        { slug: 'credits', title: 'Credits' },
        { slug: 'debug', title: 'Debug' }
36
      ],
37
38
39
40
      user: {
        name: '',
        email: '',
        access_token: '',
41
        expires_at: 0,
42
43
44
        logged_in: false
      },
      userOIDC: null,
45
      oidcmgr: new oidc.UserManager({
46
47
        userStore: new oidc.WebStorageStateStore(),
        authority: process.env.API_STEERING_OIDC_URI,
48
49
        // the client id has to be a string, therefore we add the + ''
        client_id: process.env.OIDC_CLIENT_ID,
50
        redirect_uri: process.env.API_STEERING_OIDC_REDIRECT_URI,
51
52
        silent_redirect_uri: 'http://localhost:8080/static/oidc_callback_silentRenew.html',
        popup_redirect_uri: 'http://localhost:8080/static/oidc_callback_popupRenew.html',
53
        accessTokenExpiringNotificationTime: process.env.API_STEERING_OIDC_EXPIRE_NOTIFICATION,
54
55
56
        response_type: 'id_token token',
        scope: 'openid profile email',
        post_logout_redirect_uri: process.env.API_STEERING_OIDC_REDIRECT_URI_POSTLOGOUT,
57
        loadUserInfo: true
58
      })
59
    }
60
61
62
63
64
65
66
67
68
  },
  computed: {
    modules: function () {
      if (this.user.logged_in === true) return this.modules_logged_in
      else return this.modules_logged_out
    }
  },
  methods: {
    signIn () {
69
      this.oidcmgr.signinRedirect().catch(function (err) {
70
71
72
73
74
        console.log(err)
      })
    },
    signOut () {
      let self = this
75
      this.oidcmgr.signoutRedirect().then(function (resp) {
76
77
78
79
80
81
        self.user.logged_in = false
        console.log('signed out', resp)
      }).catch(function (err) {
        console.log(err)
      })
    },
82
    getOIDCUser () {
83
      let self = this
84
85
      this.oidcmgr.getUser().then(function (user) {
        if (user == null) {
86
87
88
89
90
          self.user.logged_in = false
          self.user.name = ''
          self.user.email = ''
          self.user.access_token = ''
        } else {
91
92
          // TODO: check user.expires_at
          // if token already expired try to get a new one or mark the user as logged out
93
          self.setUserProperties(user)
94
95
96
97
        }
      }).catch(function (err) {
        console.log(err)
      })
98
99
100
101
102
103
104
    },
    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
105
      // TODO: remove debug info after thorough testing
106
107
108
      console.log(new Date(user.expires_at * 1000).toString())
      console.log(new Date(user.expires_at * 1000).toUTCString())
      console.log(user.access_token)
109
110
111
    }
  },
  mounted () {
112
113
114
    // TODO: remove oidc logging after thorough testing
    oidc.Log.logger = console
    let self = this
115
    this.oidcmgr.events.addAccessTokenExpiring(function () {
116
      console.log('starting silent access_token renewal')
117
118
119
120
121
      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)
122
123
        alert('Your OpenID access token could not be renewed automatically.\n' +
          'You will be logged out in ' + process.env.API_STEERING_OIDC_EXPIRE_NOTIFICATION + ' seconds.')
124
      })
125
    })
126
    this.oidcmgr.events.addAccessTokenExpired(function () {
127
128
      console.log('expired!')
    })
129
    this.getOIDCUser()
130
131
132
133
134
135
  }
}
</script>

<style>
#app {
136
  color: #2e3436;
137
}
138
/*
139
140
141
142
143
#app .content-width {
  margin: auto;
  width: 920px;
  max-width: 920px;
}
144
*/
145
</style>