Skip to content

Commit

Permalink
feat(auth): User profile and viewer context (#4480)
Browse files Browse the repository at this point in the history
* viewer node

* feat(auth): rudamentary viewer context and profile page
  • Loading branch information
mikeldking authored and Parker-Stafford committed Sep 4, 2024
1 parent aa998cf commit 51b8122
Show file tree
Hide file tree
Showing 10 changed files with 429 additions and 6 deletions.
2 changes: 1 addition & 1 deletion app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@
"build:relay": "relay-compiler",
"test": "jest --config ./jest.config.js",
"dev": "pnpm run dev:server:init & pnpm run build:static && pnpm run build:relay && vite",
"dev:auth": "export DANGEROUSLY_SET_PHOENIX_ENABLE_AUTH=true && export DANGEROUSLY_SET_PHOENIX_SECRET=secret && pnpm run dev:server & pnpm run build:static && pnpm run build:relay && vite",
"dev:auth": "export DANGEROUSLY_SET_PHOENIX_ENABLE_AUTH=true && export DANGEROUSLY_SET_PHOENIX_SECRET=secretsecretsecretsecretsecretsecretsecret1 && pnpm run dev:server & pnpm run build:static && pnpm run build:relay && vite",
"dev:ui": "pnpm run build:static && pnpm run build:relay && vite",
"dev:server:init": "python -m phoenix.server.main --dev serve --with-fixture=chatbot --with-project=demo_llama_index --force-fixture-ingestion",
"dev:server": "python -m phoenix.server.main --dev serve",
Expand Down
5 changes: 3 additions & 2 deletions app/src/Routes.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import { embeddingLoaderQuery$data } from "./pages/embedding/__generated__/embed
import { projectLoaderQuery$data } from "./pages/project/__generated__/projectLoaderQuery.graphql";
import {
APIsPage,
AuthenticatedRoot,
authenticatedRootLoader,
datasetLoader,
DatasetPage,
DatasetsPage,
Expand All @@ -23,7 +25,6 @@ import {
experimentsLoader,
ExperimentsPage,
homeLoader,
Layout,
LoginPage,
ModelPage,
ModelRoot,
Expand All @@ -41,7 +42,7 @@ const router = createBrowserRouter(
createRoutesFromElements(
<Route path="/" errorElement={<ErrorElement />}>
<Route path="/login" element={<LoginPage />} />
<Route element={<Layout />}>
<Route element={<AuthenticatedRoot />} loader={authenticatedRootLoader}>
<Route
path="/profile"
handle={{ crumb: () => "profile" }}
Expand Down
52 changes: 52 additions & 0 deletions app/src/contexts/ViewerContext.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import React from "react";
import { graphql, useRefetchableFragment } from "react-relay";

import {
ViewerContext_viewer$data,
ViewerContext_viewer$key,
} from "./__generated__/ViewerContext_viewer.graphql";

export type ViewerContextType = {
viewer: ViewerContext_viewer$data["viewer"];
};

export const ViewerContext = React.createContext<ViewerContextType>({
viewer: null,
});

export function useViewer() {
const context = React.useContext(ViewerContext);
if (context == null) {
throw new Error("useViewer must be used within a ViewerProvider");
}
return context;
}

export function ViewerProvider({
query,
children,
}: React.PropsWithChildren<{
query: ViewerContext_viewer$key;
}>) {
const [data] = useRefetchableFragment(
graphql`
fragment ViewerContext_viewer on Query
@refetchable(queryName: "ViewerContextRefetchQuery") {
viewer {
id
username
email
role {
name
}
}
}
`,
query
);
return (
<ViewerContext.Provider value={{ viewer: data.viewer }}>
{children}
</ViewerContext.Provider>
);
}
108 changes: 108 additions & 0 deletions app/src/contexts/__generated__/ViewerContextRefetchQuery.graphql.ts

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

100 changes: 100 additions & 0 deletions app/src/contexts/__generated__/ViewerContext_viewer.graphql.ts

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

19 changes: 19 additions & 0 deletions app/src/pages/AuthenticatedRoot.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import React from "react";
import { useLoaderData } from "react-router";

import { ViewerProvider } from "@phoenix/contexts/ViewerContext";

import { authenticatedRootLoaderQuery$data } from "./__generated__/authenticatedRootLoaderQuery.graphql";
import { Layout } from "./Layout";

/**
* The root of the authenticated application. Note that authentication might be entirely disabled
*/
export function AuthenticatedRoot() {
const loaderData = useLoaderData() as authenticatedRootLoaderQuery$data;
return (
<ViewerProvider query={loaderData}>
<Layout />
</ViewerProvider>
);
}
Loading

0 comments on commit 51b8122

Please sign in to comment.