Skip to content

Commit e44a05e

Browse files
committed
Start auth
1 parent 9e80f36 commit e44a05e

File tree

7 files changed

+138
-20
lines changed

7 files changed

+138
-20
lines changed

api/auth/index.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { createAuthenticator } from "@glenstack/cf-workers-access";
2+
import { FABRuntime } from "@fab/core";
3+
4+
const AUTHENTICATION_DOMAIN = "gregbrimble.cloudflareaccess.com";
5+
const POLICY_AUD =
6+
"95c32a0c3411d058a18ae26e96f95b09f30f59da55f17973018f7f14a7eb6aca";
7+
8+
const loginHandler = async (request: Request): Promise<Response> => {
9+
const authenticator = await createAuthenticator(AUTHENTICATION_DOMAIN, {
10+
aud: POLICY_AUD,
11+
});
12+
13+
const jwt = await authenticator(request);
14+
15+
if (jwt)
16+
return new Response(null, {
17+
status: 301,
18+
headers: { Location: "/search" },
19+
});
20+
21+
return new Response(null, { status: 301, headers: { Location: "/" } });
22+
};
23+
24+
export default function login({ Router, ServerContext }: FABRuntime) {
25+
Router.on("/login", ({ request }) => loginHandler(request));
26+
Router.on("/logout", ({ request }) =>
27+
Promise.resolve(
28+
new Response(null, {
29+
status: 301,
30+
headers: { Location: "/cdn-cgi/access/logout" },
31+
})
32+
)
33+
);
34+
}

fab.config.json5

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
},
77
"@fab/plugin-precompile": {
88
"./api/graphql/index.ts": {},
9+
"./api/auth/index.ts": {
10+
_config: "./plugin-overrides.js",
11+
},
912
},
1013
"@fab/plugin-render-html": {
1114
fallback: "/index.html",

package-lock.json

Lines changed: 79 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"dependencies": {
66
"@apollo/client": "3.3.7",
77
"@fontsource/inter": "4.1.0",
8+
"@glenstack/cf-workers-access": "1.0.8",
89
"@glenstack/cf-workers-graphql": "1.0.4",
910
"@graphql-tools/schema": "6.0.18",
1011
"@headlessui/react": "0.2.0",

plugin-overrides.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
const webpack = require("webpack");
2+
3+
module.exports = {
4+
webpack: (config) => {
5+
config.plugins.push(
6+
new webpack.DefinePlugin({
7+
window: {},
8+
})
9+
);
10+
return config;
11+
},
12+
};

src/App.tsx

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { Home } from "./pages/Home";
2-
import { Login } from "./pages/Login";
1+
import { SearchPage } from "./pages/Search";
2+
// import { Login } from "./pages/Login";
33
import { BrowserRouter as Router, Switch, Route } from "react-router-dom";
44
import { ApolloProvider } from "@apollo/client";
55
import { client } from "./client";
@@ -9,11 +9,11 @@ function App() {
99
<ApolloProvider client={client}>
1010
<Router>
1111
<Switch>
12-
<Route path="/login" exact>
12+
{/* <Route path="/login" exact>
1313
<Login />
14-
</Route>
15-
<Route path="/" exact>
16-
<Home />
14+
</Route> */}
15+
<Route path="/search" exact>
16+
<SearchPage />
1717
</Route>
1818
</Switch>
1919
</Router>

src/pages/Home.tsx renamed to src/pages/Search.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ const SEARCH = gql`
3030
}
3131
`;
3232

33-
export const Home: FC = () => {
33+
export const SearchPage: FC = () => {
3434
const [isOpen, setIsOpen] = useState(false);
3535
const [searchTerm, setSearchTerm] = useState("");
3636

@@ -186,7 +186,7 @@ export const Home: FC = () => {
186186
type="search"
187187
name="search"
188188
value={searchTerm}
189-
onChange={event => setSearchTerm(event.target.value)}
189+
onChange={(event) => setSearchTerm(event.target.value)}
190190
/>
191191
</div>
192192
</div>
@@ -313,7 +313,7 @@ export const Home: FC = () => {
313313
type="search"
314314
name="search"
315315
value={searchTerm}
316-
onChange={event => setSearchTerm(event.target.value)}
316+
onChange={(event) => setSearchTerm(event.target.value)}
317317
/>
318318
</div>
319319
</div>

0 commit comments

Comments
 (0)