Skip to content

Commit

Permalink
Set admin user in Sentry
Browse files Browse the repository at this point in the history
  • Loading branch information
rakyi committed Jan 20, 2025
1 parent 1d28275 commit f0ef851
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 5 deletions.
9 changes: 9 additions & 0 deletions adminSiteClient/Admin.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import * as Sentry from "@sentry/react"
import ReactDOM from "react-dom"
import * as lodash from "lodash"
import { observable, computed, action } from "mobx"
Expand Down Expand Up @@ -30,18 +31,26 @@ export class Admin {
@observable errorMessage?: ErrorMessage
basePath: string
username: string
email: string
isSuperuser: boolean
settings: ClientSettings

constructor(props: {
username: string
email: string
isSuperuser: boolean
settings: ClientSettings
}) {
this.basePath = "/admin"
this.username = props.username
this.email = props.email
this.isSuperuser = props.isSuperuser
this.settings = props.settings

Sentry.setUser({
username: this.username,
email: this.email,
})
}

@observable currentRequests: Promise<Response>[] = []
Expand Down
12 changes: 7 additions & 5 deletions adminSiteServer/IndexPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,20 @@ import {
import { viteAssetsForAdmin } from "../site/viteUtils.js"

export const IndexPage = (props: {
email: string
username: string
isSuperuser: boolean
gitCmsBranchName: string
}) => {
const assets = viteAssetsForAdmin()
const script = `
window.isEditor = true
window.admin = new Admin({ username: "${
props.username
}", isSuperuser: ${props.isSuperuser.toString()}, settings: ${JSON.stringify(
{ ENV, GITHUB_USERNAME, DATA_API_FOR_ADMIN_UI }
)}})
window.admin = new Admin({
username: "${props.username}",
email: "${props.email}",
isSuperuser: ${props.isSuperuser.toString()},
settings: ${JSON.stringify({ ENV, GITHUB_USERNAME, DATA_API_FOR_ADMIN_UI })}
})
admin.start(document.querySelector("#app"), '${props.gitCmsBranchName}')
`

Expand Down
1 change: 1 addition & 0 deletions adminSiteServer/appClass.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ export class OwidAdminApp {
res.send(
renderToHtmlPage(
<IndexPage
email={res.locals.user.email}
username={res.locals.user.fullName}
isSuperuser={res.locals.user.isSuperuser}
gitCmsBranchName={this.gitCmsBranchName}
Expand Down
8 changes: 8 additions & 0 deletions adminSiteServer/authentication.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import * as Sentry from "@sentry/node"
import express from "express"
import crypto from "crypto"
import randomstring from "randomstring"
Expand Down Expand Up @@ -194,6 +195,13 @@ export async function authMiddleware(
if (user?.isActive) {
res.locals.session = session
res.locals.user = user

Sentry.setUser({
id: user.id,
email: user.email,
username: user.fullName,
})

return next()
} else if (!req.path.startsWith("/admin") || req.path === "/admin/login")
return next()
Expand Down

0 comments on commit f0ef851

Please sign in to comment.