Skip to content
Snippets Groups Projects
App.vue 5.4 KiB
Newer Older
  • Learn to ignore specific revisions
  • <template>
      <div id="app">
    
        <app-header v-bind:modules="modules" v-bind:user="user"></app-header>
    
        <app-footer></app-footer>
      </div>
    </template>
    
    <script>
    
    import 'bootstrap/dist/css/bootstrap.css'
    import 'bootstrap-vue/dist/bootstrap-vue.css'
    
    import oidc from 'oidc-client'
    
    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 {
    
          modules_logged_out: [
            { slug: 'home', title: 'Home' },
            { slug: 'credits', title: 'Credits' },
            { slug: 'debug', title: 'Debug' }
          ],
          modules_logged_in: [
    
            { slug: 'home', title: 'Home' },
    
            { slug: 'shows', title: 'Sendungen verwalten' },
    
            { slug: 'files', title: 'Dateien und Playlists' },
            { slug: 'settings', title: 'Settings' },
    
            { slug: 'credits', title: 'Credits' },
            { slug: 'debug', title: 'Debug' }
    
          user: {
            name: '',
            email: '',
            access_token: '',
    
            logged_in: false,
            steeringUser: null
    
            userStore: new oidc.WebStorageStateStore(),
    
            authority: process.env.VUE_APP_API_STEERING_OIDC_URI,
    
            // the client id has to be a string, therefore we add the + ''
    
            client_id: process.env.VUE_APP_OIDC_CLIENT_ID,
            redirect_uri: process.env.VUE_APP_API_STEERING_OIDC_REDIRECT_URI,
    
            silent_redirect_uri: 'http://localhost:8080/oidc_callback_silentRenew.html',
            popup_redirect_uri: 'http://localhost:8080/oidc_callback_popupRenew.html',
    
            accessTokenExpiringNotificationTime: process.env.VUE_APP_API_STEERING_OIDC_EXPIRE_NOTIFICATION,
    
            response_type: 'id_token token',
            scope: 'openid profile email',
    
            post_logout_redirect_uri: process.env.VUE_APP_API_STEERING_OIDC_REDIRECT_URI_POSTLOGOUT,
    
      },
      computed: {
        modules: function () {
          if (this.user.logged_in === true) return this.modules_logged_in
          else return this.modules_logged_out
        }
      },
      methods: {
        signIn () {
    
          this.oidcmgr.signinRedirect().catch(function (err) {
    
            console.log(err)
          })
        },
        signOut () {
          let self = this
    
          this.oidcmgr.signoutRedirect().then(function (resp) {
    
            self.user.logged_in = false
            console.log('signed out', resp)
          }).catch(function (err) {
            console.log(err)
          })
        },
    
          axios.get(process.env.VUE_APP_API_STEERING + 'users/', {
    
            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)
          })
        },
    
          this.oidcmgr.getUser().then(function (user) {
            if (user == null) {
    
              self.user.logged_in = false
              self.user.name = ''
              self.user.email = ''
              self.user.access_token = ''
            } else {
    
              // TODO: check user.expires_at
              // if token already expired try to get a new one or mark the user as logged out
    
            }
          }).catch(function (err) {
            console.log(err)
          })
    
        },
        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
    
          // TODO: remove debug info after thorough testing
    
          console.log(new Date(user.expires_at * 1000).toString())
          console.log(new Date(user.expires_at * 1000).toUTCString())
          console.log(user.access_token)
    
        // TODO: remove oidc logging after thorough testing
        oidc.Log.logger = console
        let self = this
    
        this.oidcmgr.events.addAccessTokenExpiring(function () {
    
          console.log('starting silent access_token renewal')
    
          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)
    
            alert('Your OpenID access token could not be renewed automatically.\n' +
    
              'You will be logged out in ' + process.env.VUE_APP_API_STEERING_OIDC_EXPIRE_NOTIFICATION + ' seconds.')
    
        this.oidcmgr.events.addAccessTokenExpired(function () {
    
    #app .content-width {
      margin: auto;
      width: 920px;
      max-width: 920px;
    }