Skip to content
Snippets Groups Projects
App.vue 4.24 KiB
Newer Older
<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
      },
      userOIDC: null,
      mgr: new oidc.UserManager({
        userStore: new oidc.WebStorageStateStore(),
        authority: process.env.API_STEERING_OIDC_URI,
        // the client id has to be a string, therefore we add the + ''
        client_id: process.env.OIDC_CLIENT_ID,
        redirect_uri: process.env.API_STEERING_OIDC_REDIRECT_URI,
        silent_redirect_uri: 'http://localhost:8080/static/oidc_callback_silentRenew.html',
        popup_redirect_uri: 'http://localhost:8080/static/oidc_callback_popupRenew.html',
        response_type: 'id_token token',
        scope: 'openid profile email',
        post_logout_redirect_uri: process.env.API_STEERING_OIDC_REDIRECT_URI_POSTLOGOUT,
        loadUserInfo: true,
        automaticSilentRenew: true
  },
  computed: {
    modules: function () {
      if (this.user.logged_in === true) return this.modules_logged_in
      else return this.modules_logged_out
    }
  },
  methods: {
    signIn () {
      this.mgr.signinRedirect().catch(function (err) {
        console.log(err)
      })
    },
    signOut () {
      let self = this
      this.mgr.signoutRedirect().then(function (resp) {
        self.user.logged_in = false
        console.log('signed out', resp)
      }).catch(function (err) {
        console.log(err)
      })
    },
    getUser () {
      let self = this
      this.mgr.getUser().then(function (u) {
        if (u == null) {
          self.user.logged_in = false
          self.user.name = ''
          self.user.email = ''
          self.user.access_token = ''
        } else {
          self.userOIDC = u
          self.user.logged_in = true
          self.user.name = u.profile.nickname
          self.user.email = u.profile.email
          self.user.access_token = u.access_token
          console.log(new Date(u.expires_at * 1000).toString())
          console.log(new Date(u.expires_at * 1000).toUTCString())
          console.log(u.access_token)
        }
      }).catch(function (err) {
        console.log(err)
      })
    }
  },
  mounted () {
    // TODO: remove oidc logging after thorough testing
    oidc.Log.logger = console
    let self = this
    this.mgr.events.addSilentRenewError(function () {
      // TODO: set accessTokenExpiringNotificationTime when this.mgr is created and insert this value here too
      alert('Your OpenID access token could not be renewed automatically. You will be logged out in 60 seconds.')
    })
    this.mgr.events.addAccessTokenExpiring(function () {
      console.log('token will soon expire')
      console.log(self)
      /* Use the following code instead of silent renewal if you want to use popups
      console.log('starting signinPopup')
      self.mgr.signinPopup(function (user) {
        console.log('signinPopup result for user:')
        console.log(user)
      }).catch(function (err) {
        console.log(err)
      })
      */
    })
    this.mgr.events.addAccessTokenExpired(function () {
      console.log('expired!')
    })
#app .content-width {
  margin: auto;
  width: 920px;
  max-width: 920px;
}