From 756a75d0aaf15aa207b6906a1dffa1955556cbf4 Mon Sep 17 00:00:00 2001 From: Dominika Liberda Date: Sun, 25 Oct 2020 02:30:47 +0100 Subject: [PATCH] login flow now works ;) also small refactor --- front/src/index.ts | 33 +++++++++++++++++++++++++++------ webroot/api/collect.shs | 4 ++-- webroot/api/login.shs | 8 +++++--- webroot/api/logout.shs | 3 +++ webroot/api/register.shs | 6 +++--- webroot/api/session.shs | 4 ++-- webroot/api/smscode.shs | 4 ++-- webroot/index.html | 4 +++- 8 files changed, 47 insertions(+), 19 deletions(-) create mode 100644 webroot/api/logout.shs diff --git a/front/src/index.ts b/front/src/index.ts index 3cbc631..e0e8b81 100644 --- a/front/src/index.ts +++ b/front/src/index.ts @@ -26,16 +26,14 @@ const openLockerConfirm = (lat: string, lon: string, openCode: string, id: strin id, }).then((res) => { if (res.status != 200) { - openError(res.description || res.error); + openError(res.description || res.msg); } }); }; const openLocker = (lat: string, lon: string, openCode: string, id: string) => { (document.querySelector(".areyousure-box") as HTMLElement).style.display = "block"; - document - .querySelector(".areyousure-button")! - .addEventListener("click", () => openLockerConfirm(lat, lon, openCode, id)); + (document.querySelector(".areyousure-button") as HTMLElement)!.onclick = () => { openLockerConfirm(lat, lon, openCode, id) }; }; const refreshPackages = () => @@ -78,9 +76,26 @@ window.addEventListener("load", () => { request("login", { login: form.login.value, password: form.password.value, + }).then((res) => { + if(res.status == 200) { + refreshPackages().then(() => { + [".login-box", ".register-box"].forEach((elementName) => { + (document.querySelector(elementName)! as HTMLDivElement).style.display = "none"; + }); + (document.querySelector(".package-list")! as HTMLDivElement).style.display = "block"; + }); + } else { + openError(res.msg || "Error during login. Try again in a while?") + } }); }); + const logout = document.querySelector(".logout-button")!; + logout.addEventListener("click", (event) => { + request("logout",{}).then(() => { + document.location = document.location}) + }) + const register = document.querySelector(".register-form")!; register.addEventListener("submit", async (event) => { event.preventDefault(); @@ -97,7 +112,7 @@ window.addEventListener("load", () => { ); (document.querySelector(".sms-box")! as HTMLDivElement).style.display = "block"; } else { - openError(res.description || res.error); + openError(res.description || res.msg); } }); @@ -114,7 +129,7 @@ window.addEventListener("load", () => { await refreshPackages(); (document.querySelector(".package-list")! as HTMLDivElement).style.display = "block"; } else { - openError(res.description || res.error); + openError(res.description || res.msg); } }); @@ -124,6 +139,12 @@ window.addEventListener("load", () => { errorBoxWindow.style.display = "none"; }); + const areYouNotSureButton = document.querySelector(".areyousure-button-deny")!; + areYouNotSureButton.addEventListener("click", () => { + (document.querySelector(".areyousure-box") as HTMLElement).style.display = "none"; + }); + + request("session", {}).then((res) => { if (res.status == 200) { // logged in diff --git a/webroot/api/collect.shs b/webroot/api/collect.shs index 53271b1..802ac18 100755 --- a/webroot/api/collect.shs +++ b/webroot/api/collect.shs @@ -8,13 +8,13 @@ source "${cfg[namespace]}/code/common.sh" data=$(curl -s -X POST -H "$(get_auth_string)" -H "Content-type: application/json" "https://api-inmobile-pl.easypack24.net/v1/collect/validate" --data '{"geoPoint":{"accuracy":"1","latitude":"'${post_data[lat]}'","longitude":"'${post_data[lon]}'"},"parcel":{"openCode":"'${post_data[openCode]}'","shipmentNumber":"'${post_data[id]}'"}}') if [[ $(echo $data | jq .status) == "403" ]]; then - echo $data | jq "{status: .status, error: .error}" + echo $data | jq "{status: .status, msg: .error}" else uuid=$(echo $data | jq -r ".sessionUuid") data=$(curl -X POST -H "$(get_auth_string)" "https://api-inmobile-pl.easypack24.net/v1/collect/compartment/open/$uuid") if [[ $(echo $data | jq ".compartment") != "" ]]; then echo $data else - jq -n '{status: $ARGS.positional[0], error: $ARGS.positional[1]}' --args -1 "emptyPage" + jq -n '{status: $ARGS.positional[0], msg: $ARGS.positional[1]}' --args -1 "emptyPage" fi fi diff --git a/webroot/api/login.shs b/webroot/api/login.shs index c0e4b1b..7de71a0 100644 --- a/webroot/api/login.shs +++ b/webroot/api/login.shs @@ -4,8 +4,10 @@ if [[ ${post_data[login]} != '' && ${post_data[password]} != '' ]]; then login ${post_data[login]} ${post_data[password]} status=$? if [[ $status == 0 ]]; then - jq -n '{status: $ARGS.positional[0], error: $ARGS.positional[1]}' --args 200 "success" + jq -n '{status: $ARGS.positional[0], msg: $ARGS.positional[1]}' --args 200 "success" elif [[ $status == 1 && $reason != '' ]]; then - jq -n '{status: $ARGS.positional[0], error: $ARGS.positional[1]}' --args -1 "$reason" + jq -n '{status: $ARGS.positional[0], msg: $ARGS.positional[1]}' --args -1 "$reason" fi -fi \ No newline at end of file +else + jq -n '{status: $ARGS.positional[0], msg: $ARGS.positional[1]}' --args -1 "Send me some credentials, dammit!" +fi diff --git a/webroot/api/logout.shs b/webroot/api/logout.shs new file mode 100644 index 0000000..a52937f --- /dev/null +++ b/webroot/api/logout.shs @@ -0,0 +1,3 @@ +#!/bin/bash +logout +jq -n '{status: $ARGS.positional[0], msg: $ARGS.positional[1]}' --args 200 "logged?? you?? out???? not sure, but probably" diff --git a/webroot/api/register.shs b/webroot/api/register.shs index f208c4f..858a53a 100644 --- a/webroot/api/register.shs +++ b/webroot/api/register.shs @@ -4,9 +4,9 @@ if [[ ${post_data[login]} != '' && ${post_data[password]} != '' && ${post_data[p register ${post_data[login]} ${post_data[password]} status=$? if [[ $status == 0 ]]; then - jq -n '{status: $ARGS.positional[0], error: $ARGS.positional[1]}' --args 200 "success" + jq -n '{status: $ARGS.positional[0], msg: $ARGS.positional[1]}' --args 200 "success" curl -s "https://api-inmobile-pl.easypack24.net/v1/sendSMSCode/${post_data[phone]}" > /dev/null elif [[ $status == 1 && $reason != '' ]]; then - jq -n '{status: $ARGS.positional[0], error: $ARGS.positional[1]}' --args -1 "$reason" + jq -n '{status: $ARGS.positional[0], msg: $ARGS.positional[1]}' --args -1 "$reason" fi -fi \ No newline at end of file +fi diff --git a/webroot/api/session.shs b/webroot/api/session.shs index 2b26794..73e7059 100644 --- a/webroot/api/session.shs +++ b/webroot/api/session.shs @@ -1,7 +1,7 @@ #!/bin/bash if ! session_verify ${cookies[sh_session]}; then - jq -n '{status: $ARGS.positional[0], error: $ARGS.positional[1]}' --args -1 "You're not logged in!" + jq -n '{status: $ARGS.positional[0], msg: $ARGS.positional[1]}' --args -1 "You're not logged in!" else - jq -n '{status: $ARGS.positional[0], error: $ARGS.positional[1]}' --args 200 "" + jq -n '{status: $ARGS.positional[0], msg: $ARGS.positional[1]}' --args 200 "OK" fi diff --git a/webroot/api/smscode.shs b/webroot/api/smscode.shs index e4e225b..ddd1ea7 100644 --- a/webroot/api/smscode.shs +++ b/webroot/api/smscode.shs @@ -9,10 +9,10 @@ if [[ ${post_data[phone]} != "" && ${post_data[code]} != "" ]]; then tokens=$(curl -s -X POST -H "Content-type: application/json" --data '{"phoneOS":"Android"}' "https://api-inmobile-pl.easypack24.net/v1/confirmSMSCode/${post_data[phone]}/${post_data[code]}") if [[ $(echo $tokens | jq .status) == "404" ]]; then - jq -n '{status: $ARGS.positional[0], error: $ARGS.positional[1]}' --args -1 "wrongData" + jq -n '{status: $ARGS.positional[0], msg: $ARGS.positional[1]}' --args -1 "wrongData" else add_account_refreshtoken $(echo $tokens | jq -r .refreshToken) add_account_authtoken $(echo $tokens | jq -r .authToken) - jq -n '{status: $ARGS.positional[0], error: $ARGS.positional[1]}' --args 200 "success" + jq -n '{status: $ARGS.positional[0], msg: $ARGS.positional[1]}' --args 200 "success" fi fi diff --git a/webroot/index.html b/webroot/index.html index 52a2705..67e6320 100644 --- a/webroot/index.html +++ b/webroot/index.html @@ -3,7 +3,7 @@ - OCW + Otwarty Czapkomat Webowy @@ -74,6 +74,7 @@ @@ -87,6 +88,7 @@
+
Log out