diff --git a/API/Functions/Users/index.js b/API/Functions/Users/index.js index 8ce45c3..4f53e1c 100644 --- a/API/Functions/Users/index.js +++ b/API/Functions/Users/index.js @@ -1,5 +1,5 @@ const UserRepo = require("./../../../shared/database/repos/user.repo"); - +const { encrypt, decrypt } = require('../../../shared/utils/crypto') module.exports.getUserInformation = async () => { try { const result = await UserRepo.getUser( @@ -66,3 +66,41 @@ module.exports.addNewCategory = async (event) => { }; } }; + +module.exports.checkSecretKey = async (event) => { + const body = event.body ? JSON.parse(event.body) : {}; + + if (!body.secretKey) return { + statusCode: 400, + headers: { + "Access-Control-Allow-Origin": "*", + } + } + + const user = await UserRepo.getUser({ + emails: process.env.EMAIL_USERNAME, + }) + + if (!user.secretKey) return { + statusCode: 409, + headers: { + "Access-Control-Allow-Origin": "*", + } + } + + const userKey = decrypt(user.secretKey) + + if (userKey !== body.secretKey) return { + statusCode: 401, + headers: { + "Access-Control-Allow-Origin": "*", + } + } + + return { + statusCode: 200, + headers: { + "Access-Control-Allow-Origin": "*", + } + } +} \ No newline at end of file diff --git a/client/src/App.js b/client/src/App.js index 699a830..af7ac11 100644 --- a/client/src/App.js +++ b/client/src/App.js @@ -14,7 +14,7 @@ class App extends React.Component { constructor(props) { super(props); this.state = { - secret: "null", + secret: null, user: {}, banks: [], prepayments: [], @@ -37,12 +37,27 @@ class App extends React.Component { }; componentDidMount = () => { + if(!this.state.secretKey) return; + this.loadInitialData() + }; + + loadInitialData(){ this.getPrePayments(); this.getUserInformation() - }; + } + componentDidUpdate(prevProps, prevState){ + const { secret } = prevState + if(!secret && this.state.secret){ + this.loadInitialData() + } + } onLoginClick = (secret) => { - console.log(secret); + axios.post(constants.basepath + constants.routes.secret, { + secretKey: secret + }).then(result=>{ + this.setState({secret: true}) + }).catch(err=>console.error(err)) }; getUserInformation = () => { @@ -89,6 +104,7 @@ class App extends React.Component { } }) } + render() { const { prepayments, secret, user } = this.state; diff --git a/client/src/components/SecretCodeScreen.js b/client/src/components/SecretCodeScreen.js index 2bd88a8..e9c0565 100644 --- a/client/src/components/SecretCodeScreen.js +++ b/client/src/components/SecretCodeScreen.js @@ -2,29 +2,31 @@ import React from "react"; import { PasswordStrengthMeter, TextField, Icon, Button } from "emerald-ui/lib"; const SecretCodeScreen = (props) => { - const { - onLoginClick = ()=>null - } = props - const [secretPassword, setSecret] = React.useState('') + const { onLoginClick = () => null } = props; + const [secretPassword, setSecret] = React.useState(""); return (