From 8ac2bc46762bc9459810c58f59c15608eeceddf1 Mon Sep 17 00:00:00 2001 From: Minion Date: Fri, 21 Jun 2024 11:21:18 +0100 Subject: [PATCH] login: authenticate-entity --- public/electron.js | 19 ++++- public/preload.js | 11 ++- src/Components/Login.js | 166 ++++++++++++++++++++++++---------------- src/grpcNode.js | 14 +++- 4 files changed, 139 insertions(+), 71 deletions(-) diff --git a/public/electron.js b/public/electron.js index cfac3e43..eb8b772a 100644 --- a/public/electron.js +++ b/public/electron.js @@ -111,10 +111,25 @@ ipcMain.handle( ipcMain.handle( "authenticate-entity", - async (event, { phoneNumber, password }) => { + async ( + event, + { + phoneNumber, + password, + client_publish_pub_key, + client_device_id_pub_key, + ownership_proof_response, + } + ) => { return new Promise((resolve, reject) => { authenticateEntity( - { phone_number: phoneNumber, password: password }, + { + phone_number: phoneNumber, + password: password, + client_publish_pub_key: client_publish_pub_key, + client_device_id_pub_key: client_device_id_pub_key, + ownership_proof_response: ownership_proof_response, + }, (err, response) => { if (err) { reject(err); diff --git a/public/preload.js b/public/preload.js index 9cc5850a..101c6e80 100644 --- a/public/preload.js +++ b/public/preload.js @@ -24,11 +24,20 @@ contextBridge.exposeInMainWorld("api", { throw error; } }, - authenticateEntity: async (phoneNumber, password) => { + authenticateEntity: async ( + phoneNumber, + password, + client_publish_pub_key, + client_device_id_pub_key, + ownership_proof_response + ) => { try { const response = await ipcRenderer.invoke("authenticate-entity", { phoneNumber, password, + client_publish_pub_key, + client_device_id_pub_key, + ownership_proof_response, }); return response; } catch (error) { diff --git a/src/Components/Login.js b/src/Components/Login.js index b4dd81d9..ba726738 100644 --- a/src/Components/Login.js +++ b/src/Components/Login.js @@ -1,12 +1,5 @@ import React, { useState } from "react"; -import { - TextField, - Button, - Box, - Dialog, - Typography, - Input, -} from "@mui/material"; +import { TextField, Button, Box, Dialog, Typography } from "@mui/material"; import PhoneInput from "react-phone-number-input"; import "react-phone-number-input/style.css"; import flags from "react-phone-number-input/flags"; @@ -16,47 +9,82 @@ import OTPDialog from "../Components/OTP"; function Login({ onClose, open }) { const { t } = useTranslation(); const [loading, setLoading] = useState(false); - const [phoneNumber, setPhoneNumber] = useState(""); - const [password, setPassword] = useState(""); + const [loginData, setLoginData] = useState({ + phoneNumber: "", + password: "", + }); const [responseMessage, setResponseMessage] = useState(""); const [otpOpen, setOtpOpen] = useState(false); + const [serverResponse, setServerResponse] = useState({ + server_publish_pub_key: "", + server_device_id_pub_key: "", + long_lived_token: "", + }); const handleClose = () => { onClose(); - setPhoneNumber(""); - setPassword(""); + setLoginData({ phoneNumber: "", password: "" }); setResponseMessage(""); setOtpOpen(false); - console.log("Dialog closed, state reset."); + setServerResponse({ + server_publish_pub_key: "", + server_device_id_pub_key: "", + long_lived_token: "", + }); }; - const handleAuthenticateEntity = async () => { - setLoading(true); - try { - const response = await window.api.authenticateEntity( - phoneNumber, - password - ); - console.log("Response:", response); - setResponseMessage(response.message); - if (response.requires_ownership_proof) { - setOtpOpen(true); // Open the OTP dialog if required + const handleLoginChange = (event) => { + const { name, value } = event.target; + setLoginData((prevData) => ({ + ...prevData, + [name]: value, + })); + }; + + const handleLoginSubmit = async (event) => { + event.preventDefault(); + const errors = {}; + if (!loginData.phoneNumber) errors.phoneNumber = "Phone number is required"; + if (!loginData.password) errors.password = "Password is required"; + + if (Object.keys(errors).length === 0) { + setLoading(true); + try { + const response = await window.api.authenticateEntity( + loginData.phoneNumber, + loginData.password + ); + console.log("Response:", response); + setResponseMessage(response.message); + if (response.requires_ownership_proof) { + setServerResponse({ + server_publish_pub_key: response.server_publish_pub_key, + server_device_id_pub_key: response.server_device_id_pub_key, + long_lived_token: response.long_lived_token, + }); + setOtpOpen(true); + } + } catch (error) { + console.error("Error:", error); + setResponseMessage(`Error: ${error.message}`); + } finally { + setLoading(false); } - } catch (error) { - console.error("Error:", error); - setResponseMessage(`Error: ${error.message}`); - } finally { - setLoading(false); } }; - const handleOtpSubmit = async (otp) => { setLoading(true); try { - const response = await window.api.verifyOtp(phoneNumber, otp); + const response = await window.api.authenticateEntity( + loginData.phoneNumber, + loginData.password, + "tWpZMXpbe8CBdWvftxE5BknAiMPWCaWV+OfGHIEtlj0=", + "jptPhaMJ+GCLoCKtlXcHHEKNYQjSspuWhj7E3uH2HBQ=", + otp + ); console.log("OTP Verification Response:", response); setResponseMessage(`OTP Verified: ${response.message}`); - handleClose(); // Close the dialog after successful OTP verification + handleClose(); } catch (error) { console.error("OTP Verification Error:", error); setResponseMessage(`OTP Verification Error: ${error.message}`); @@ -66,45 +94,49 @@ function Login({ onClose, open }) { }; return ( - - - - - setPassword(e.target.value)} - sx={{ mb: 4 }} - /> - {responseMessage && ( - {responseMessage} - )} - - + + + {t("login")} + +
+ + + setLoginData((prevData) => ({ ...prevData, phoneNumber: value })) + } + /> + + {responseMessage && ( + {responseMessage} + )} + + +
setOtpOpen(false)} onSubmit={handleOtpSubmit} + // value={loginData.otp} />
); diff --git a/src/grpcNode.js b/src/grpcNode.js index adbc8f6e..0a894c0e 100644 --- a/src/grpcNode.js +++ b/src/grpcNode.js @@ -50,11 +50,23 @@ function createEntity( ); } -function authenticateEntity({ phone_number, password }, callback) { +function authenticateEntity( + { + phone_number, + password, + client_publish_pub_key, + client_device_id_pub_key, + ownership_proof_response, + }, + callback +) { client.AuthenticateEntity( { phone_number, password, + client_publish_pub_key, + client_device_id_pub_key, + ownership_proof_response, }, function (err, response) {