Skip to content

Commit

Permalink
fix register flow
Browse files Browse the repository at this point in the history
  • Loading branch information
aryaniyaps committed Mar 5, 2024
1 parent 1254506 commit 5effcea
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 30 deletions.
10 changes: 3 additions & 7 deletions client/app/(authentication)/register/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,17 @@ export default async function RegisterPage() {
const cookieStore = cookies();
const flowId = cookieStore.get(REGISTER_FLOW_ID_COOKIE);

let flow;
let flow = null;

if (flowId) {
try {
console.log('GETTING FLOW DATA: ', flowId.value);
const { data } = await client.GET('/auth/register/flows', {
params: { cookie: { register_flow_id: flowId.value } },
const { data } = await client.GET('/auth/register/flows/{flow_id}', {
params: { path: { flow_id: flowId.value } },
});
if (data) {
console.log('GOT FLOW DATA');
flow = data;
}
} catch (err) {
console.log(typeof err);
console.log(JSON.stringify(err, null, 4));
// TODO: handle errs better
// TODO: delete register flow ID cookie if it is invalid
// cookieStore.delete(REGISTER_FLOW_ID_COOKIE);
Expand Down
10 changes: 5 additions & 5 deletions client/components/register/email-verification.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const registerVerificationSchema = yup
.required();

export default function RegisterEmailVerification() {
const { setCurrentStep, flow: flowData } = useRegisterFlow();
const { setCurrentStep, flow } = useRegisterFlow();

const router = useRouter();

Expand All @@ -43,7 +43,7 @@ export default function RegisterEmailVerification() {
// verify register flow
const { data } = await client.POST('/auth/register/flows/verify', {
body: { verificationCode: input.verificationCode },
params: { cookie: { register_flow_id: flowData!.id } },
params: { cookie: { register_flow_id: flow!.id } },
});

if (data) {
Expand All @@ -61,7 +61,7 @@ export default function RegisterEmailVerification() {
const resendVerificationCode = async () => {
await client.POST('/auth/register/flows/resend-verification', {
params: {
cookie: { register_flow_id: flowData!.id },
cookie: { register_flow_id: flow!.id },
header: { 'user-agent': navigator.userAgent },
},
});
Expand All @@ -73,7 +73,7 @@ export default function RegisterEmailVerification() {
<div className='flex flex-col gap-unit-2'>
<h1 className='text-md font-semibold'>Enter Verification Code</h1>
<h3 className='text-xs font-extralight'>
Enter the code we sent to {flowData?.email}
Enter the code we sent to {flow?.email}
</h3>
</div>
<Button size='sm' variant='ghost' onClick={resendVerificationCode}>
Expand Down Expand Up @@ -163,7 +163,7 @@ export default function RegisterEmailVerification() {
fullWidth
onClick={async () => {
await client.POST('/auth/register/flows/cancel', {
params: { cookie: { register_flow_id: flowData!.id } },
params: { cookie: { register_flow_id: flow!.id } },
});
router.refresh();
}}
Expand Down
17 changes: 12 additions & 5 deletions client/components/register/flow-provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,31 @@ import { createContext, useContext, useState } from 'react';
interface RegisterFlowContextType {
setCurrentStep: (step: string | null) => void;
currentStep: string | null;
flow: { email: string; currentStep: string; id: string } | undefined;
flow: { email: string; currentStep: string; id: string } | null;
setFlow: (flow: { email: string; currentStep: string; id: string }) => void;
}

// TODO: pass register flow email here
const RegisterFlowContext = createContext<RegisterFlowContextType>({
setCurrentStep(step) {},
currentStep: null,
flow: undefined,
setFlow(flow) {},
flow: null,
});

export const useRegisterFlow = () => useContext(RegisterFlowContext);

interface RegisterFlowProviderProps {
flow: { email: string; currentStep: string; id: string } | undefined;
flow: { email: string; currentStep: string; id: string } | null;
}

export const RegisterFlowProvider: React.FC<
React.PropsWithChildren<RegisterFlowProviderProps>
> = ({ children, flow }) => {
const [localFlow, setFlow] = useState<{
email: string;
currentStep: string;
id: string;
} | null>(flow ? flow : null);
const [localCurrentStep, setCurrentStep] = useState<string | null>(
flow ? flow.currentStep : null
);
Expand All @@ -32,7 +38,8 @@ export const RegisterFlowProvider: React.FC<
value={{
setCurrentStep,
currentStep: localCurrentStep,
flow,
flow: localFlow,
setFlow,
}}
>
{children}
Expand Down
3 changes: 2 additions & 1 deletion client/components/register/flow-start.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const registerSchema = yup
.required();

export default function RegisterFlowStart() {
const { setCurrentStep } = useRegisterFlow();
const { setCurrentStep, setFlow } = useRegisterFlow();

const { handleSubmit, control, formState, setError } = useForm({
resolver: yupResolver(registerSchema),
Expand All @@ -46,6 +46,7 @@ export default function RegisterFlowStart() {
});

if (data) {
setFlow(data.registerFlow);
setCurrentStep(data.registerFlow.currentStep);
}
} catch (err) {
Expand Down
6 changes: 3 additions & 3 deletions client/generated/api/v1.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export interface paths {
*/
get: operations["OpenAPITag.USERS-get_user"];
};
"/auth/register/flows": {
"/auth/register/flows/{flow_id}": {
/**
* Get a register flow.
* @description Get a register flow.
Expand Down Expand Up @@ -849,8 +849,8 @@ export interface operations {
*/
"OpenAPITag.AUTHENTICATION-get_register_flow": {
parameters: {
cookie: {
register_flow_id: string;
path: {
flow_id: string;
};
};
responses: {
Expand Down
2 changes: 1 addition & 1 deletion schema/openapi.json

Large diffs are not rendered by default.

20 changes: 12 additions & 8 deletions server/app/routes/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from uuid import UUID

import user_agents
from fastapi import APIRouter, Cookie, Depends, Header, Response
from fastapi import APIRouter, Cookie, Depends, Header, Path, Response
from webauthn.helpers import (
parse_authentication_credential_json,
parse_registration_credential_json,
Expand Down Expand Up @@ -52,7 +52,7 @@


@auth_router.get(
"/register/flows",
"/register/flows/{flow_id}",
response_model=RegisterFlowSchema,
responses={
HTTPStatus.NOT_FOUND: {
Expand All @@ -63,9 +63,9 @@
summary="Get a register flow.",
)
async def get_register_flow(
register_flow_id: Annotated[
str,
Cookie(
flow_id: Annotated[
UUID,
Path(
title="The ID of the register flow.",
),
],
Expand All @@ -77,9 +77,7 @@ async def get_register_flow(
],
) -> RegisterFlow:
"""Get a register flow."""
return await auth_service.get_register_flow(
flow_id=UUID(register_flow_id),
)
return await auth_service.get_register_flow(flow_id=flow_id)


@auth_router.post(
Expand Down Expand Up @@ -311,6 +309,12 @@ async def finish_webauthn_register_flow(
credential=parse_registration_credential_json(data.credential),
)

# remove register flow ID from cookie
response.delete_cookie(
key=REGISTER_FLOW_ID_COOKIE,
secure=settings.is_production(),
)

# set authentication token in a cookie
response.set_cookie(
key=AUTHENTICATION_TOKEN_COOKIE,
Expand Down

0 comments on commit 5effcea

Please sign in to comment.