Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Emit an event when the CSRF token changes
Signed-off-by: Christoph Wurst <[email protected]>
  • Loading branch information
ChristophWurst authored and skjnldsv committed Oct 5, 2019
commit 8c023eeabdb3874470e5db1a756ed1b62ea3ecf9
12 changes: 9 additions & 3 deletions core/src/OC/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

import { subscribe } from '@nextcloud/event-bus'

import { addScript, addStyle } from './legacy-loader'
import {
ajaxConnectionLostHandler,
Expand Down Expand Up @@ -67,8 +69,7 @@ import {
getProtocol
} from './host'
import {
getToken as getRequestToken,
subscribe as subscribeToRequestTokenChange
getToken as getRequestToken
} from './requesttoken'
import {
hideMenus,
Expand Down Expand Up @@ -258,4 +259,9 @@ export default {
}

// Keep the request token prop in sync
subscribeToRequestTokenChange(token => { OC.requestToken = token })
subscribe('csrf-token-update', e => {
OC.requestToken = e.token

// Logging might help debug (Sentry) issues
console.info('OC.requestToken changed', e.token)
})
14 changes: 6 additions & 8 deletions core/src/OC/requesttoken.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,24 +19,22 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

import { emit } from '@nextcloud/event-bus'

let token = document.getElementsByTagName('head')[0].getAttribute('data-requesttoken')
const observers = []

/**
* @returns {string}
*/
export const getToken = () => token

/**
* @param {Function} observer observer
* @returns {number}
*/
export const subscribe = observer => observers.push(observer)

/**
* @param {String} newToken new token
*/
export const setToken = newToken => {
token = newToken
observers.forEach(o => o(token))

emit('csrf-token-update', {
token
})
}
15 changes: 15 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"dependencies": {
"@babel/polyfill": "^7.6.0",
"@chenfengyuan/vue-qrcode": "^1.0.1",
"@nextcloud/event-bus": "^0.2.0",
"@nextcloud/router": "^0.1.0",
"autosize": "^4.0.2",
"backbone": "^1.4.0",
Expand Down