Skip to content

Commit

Permalink
inertia-fying
Browse files Browse the repository at this point in the history
  • Loading branch information
dcordz committed Dec 16, 2024
1 parent a00dd1b commit 141e7d1
Show file tree
Hide file tree
Showing 7 changed files with 195 additions and 143 deletions.
1 change: 1 addition & 0 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ def route_component(route)
render json: {route: ROUTES[:REGISTRATION], phone:}
else
Rails.logger.info "ServerRendering.route - Route to page - #{route}"

render json: {route:, phone:}
end
end
Expand Down
3 changes: 3 additions & 0 deletions app/controllers/legislators_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
class LegislatorsController < ApplicationController
# GET /legislators or /legislators.json
def index

# binding.pry

T.unsafe(self).render_legislators(
lambda do
{
Expand Down
7 changes: 5 additions & 2 deletions app/controllers/users/webauthn/sessions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,12 @@ def callback

session[:verified_phone] = user.phone
if user.is_registration_complete
T.unsafe(self).route_legislators
# T.unsafe(self).route_legislators

redirect_to legislators_path
else
T.unsafe(self).route_registration
# T.unsafe(self).route_registration
redirect_to registration_path
end
rescue WebAuthn::Error => e
Sentry.capture_exception(e)
Expand Down
73 changes: 52 additions & 21 deletions app/frontend/hooks/authentication/useWebAuthnAuthentication.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,26 @@ import { handleError, logDev } from "app/frontend/sway_utils";
import { PublicKeyCredentialRequestOptionsJSON } from "node_modules/@github/webauthn-json/dist/types/basic/json";
import { useCallback, useState } from "react";
import { sway } from "sway";
import { UseInertiaFormProps } from "use-inertia-form";

export const useWebAuthnAuthentication = (onAuthenticated: (user: sway.IUser) => void) => {
export const useWebAuthnAuthentication = (
verify: UseInertiaFormProps<
sway.IUser & { publicKeyCredential?: webauthnJson.PublicKeyCredentialWithAssertionJSON }
>["post"],
transform: UseInertiaFormProps<
sway.IUser & { publicKeyCredential?: webauthnJson.PublicKeyCredentialWithAssertionJSON }
>["transform"],
onAuthenticated: (user: sway.IUser) => Promise<void>,
) => {
const { post: authenticate } = useAxios_NOT_Authenticated_POST_PUT<
PublicKeyCredentialRequestOptionsJSON | sway.IValidationResult
>("/users/webauthn/sessions", {
errorHandler: (e) => {
throw e;
},
});
const { post: verify } = useAxios_NOT_Authenticated_POST_PUT<sway.IUser>("/users/webauthn/sessions/callback");

// const { post: verify } = useAxios_NOT_Authenticated_POST_PUT<sway.IUser>("/users/webauthn/sessions/callback");

const [isLoading, setLoading] = useState<boolean>(false);

Expand All @@ -41,41 +51,62 @@ export const useWebAuthnAuthentication = (onAuthenticated: (user: sway.IUser) =>
throw e;
})
.finally(() => {
setLoading(false);
// setLoading(false);
});
},
[authenticate],
);

// https://github.com/Yubico/java-webauthn-server/#3-registration
const verifyAuthentication = useCallback(
async (phone: string, publicKeyCredential: webauthnJson.PublicKeyCredentialWithAssertionJSON) => {
(phone: string, publicKeyCredential: webauthnJson.PublicKeyCredentialWithAssertionJSON) => {
if (!publicKeyCredential) {
logDev("verifyAuthentication - no public key credential");
setLoading(false);
return;
}

return verify({
transform((data) => ({
...data,
phone,
publicKeyCredential,
})
.then((result) => {
setLoading(false);
if (result) {
logDev(
"useWebAuthnAuthentication.verifyAuthentication.verify - Verified Auth. Calling onAuthenticated with result -",
result,
);
onAuthenticated(result as sway.IUser);
}
return result;
})
.catch((e) => {
}));

return verify("/users/webauthn/sessions/callback", {
onSuccess: () => {
onAuthenticated({ phone } as sway.IUser);
},
onError: (e) => {
console.error(e);
},
onFinish: () => {
setLoading(false);
handleError(e);
});
},
});
// .then((result) => {
// logDev("verifyAuthentication.callback.result -", result);
// if (result) {
// logDev(
// "useWebAuthnAuthentication.verifyAuthentication.verify - Verified Auth. Calling onAuthenticated with result -",
// result,
// );
// // onAuthenticated(result as sway.IUser)
// // .then(() => {
// // setLoading(false);
// // return result;
// // })
// // .catch(console.error);
// } else {
// setLoading(false);
// return result;
// }
// })
// .catch((e) => {
// setLoading(false);
// handleError(e);
// });
},
[verify, onAuthenticated],
[transform, verify, onAuthenticated],
);

return {
Expand Down
20 changes: 10 additions & 10 deletions app/frontend/hooks/useAxios.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import { router } from "@inertiajs/react";
import axios, { AxiosError, AxiosResponse } from "axios";
import { useCallback, useEffect, useMemo, useState } from "react";
import { sway } from "sway";
import { DEFAULT_ERROR_MESSAGE, handleError, logDev, notify } from "../sway_utils";
import { isFailedRequest } from "../sway_utils/http";
import { useCancellable } from "./useCancellable";
import { removeNonDigits } from "app/frontend/sway_utils/phone";

type TPayload = Record<number, any> | Record<string, any> | FormData;

Expand Down Expand Up @@ -53,11 +51,12 @@ const handleAxiosError = (ex: AxiosError | Error) => {

const handleRoutedResponse = (result: IRoutableResponse) => {
if (result.phone) {
localStorage.setItem("@sway/phone", removeNonDigits(result.phone));
// localStorage.setItem("@sway/phone", removeNonDigits(result.phone));
}
if (result.route) {
router.visit(result.route);
// router.visit(result.route);
}
return result;
};

/*
Expand Down Expand Up @@ -124,6 +123,7 @@ export const useAxiosGet = <T extends IRoutableResponse>(
// message: (result as sway.IValidationResult)?.message || DEFAULT_ERROR_MESSAGE,
});
} else if ("route" in result && result.route) {
// @ts-ignore

Check warning on line 126 in app/frontend/hooks/useAxios.ts

View workflow job for this annotation

GitHub Actions / eslint

Use "@ts-expect-error" instead of "@ts-ignore", as "@ts-ignore" will do nothing if the following line is error-free
return handleRoutedResponse(result);
} else if (options?.defaultValue) {
setItems(options.defaultValue);
Expand Down Expand Up @@ -192,8 +192,8 @@ export const useAxiosPost = <T extends IRoutableResponse>(
if (!result) {
return null;
} else if ("route" in result && result.route) {
handleRoutedResponse(result);
return null;
// @ts-ignore

Check warning on line 195 in app/frontend/hooks/useAxios.ts

View workflow job for this annotation

GitHub Actions / eslint

Use "@ts-expect-error" instead of "@ts-ignore", as "@ts-ignore" will do nothing if the following line is error-free
return handleRoutedResponse(result);
} else if (isFailedRequest(result)) {
if (options?.notifyOnValidationResultFailure) {
notify({
Expand Down Expand Up @@ -383,8 +383,8 @@ export const useAxios_NOT_Authenticated_GET = <T extends IRoutableResponse>(
if (!result) {
return null;
} else if ("route" in result && result.route) {
handleRoutedResponse(result);
return null;
// @ts-ignore

Check warning on line 386 in app/frontend/hooks/useAxios.ts

View workflow job for this annotation

GitHub Actions / eslint

Use "@ts-expect-error" instead of "@ts-ignore", as "@ts-ignore" will do nothing if the following line is error-free
return handleRoutedResponse(result);
} else if (isFailedRequest(result)) {
if (options?.notifyOnValidationResultFailure) {
notify({
Expand Down Expand Up @@ -455,8 +455,8 @@ export const useAxios_NOT_Authenticated_POST_PUT = <T extends IRoutableResponse>
if (!result) {
return null;
} else if ("route" in result && result.route) {
handleRoutedResponse(result);
return null;
// @ts-ignore

Check warning on line 458 in app/frontend/hooks/useAxios.ts

View workflow job for this annotation

GitHub Actions / eslint

Use "@ts-expect-error" instead of "@ts-ignore", as "@ts-ignore" will do nothing if the following line is error-free
return handleRoutedResponse(result);
} else if (isFailedRequest(result)) {
if (notifyOnValidationResultFailure) {
notify({
Expand Down
Loading

0 comments on commit 141e7d1

Please sign in to comment.