[+] Merge Account Branch: Implement Register, Login, Logout, Delete

[+] Merge Account Branch: Implement Register, Login, Logout, Delete
This commit is contained in:
Hykilpikonna
2021-01-22 18:16:26 -05:00
committed by GitHub
3 changed files with 70 additions and 50 deletions
+34 -16
View File
@@ -7,6 +7,9 @@
import UIKit
/**
Account view controller controlling the two separate view controllers
*/
class AccountViewController: UIViewController
{
@IBOutlet var vLogin: UIView!
@@ -80,29 +83,28 @@ class LoginVC: UIViewController
return
}
// Error messages
let errors = ["409 - [\"A0111\"]": "Account already exists, please login instead.",
"401 -": "Incorrect username/password",
"404 -": "Username does not exist in the database",
]
// Send register request
let a = alert(login ? "Logging in..." : "Registering...", "Please Wait")
send(login ? APIs.login : APIs.register, ["username": name, "password": pass.sha256])
sendReq(login ? APIs.login : APIs.register,
title: login ? "Logging in..." : "Registering...", errors: errors,
params: ["username": name, "password": pass.sha256])
{
// Store username and password
localStorage["name"] = name
localStorage["pass"] = pass.sha256
localStorage["id"] = $0
a.dismiss
{
// Send feedback
if login { self.msg("Login success!", "Now you can use account features, yay!") }
else { self.msg("Registration success!", "Now you have an account, yay!") }
// Hide registration and show account detail view
ui { AccountViewController.this.login() }
}
}
err:
{
print($0)
a.dismiss { self.msg("An error occurred", "Maybe the server is on fire, just wait a few hours.") }
// Send feedback
if login { self.msg("Login success!", "Now you can use account features, yay!") }
else { self.msg("Registration success!", "Now you have an account, yay!") }
// Hide registration and show account detail view
AccountViewController.this.login()
}
}
@@ -123,6 +125,9 @@ class LoginVC: UIViewController
}
}
/**
Account manage view controller
*/
class ManageVC: UIViewController
{
static var this: ManageVC!
@@ -157,4 +162,17 @@ class ManageVC: UIViewController
{
AccountViewController.this.logout()
}
/**
Called when the user clicks the delete account button
*/
@IBAction func deleteAccount(_ sender: Any)
{
sendReq(APIs.delete, title: "Deleting...")
{
print("Deleted! \($0)")
self.msg("Deleted!", "You are erased from our database, you no longer exist.")
self.logout(sender)
}
}
}
+17 -33
View File
@@ -638,7 +638,7 @@
<textField opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="left" contentVerticalAlignment="center" borderStyle="roundedRect" textAlignment="natural" minimumFontSize="17" translatesAutoresizingMaskIntoConstraints="NO" id="3pJ-zP-vKy">
<rect key="frame" x="100" y="0.0" width="274" height="34"/>
<fontDescription key="fontDescription" type="system" pointSize="14"/>
<textInputTraits key="textInputTraits" secureTextEntry="YES" textContentType="new-password"/>
<textInputTraits key="textInputTraits" secureTextEntry="YES"/>
</textField>
</subviews>
</stackView>
@@ -690,13 +690,13 @@
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<stackView opaque="NO" contentMode="scaleToFill" axis="vertical" spacing="20" translatesAutoresizingMaskIntoConstraints="NO" id="gYR-mG-zfS">
<rect key="frame" x="20" y="0.0" width="374" height="371"/>
<rect key="frame" x="20" y="0.0" width="374" height="270"/>
<subviews>
<view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="gUf-g1-1af">
<rect key="frame" x="0.0" y="0.0" width="374" height="170"/>
<subviews>
<imageView clipsSubviews="YES" userInteractionEnabled="NO" contentMode="scaleAspectFit" horizontalHuggingPriority="251" verticalHuggingPriority="251" translatesAutoresizingMaskIntoConstraints="NO" id="yVW-fL-SIQ">
<rect key="frame" x="132" y="0.0" width="110" height="110"/>
<imageView clipsSubviews="YES" userInteractionEnabled="NO" contentMode="scaleAspectFit" horizontalHuggingPriority="251" verticalHuggingPriority="251" image="person.crop.circle.badge.checkmark" catalog="system" translatesAutoresizingMaskIntoConstraints="NO" id="yVW-fL-SIQ">
<rect key="frame" x="130.5" y="0.0" width="111.5" height="109.5"/>
<constraints>
<constraint firstAttribute="width" constant="110" id="So8-k5-nLS"/>
<constraint firstAttribute="height" constant="110" id="inA-vb-CrL"/>
@@ -728,44 +728,23 @@
<constraint firstAttribute="trailing" secondItem="cT0-Ux-t7H" secondAttribute="trailing" constant="20" id="yRX-lY-fXX"/>
</constraints>
</view>
<stackView opaque="NO" contentMode="scaleToFill" spacing="20" translatesAutoresizingMaskIntoConstraints="NO" id="o9J-ZW-PSX">
<rect key="frame" x="0.0" y="190" width="374" height="31"/>
<subviews>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Auto Backup" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="QTC-5i-6b9">
<rect key="frame" x="0.0" y="0.0" width="305" height="31"/>
<fontDescription key="fontDescription" type="system" pointSize="17"/>
<nil key="textColor"/>
<nil key="highlightedColor"/>
</label>
<switch opaque="NO" contentMode="scaleToFill" horizontalHuggingPriority="750" verticalHuggingPriority="750" contentHorizontalAlignment="center" contentVerticalAlignment="center" on="YES" translatesAutoresizingMaskIntoConstraints="NO" id="fRF-sU-NPD">
<rect key="frame" x="325" y="0.0" width="51" height="31"/>
</switch>
</subviews>
</stackView>
<stackView opaque="NO" contentMode="scaleToFill" distribution="fillEqually" translatesAutoresizingMaskIntoConstraints="NO" id="eUn-CF-BxW">
<rect key="frame" x="0.0" y="241" width="374" height="30"/>
<subviews>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="system" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="qrI-ZV-4EL">
<rect key="frame" x="0.0" y="0.0" width="187" height="30"/>
<state key="normal" title="Backup Config"/>
</button>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="system" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="adM-2W-pdW">
<rect key="frame" x="187" y="0.0" width="187" height="30"/>
<state key="normal" title="Restore Backup"/>
</button>
</subviews>
</stackView>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="system" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="3pG-br-PVl">
<rect key="frame" x="0.0" y="291" width="374" height="30"/>
<rect key="frame" x="0.0" y="190" width="374" height="30"/>
<state key="normal" title="Log Out">
<color key="titleColor" systemColor="systemOrangeColor"/>
</state>
<connections>
<action selector="logout:" destination="3gV-kF-UbK" eventType="touchUpInside" id="dyQ-AR-u9Y"/>
</connections>
</button>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="system" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="awy-Du-cqu">
<rect key="frame" x="0.0" y="341" width="374" height="30"/>
<rect key="frame" x="0.0" y="240" width="374" height="30"/>
<state key="normal" title="Delete Account">
<color key="titleColor" systemColor="systemRedColor"/>
</state>
<connections>
<action selector="deleteAccount:" destination="3gV-kF-UbK" eventType="touchUpInside" id="iQ9-m9-VIm"/>
</connections>
</button>
</subviews>
</stackView>
@@ -778,6 +757,10 @@
<constraint firstAttribute="trailing" secondItem="gYR-mG-zfS" secondAttribute="trailing" constant="20" id="fxQ-LY-4QQ"/>
</constraints>
</view>
<connections>
<outlet property="lJoinDate" destination="cT0-Ux-t7H" id="1XG-r8-Qr4"/>
<outlet property="lUsername" destination="kAN-ll-4wh" id="3Ta-rI-wql"/>
</connections>
</viewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="pLc-eQ-yBW" userLabel="First Responder" customClass="UIResponder" sceneMemberID="firstResponder"/>
</objects>
@@ -789,6 +772,7 @@
<image name="checkmark" catalog="system" width="128" height="114"/>
<image name="hammer.fill" catalog="system" width="128" height="117"/>
<image name="person.crop.circle" catalog="system" width="128" height="121"/>
<image name="person.crop.circle.badge.checkmark" catalog="system" width="128" height="113"/>
<image name="trash.fill" catalog="system" width="121" height="128"/>
<systemColor name="secondaryLabelColor">
<color red="0.23529411764705882" green="0.23529411764705882" blue="0.2627450980392157" alpha="0.59999999999999998" colorSpace="custom" customColorSpace="sRGB"/>
+19 -1
View File
@@ -139,7 +139,25 @@ extension UIViewController
func msg(_ title: String, _ message: String) -> UIAlertController { alert(title, message, okayable: true) }
/// More convenient dismiss function
func dismiss(_ completion: (() -> Void)? = nil) { dismiss(animated: false, completion: completion) }
func dismiss(_ completion: (() -> Void)? = nil) { ui { self.dismiss(animated: false, completion: completion) } }
/**
Send a http request even more conveniently
*/
func sendReq<T: Decodable>(_ api: API<T>, title: String, errors: [String: String] = [:], params: [String: String]? = [:], _ success: @escaping (T) -> Void, err: @escaping (String) -> Void = {it in})
{
// Send request
let a = alert(title, "Please Wait")
send(api, params) { it in a.dismiss { success(it) } }
err:
{
// Display error message
print("===== Error: \($0) =====")
let message = errors[$0.trimmingCharacters(in: .whitespaces)]
?? "Maybe the server is on fire, just wait a few hours."
a.dismiss { self.msg("An error occurred", message) }
}
}
}