<commit_before>module.exports = (function () {
    
    var _tokenRefreshTimeout = null

    // Default

    function _beforeEach (transition) {
        var auth = _toArray(transition.to.auth)

        if (auth && (auth === true || auth.constructor === Array)) {
            if (!this.check()) {
                this.$router.replace(this.getOption('authRedirect'))
            } else if (auth.constructor === Array && !_compare(auth, _toArray(this.user()[this.getOption('rolesVar')]))) {
                this.$router.replace(this.getOption('forbiddenRedirect'))
            } else {
                return transition.next()
            }
        } else if (auth === false && this.check()) {
            this.$router.replace(this.getOption('notFoundRedirect'))
        } else {
            return transition.next()
        }
    }

    function _userData (res) {
        return res.data || res
    }

    function _cookieDomain () {
        return window.location.hostname
    }

    function _invalidToken (res) {
        if (res.status === 401) {
            this.logout(this.getOption('logoutRedirect'))
        }
    }

    // Utils

    function _getUrl () {
        var port = window.location.port

        return window.location.protocol + '//' + window.location.hostname + (port ? ':' + port : '')
    }

    function _compare (one, two) {
        var i, ii

        one = _toArray(one)
        two = _toArray(two)

        if (one.constructor !== Array || two.constructor !== Array) {
            return false
        }

        for (i = 0, ii = one.length; i < ii; i++) {
            if (two.indexOf(one[i]) >= 0) {
                return true
            }
        }

        return false
    }

    function _toArray (val) {
        return (typeof val) === 'string' ? [val] : val
    }

    // Remember Me

    function _setCookie (name, value, timeOffset) {
        var domain = this.getOption('cookieDomain').call(this),
            expires = (new Date((new Date()).getTime() + timeOffset)).toUTCString()

        document.cookie = (name + '=' + value + '; domain=' + domain + '; path=/;' + (timeOffset ? ' expires=' + expires + ';' : ''))
    }

    function _setRememberMeCookie (rememberMe) {
        _setCookie.call(this,
            'rememberMe',
            rememberMe === true ? 'true' : 'false',
            rememberMe === true ? 12096e5 : undefined
        )
    }

    function _removeRememberMeCookie () {
        _setCookie.call(this, 'rememberMe', 'false', -12096e5)
    }

    // Token

    function _setToken (token) {
        if (token) {
            localStorage.setItem((this.other() ? 'login-as-' : '') + this.getOption('tokenName'), token)
        }
    }

    function _getToken () {
        return localStorage.getItem((this.other() ? 'login-as-' : '') + this.getOption('tokenName'))
    }

    function _removeToken () {
        localStorage.removeItem((this.other() ? 'login-as-' : '') + this.getOption('tokenName'))
    }

    function _refreshToken () {
        var _this = this

        if (_getToken.call(this)) {
            this.$http.get(this.getOption('tokenUrl'), function (res) {
                var tokenJSON = _decodeToken(_getToken.call(_this)),
                    expireTime = _getTokenExpirationDate(tokenJSON).valueOf(),
                    nowTime = new Date().valueOf(),
                    offsetTime = this.getOption('tokenTimeoutOffset'),
                    timeout = expireTime - nowTime - offsetTime;

                clearTimeout(_tokenRefreshTimeout)

                _tokenRefreshTimeout = setTimeout(function () {
                    _refreshToken.call(_this)
                }, timeout)
            });   
        }
    }

    // Token util

    function _urlBase64Decode (str) {
        let output = str.replace(/-/g, '+').replace(/_/g, '/')
        
        switch (output.length % 4) {
            case 0: { break }
            case 2: { output += '=='; break }
            case 3: { output += '='; break }
            default: {
                console.log('Illegal base64url string!')
            }
        }
    
        return decodeURIComponent(escape(window.atob(output))) // polifyll https://github.com/davidchambers/Base64.js
    }

    function _decodeToken (token) {
        if ( ! token) { return; }

        let parts = token.split('.')

        if (parts.length !== 3) {
            throw new Error('JWT must have 3 parts')
        }

        let decoded = _urlBase64Decode(parts[1])
        
        if ( ! decoded) {
            throw new Error('Cannot decode the token')
        }

        return JSON.parse(decoded)
    }
  
    function _getTokenExpirationDate (tokenJSON) {
        if ( ! tokenJSON || ! tokenJSON.exp) { return }
    
        let d = new Date(0) // The 0 here is the key, which sets the date to the epoch
        
        d.setUTCSeconds(tokenJSON.exp)

        return d
    }

    // function _isTokenExpired (token, offsetSeconds) {
    //     if ( ! token) { return true; }

    //     let d = _getTokenExpirationDate(token)
        
    //     offsetSeconds = offsetSeconds || 0
    
    //     if (d === null) {
    //         return false
    //     }

    //     // Token expired?
    //     return !(d.valueOf() > (new Date().valueOf() + (offsetSeconds * 1000)))
    // }

    // Router
  
    function _setRoute (route, router) {
        this.$route = route
        this.$router = router
    }

    // Auth
    
    function _login (path, data, rememberMe, redirectUrl, options) {
        options = options || {}

        this.$http.post(path, data, function (res) {
            var _this = this

            _setRememberMeCookie.call(this, rememberMe)

            _setToken.call(this, res[this.getOption('tokenVar')])<commit_msg>Remove dead comments.<commit_after>module.exports = (function () {
    
    var _tokenRefreshTimeout = null

    // Default

    function _beforeEach (transition) {
        var auth = _toArray(transition.to.auth)

        if (auth && (auth === true || auth.constructor === Array)) {
            if (!this.check()) {
                this.$router.replace(this.getOption('authRedirect'))
            } else if (auth.constructor === Array && !_compare(auth, _toArray(this.user()[this.getOption('rolesVar')]))) {
                this.$router.replace(this.getOption('forbiddenRedirect'))
            } else {
                return transition.next()
            }
        } else if (auth === false && this.check()) {
            this.$router.replace(this.getOption('notFoundRedirect'))
        } else {
            return transition.next()
        }
    }

    function _userData (res) {
        return res.data || res
    }

    function _cookieDomain () {
        return window.location.hostname
    }

    function _invalidToken (res) {
        if (res.status === 401) {
            this.logout(this.getOption('logoutRedirect'))
        }
    }

    // Utils

    function _getUrl () {
        var port = window.location.port

        return window.location.protocol + '//' + window.location.hostname + (port ? ':' + port : '')
    }

    function _compare (one, two) {
        var i, ii

        one = _toArray(one)
        two = _toArray(two)

        if (one.constructor !== Array || two.constructor !== Array) {
            return false
        }

        for (i = 0, ii = one.length; i < ii; i++) {
            if (two.indexOf(one[i]) >= 0) {
                return true
            }
        }

        return false
    }

    function _toArray (val) {
        return (typeof val) === 'string' ? [val] : val
    }

    // Remember Me

    function _setCookie (name, value, timeOffset) {
        var domain = this.getOption('cookieDomain').call(this),
            expires = (new Date((new Date()).getTime() + timeOffset)).toUTCString()

        document.cookie = (name + '=' + value + '; domain=' + domain + '; path=/;' + (timeOffset ? ' expires=' + expires + ';' : ''))
    }

    function _setRememberMeCookie (rememberMe) {
        _setCookie.call(this,
            'rememberMe',
            rememberMe === true ? 'true' : 'false',
            rememberMe === true ? 12096e5 : undefined
        )
    }

    function _removeRememberMeCookie () {
        _setCookie.call(this, 'rememberMe', 'false', -12096e5)
    }

    // Token

    function _setToken (token) {
        if (token) {
            localStorage.setItem((this.other() ? 'login-as-' : '') + this.getOption('tokenName'), token)
        }
    }

    function _getToken () {
        return localStorage.getItem((this.other() ? 'login-as-' : '') + this.getOption('tokenName'))
    }

    function _removeToken () {
        localStorage.removeItem((this.other() ? 'login-as-' : '') + this.getOption('tokenName'))
    }

    function _refreshToken () {
        var _this = this

        if (_getToken.call(this)) {
            this.$http.get(this.getOption('tokenUrl'), function (res) {
                var tokenJSON = _decodeToken(_getToken.call(_this)),
                    expireTime = _getTokenExpirationDate(tokenJSON).valueOf(),
                    nowTime = new Date().valueOf(),
                    offsetTime = this.getOption('tokenTimeoutOffset'),
                    timeout = expireTime - nowTime - offsetTime;

                clearTimeout(_tokenRefreshTimeout)

                _tokenRefreshTimeout = setTimeout(function () {
                    _refreshToken.call(_this)
                }, timeout)
            });   
        }
    }

    // Token util

    function _urlBase64Decode (str) {
        let output = str.replace(/-/g, '+').replace(/_/g, '/')
        
        switch (output.length % 4) {
            case 0: { break }
            case 2: { output += '=='; break }
            case 3: { output += '='; break }
            default: {
                console.log('Illegal base64url string!')
            }
        }
    
        return decodeURIComponent(escape(window.atob(output))) // polifyll https://github.com/davidchambers/Base64.js
    }

    function _decodeToken (token) {
        if ( ! token) { return; }

        let parts = token.split('.')

        if (parts.length !== 3) {
            throw new Error('JWT must have 3 parts')
        }

        let decoded = _urlBase64Decode(parts[1])
        
        if ( ! decoded) {
            throw new Error('Cannot decode the token')
        }

        return JSON.parse(decoded)
    }
  
    function _getTokenExpirationDate (tokenJSON) {
        if ( ! tokenJSON || ! tokenJSON.exp) { return }
    
        let d = new Date(0) // The 0 here is the key, which sets the date to the epoch
        
        d.setUTCSeconds(tokenJSON.exp)

        return d
    }

    // Router
  
    function _setRoute (route, router) {
        this.$route = route
        this.$router = router
    }

    // Auth
    
    function _login (path, data, rememberMe, redirectUrl, options) {
        options = options || {}

        this.$http.post(path, data, function (res) {
            var _this = this

            _setRememberMeCookie.call(this, rememberMe)

            _setToken.call(this, res[this.getOption('tokenVar')])
{'fraction_non_alphanumeric': 0.13607281247183925, 'fraction_numerical': 0.00657835451022799, 'mean_word_length': 1.7717282717282716, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 6, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2920610', 'n_tokens_mistral': 3283, 'n_tokens_neox': 3100, 'n_words': 674}