Provide current-user application-wide

* This change allows us to reuse the current user in multiple places.
* Sentry still receives the same information as before.
This commit is contained in:
Sebastian Serth
2023-09-06 22:22:33 +02:00
committed by Sebastian Serth
parent 50a4707f65
commit 33aa0cecba
3 changed files with 8 additions and 6 deletions

View File

@ -36,6 +36,9 @@ $(document).on('turbolinks:load', function() {
window.location.reload();
});
// Set current user
window.current_user = JSON.parse($('meta[name="current-user"]')?.attr('content') || null);
// Set locale for all JavaScript functions
const htmlTag = $('html')
I18n.defaultLocale = htmlTag.data('default-locale');
@ -59,10 +62,8 @@ $(document).on('turbolinks:load', function() {
integrations: window.SentryIntegrations(),
profilesSampleRate: 1.0,
initialScope: scope =>{
const user = $('meta[name="current-user"]').attr('content');
if (user) {
scope.setUser(JSON.parse(user));
if (current_user) {
scope.setUser(_.omit(current_user, 'displayname'));
}
return scope;
}

View File

@ -84,11 +84,12 @@ class User < ApplicationRecord
displayname
end
def to_sentry_context
def to_page_context
{
id:,
type: self.class.name,
consumer: consumer.name,
displayname:,
}
end

View File

@ -16,7 +16,7 @@ html lang="#{I18n.locale || I18n.default_locale}" data-default-locale="#{I18n.de
= yield(:head)
= csrf_meta_tags
meta name='sentry' data-enabled=SentryJavascript.active?.to_s data-release=SentryJavascript.release data-dsn=SentryJavascript.dsn data-environment=SentryJavascript.environment
meta name='current-user' content=current_user&.to_sentry_context&.to_json
meta name='current-user' content=current_user&.to_page_context&.to_json
body
- unless @embed_options[:hide_navbar]
nav.navbar.bg-dark.navbar-expand-md.mb-4.py-1 role='navigation' data-bs-theme="dark"