Skip to content

Commit

Permalink
Set Sentry user data when authenticating
Browse files Browse the repository at this point in the history
  • Loading branch information
jb3 committed Jul 10, 2024
1 parent d96ea39 commit f875821
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/api/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import { AxiosResponse } from "axios";
import { startAuthorizing, finishAuthorizing } from "../slices/authorization";
import formsStore from "../store";

import * as Sentry from "@sentry/react";

import APIClient from "./client";

const OAUTH2_CLIENT_ID = process.env.REACT_APP_OAUTH2_CLIENT_ID;
Expand Down Expand Up @@ -230,6 +232,10 @@ export async function refreshBackendJWT(): Promise<boolean> {
APIClient.post("/auth/refresh").then((response: AxiosResponse<AuthResult>) => {
cookies.set(CookieNames.Username, response.data.username, {sameSite: "strict", secure: PRODUCTION, path: "/", expires: new Date(3000, 1)});

Sentry.setUser({
username: response.data.username
});

const expiry = Date.parse(response.data.expiry);
setTimeout(refreshBackendJWT, ((expiry - Date.now()) / 1000 * 0.9));
}).catch(() => {
Expand All @@ -245,6 +251,7 @@ export async function refreshBackendJWT(): Promise<boolean> {
*/
export function clearAuth(): void {
const cookies = new Cookies();
Sentry.setUser(null);
cookies.remove(CookieNames.Scopes, {path: "/"});
cookies.remove(CookieNames.Username, {path: "/"});
}
Expand Down Expand Up @@ -278,6 +285,10 @@ export default async function authorize(scopes: OAuthScopes[] = [], disableFunct
const options: CookieSetOptions = {sameSite: "strict", secure: PRODUCTION, path: "/", expires: new Date(3000, 1)};
cookies.set(CookieNames.Username, backend_response.username, options);

Sentry.setUser({
username: backend_response.username,
});

options.maxAge = backend_response.maxAge;
cookies.set(CookieNames.Scopes, discord_response.cleanedScopes, options);

Expand Down

0 comments on commit f875821

Please sign in to comment.