Skip to content

Commit

Permalink
Merge pull request #20 from opticrd:fix/user-creation-on-iam
Browse files Browse the repository at this point in the history
fix: user registration on IAM fixed
  • Loading branch information
marluanespiritusanto authored May 10, 2023
2 parents b9c3800 + c8d49a5 commit 1b3ef8e
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 21 deletions.
28 changes: 19 additions & 9 deletions src/pages/api/biometric/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,21 +45,31 @@ export default async function handler(
responseType: "arraybuffer",
});

result = await rekognition.compareFaces({
SimilarityThreshold: 80,
TargetImage: {
Bytes: data,
},
SourceImage: {
Bytes: response.ReferenceImage.Bytes,
},
});
try {
result = await rekognition.compareFaces({
SimilarityThreshold: 80,
TargetImage: {
Bytes: data,
},
SourceImage: {
Bytes: response.ReferenceImage.Bytes,
},
});
} catch (ex) {
console.log(`Biometry validation failed for citizen ${cedula}`);

return res.status(500).json({
success: false,
});
}
}

const { FaceMatches } = result;
const isFaceMatched =
FaceMatches && FaceMatches.length && FaceMatches[0].Similarity > 90;

console.log(`Biometry validation successfully for citizen ${cedula}`);

return res.status(200).json({
match: isFaceMatched,
});
Expand Down
40 changes: 37 additions & 3 deletions src/pages/api/iam/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import { NextApiRequest, NextApiResponse } from "next/types";
import axios from "axios";
import axios, { AxiosError } from "axios";

import { VerifyIamUserResponse } from "../types";
import {
CitizensBasicInformationResponse,
VerifyIamUserResponse,
} from "../types";

export default async function handler(
req: NextApiRequest,
Expand All @@ -26,6 +29,37 @@ export default async function handler(
return res.status(200).json(data);
} else if (req.method === "POST") {
const { body } = req;
await http.post(`/auth/signup`, body);
const { username, email, password } = body;

let success: boolean = true;
let statusCode: number = 201;

const { data: citizen } = await axios.get<CitizensBasicInformationResponse>(
`${process.env.NEXT_PUBLIC_CEDULA_API}/${username}/info/basic?api-key=${process.env.NEXT_PUBLIC_CEDULA_API_KEY}`
);

try {
await http.post(`/auth/signup`, {
firstName: citizen.payload.names,
lastName: `${citizen.payload.firstSurname} ${citizen.payload.secondSurname}`,
username,
email,
password,
});

console.log(`Citizens ${username} created successfully on IAM`);
} catch (ex: unknown) {
console.log(`Error ocurred trying to create citizen ${username} on IAM`);
success = false;

if (ex instanceof AxiosError) {
const { response } = ex;
statusCode = response?.status ? response?.status : 500;
} else {
statusCode = 500;
}
}

return res.status(statusCode).json({ success });
}
}
14 changes: 5 additions & 9 deletions src/pages/register/stepper/step3.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -66,16 +66,12 @@ export default function Step3({ handleNext, infoCedula }: any) {
const onSubmit = (data: IFormInputs) => {
setLoading(true);

const obj = {
email: data.email,
username: infoCedula?.payload?.id,
firstName: infoCedula?.payload?.names,
lastName: `${infoCedula?.payload?.firstSurname} ${infoCedula?.payload?.secondSurname}`,
password: data.password,
};

axios
.post("/api/iam", obj)
.post("/api/iam", {
email: data.email,
username: infoCedula.id,
password: data.password,
})
.then(() => {
handleNext();
})
Expand Down

0 comments on commit 1b3ef8e

Please sign in to comment.