Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit edaa8b5

Browse files
committed
Work in progress...
1 parent 50759aa commit edaa8b5

File tree

4 files changed

+199
-85
lines changed

4 files changed

+199
-85
lines changed

‎LogInWithEncryption.xcodeproj/project.pbxproj‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
/* Begin PBXBuildFile section */
1010
6C4DA2588EB7463266AAC741 /* Pods_LogInWithEncryption.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = E2DD3160538EE4770881CB54 /* Pods_LogInWithEncryption.framework */; };
11+
FF21C60122E3723F00F4F916 /* ResponderableTextField.swift in Sources */ = {isa = PBXBuildFile; fileRef = FF21C60022E3723F00F4F916 /* ResponderableTextField.swift */; };
1112
FFAFB76522DBB98E006EEFE4 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = FFAFB76422DBB98E006EEFE4 /* AppDelegate.swift */; };
1213
FFAFB76722DBB98E006EEFE4 /* SceneDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = FFAFB76622DBB98E006EEFE4 /* SceneDelegate.swift */; };
1314
FFAFB76922DBB98E006EEFE4 /* ContentView.swift in Sources */ = {isa = PBXBuildFile; fileRef = FFAFB76822DBB98E006EEFE4 /* ContentView.swift */; };
@@ -40,6 +41,7 @@
4041
A2341C04860CDCF8DF46EA94 /* Pods-LogInWithEncryption.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-LogInWithEncryption.release.xcconfig"; path = "Target Support Files/Pods-LogInWithEncryption/Pods-LogInWithEncryption.release.xcconfig"; sourceTree = "<group>"; };
4142
AD51390352AA6BACE4BE32E6 /* Pods-LogInWithEncryption.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-LogInWithEncryption.debug.xcconfig"; path = "Target Support Files/Pods-LogInWithEncryption/Pods-LogInWithEncryption.debug.xcconfig"; sourceTree = "<group>"; };
4243
E2DD3160538EE4770881CB54 /* Pods_LogInWithEncryption.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_LogInWithEncryption.framework; sourceTree = BUILT_PRODUCTS_DIR; };
44+
FF21C60022E3723F00F4F916 /* ResponderableTextField.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ResponderableTextField.swift; sourceTree = "<group>"; };
4345
FFAFB76122DBB98E006EEFE4 /* LogInWithEncryption.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = LogInWithEncryption.app; sourceTree = BUILT_PRODUCTS_DIR; };
4446
FFAFB76422DBB98E006EEFE4 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
4547
FFAFB76622DBB98E006EEFE4 /* SceneDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SceneDelegate.swift; sourceTree = "<group>"; };
@@ -133,6 +135,7 @@
133135
FFAFB77222DBB990006EEFE4 /* Info.plist */,
134136
FFAFB76C22DBB990006EEFE4 /* Preview Content */,
135137
FFAFB79622DBBF68006EEFE4 /* Extensions.swift */,
138+
FF21C60022E3723F00F4F916 /* ResponderableTextField.swift */,
136139
);
137140
path = LogInWithEncryption;
138141
sourceTree = "<group>";
@@ -339,6 +342,7 @@
339342
buildActionMask = 2147483647;
340343
files = (
341344
FFAFB76522DBB98E006EEFE4 /* AppDelegate.swift in Sources */,
345+
FF21C60122E3723F00F4F916 /* ResponderableTextField.swift in Sources */,
342346
FFAFB76722DBB98E006EEFE4 /* SceneDelegate.swift in Sources */,
343347
FFAFB76922DBB98E006EEFE4 /* ContentView.swift in Sources */,
344348
FFAFB79722DBBF68006EEFE4 /* Extensions.swift in Sources */,

‎LogInWithEncryption/ContentView.swift‎

Lines changed: 125 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -13,50 +13,80 @@
1313
//
1414

1515
import SwiftUI
16+
import UIKit
1617

1718
let key = "rosesR3dS0m3A3eBlu3Oth3S@R3N0Tee"
1819
let iv = "ThisIsATestYaKno"
1920

2021
struct ContentView : View {
2122

2223
@State var isLoggedIn: Bool = false
23-
24-
var body: some View {
25-
// if self.$isLoggedIn {
26-
// LoggedInView.init()
27-
// } else {
28-
SignInView()
29-
// }
30-
}
31-
}
32-
33-
struct SignInView : View {
34-
24+
@State var showUsernameAlert: Bool = false
25+
@State var showPasswordAlert: Bool = false
3526
@State var typedUsername: String = ""
3627
@State var typedPassword: String = ""
3728

3829
var availableUsers = ["Manenga", "Mungandi"]
3930

31+
let dism = Alert.Button.destructive(Text("OK"))
32+
4033
var body: some View {
34+
35+
ZStack {
36+
HomeView.opacity(isLoggedIn ? 1 : 0)
37+
AuthView.opacity(isLoggedIn ? 0 : 1)
38+
// Alert(title: Text("empty username"), dismissButton: dism)
39+
// Alert(title: Text("Please enter a username")).opacity(showUsernameAlert ? 0 : 1)
40+
// Alert(title: Text("Please enter a password")).opacity(isLoggedIn ? 0 : 1)
41+
42+
}
43+
}
44+
45+
@State var isEditingUsers: Bool = false
46+
47+
private let spacing: Length = 18
48+
private let signInBtnText = Text("Sign In").color(.white).frame(width: UIScreen.main.bounds.width - 80 ,height: 45)
49+
private let signUpBtnText = Text("Sign up").color(.blue).frame(width: UIScreen.main.bounds.width - 80 ,height: 45)
50+
51+
var AuthView: some View {
4152
VStack {
4253
Text("Status: You are not signed in!").color(.red)
4354
Spacer()
4455

4556
VStack {
46-
VStack(spacing: 18) {
47-
TextField($typedUsername, placeholder: Text("Username"), onEditingChanged: { changed in
48-
print("onEditing: \(changed)")
49-
}).textFieldStyle(.roundedBorder)
57+
VStack(spacing: spacing) {
58+
VStack(alignment: .leading, spacing: 6) {
59+
Text("Username")
60+
.font(.system(size: 15))
61+
.opacity(0.5)
62+
63+
ResponderableTextField(text: $typedUsername,
64+
isFirstResponder: true,
65+
keyboardType: .default)
66+
.padding(10)
67+
.frame(height: 50)
68+
.border(Color.black, width: 0.3, cornerRadius: 10)
69+
}
70+
VStack(alignment: .leading, spacing: 6) {
71+
Text("Password")
72+
.font(.system(size: 15))
73+
.opacity(0.5)
74+
75+
SecureField($typedPassword, onCommit: {
76+
// to do
77+
})
78+
.padding(10)
79+
.frame(height: 50)
80+
.border(Color.black, width: 0.3, cornerRadius: 10)
81+
}
5082

51-
SecureField($typedPassword, placeholder: Text("Password")).textFieldStyle(.roundedBorder)
5283
Button(action: login) {
53-
HStack{
54-
Text("Login")
55-
.color(.white)
56-
.frame(width: UIScreen.main.bounds.width - 80 ,height: 45)
57-
}
58-
}.background(Color.blue)
84+
signInBtnText
85+
}.background(Color.blue)
86+
Button(action: signUp) {
87+
signUpBtnText
5988
}
89+
}
6090
.padding().cornerRadius(16)
6191
.background(Color.gray.opacity(0.05))
6292
.frame(height: 80, alignment: .center)
@@ -68,93 +98,109 @@ struct SignInView : View {
6898
Text("\(self.availableUsers[0ドル])")
6999
}
70100
}
71-
}.listStyle(.grouped)
72-
.frame(height: 160, alignment: .top)
73-
}.padding(25)
101+
}.listStyle(.grouped)
102+
.frame(height: 160, alignment: .top)
103+
}.padding(25)
104+
}
105+
106+
107+
108+
109+
var HomeView: some View {
110+
VStack {
111+
Text("Status: You are signed in!").color(.green)
112+
Spacer()
113+
Button(action: logout) {
114+
Text("Logout")
115+
}.foregroundColor(.purple).padding(7)
116+
}.padding(25)
74117
}
75118

76119
func doCredentialsMatch() -> Bool {
77120
// check if typed credentials match stored credentials
78121

79-
let dbEncryptedUsername = UserDefaults.standard.string(forKey: "encryptedUsername") ?? ""
80-
let dbEncryptedPassword = UserDefaults.standard.string(forKey: "encryptedPassword") ?? ""
122+
let storedEncryptedPassword = UserDefaults.standard.string(forKey: "encryptedPassword") ?? ""
81123

82124
do {
83-
let decryptedUsername = try dbEncryptedUsername.decrypt(key: key, iv: iv)
84-
let decryptedPassword = try dbEncryptedPassword.decrypt(key: key, iv: iv)
85-
print("Successfully decrypted username and password.")
86-
return (typedUsername == decryptedUsername && typedPassword == decryptedPassword)
125+
let decryptedPassword = try storedEncryptedPassword.decrypt(key: key, iv: iv)
126+
print("Successfully decrypted password.")
127+
return (typedPassword == decryptedPassword)
87128
} catch {
88-
print("Could not decrypt username and password.")
129+
print("Could not decrypt password.")
89130
return false
90131
}
91132
}
92133

134+
func dismissAlert() {
135+
showUsernameAlert = false
136+
}
137+
138+
func encryptData() {
139+
//show loading
140+
141+
// let users = UserDefaults.standard.dictionary(forKey: "users") ?? [:]
142+
// availableUsers = users.keys.map({ String(0ドル) })
143+
144+
// encrypt data, store in NS defaults
145+
do {
146+
let encryptedPassword = try typedPassword.encrypt(key: key, iv: iv)
147+
UserDefaults.standard.set(typedPassword, forKey: "username")
148+
UserDefaults.standard.set(encryptedPassword, forKey: "encryptedPassword")
149+
print("Successfully encrypted username and password.")
150+
} catch {
151+
print("Could not encrypt username and password.")
152+
// self.loadingView.isHidden = true
153+
// isLoggedIn = false
154+
}
155+
156+
DispatchQueue.main.asyncAfter(deadline: .now() + 2.5, execute: {
157+
// if doCredentialsMatch:
158+
// dismiss loading
159+
// isLoggedIn = true
160+
})
161+
}
162+
93163
func login() {
164+
isLoggedIn = true
165+
94166
if typedUsername.isEmpty {
95-
Alert(title:Text("Please enter a username"))
167+
showUsernameAlert =true
96168
} else if typedPassword.isEmpty {
97169
Alert(title: Text("Please enter a password"))
98170
} else {
99-
// encrypt data, store in NS defaults and log in
100-
101-
//show loading
102-
do {
103-
let encryptedUsername = try typedUsername.encrypt(key: key, iv: iv)
104-
let encryptedPassword = try typedPassword.encrypt(key: key, iv: iv)
105-
UserDefaults.standard.set(encryptedUsername, forKey: "encryptedUsername")
106-
UserDefaults.standard.set(encryptedPassword, forKey: "encryptedPassword")
107-
print("Successfully encrypted username and password.")
108-
} catch {
109-
print("Could not encrypt username and password.")
110-
111-
// print("Could not decrypt username and password.")
112-
// self.loadingView.isHidden = true
113-
// isLoggedIn = false
114-
}
115-
116-
DispatchQueue.main.asyncAfter(deadline: .now() + 2.5, execute: {
117-
// if doCredentialsMatch:
118-
// dismiss loading
119-
// isLoggedIn = true
120-
})
171+
encryptData()
121172
}
122173
}
123-
}
124-
125-
struct SignUpView : View {
126174

127-
var body: some View {
128-
VStack {
129-
Text("Status: You do not have any accounts").color(.blue)
130-
Spacer()
131-
Text("Create an account")
175+
func signUp() {
176+
if typedUsername.isEmpty {
177+
Alert(title: Text("Please enter a username"))
178+
} else if typedPassword.isEmpty {
179+
Alert(title: Text("Please enter a password"))
180+
} else {
181+
encryptData()
132182
}
133183
}
134-
}
135-
136-
struct LoggedInView : View {
137184

138-
var body: some View {
139-
VStack {
140-
Text("Status: You are signed in!").color(.green)
141-
Spacer()
142-
Button(action: logout) {
143-
Text("Logout")
144-
}.foregroundColor(.purple).padding(7)
145-
}.padding(25)
185+
func logout() {
186+
isLoggedIn = false // log user out
146187
}
188+
}
189+
190+
struct User: Equatable, Hashable, Codable, Identifiable {
191+
let id: UUID
192+
var name: String
147193

148-
func logout() {
149-
// log user out
194+
init(name: String) {
195+
self.id = UUID()
196+
self.name = name
150197
}
151198
}
152199

153200
#if DEBUG
154201
struct ContentView_Previews : PreviewProvider {
155202
static var previews: some View {
156-
SignInView()
157-
// LoggedInView()
203+
ContentView()
158204
}
159205
}
160206
#endif

‎LogInWithEncryption/Extensions.swift‎

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,28 @@
66
// Copyright © 2019 Manenga Mungandi. All rights reserved.
77
//
88

9-
import Foundation
109
import UIKit
11-
import LPSnackbar
10+
import SwiftUI
1211
import CryptoSwift
1312

13+
prefix operator
14+
prefix func (hex:UInt32) -> Color {
15+
return Color(hex)
16+
}
17+
18+
extension Color {
19+
init(_ hex: UInt32, opacity:Double = 1.0) {
20+
let red = Double((hex & 0xff0000) >> 16) / 255.0
21+
let green = Double((hex & 0xff00) >> 8) / 255.0
22+
let blue = Double((hex & 0xff) >> 0) / 255.0
23+
self.init(.sRGB, red: red, green: green, blue: blue, opacity: opacity)
24+
}
25+
}
26+
27+
let hexColor:(UInt32) -> (Color) = {
28+
return Color(0ドル)
29+
}
30+
1431
extension String {
1532

1633
public func encrypt(key: String, iv: String) throws -> String {
@@ -27,10 +44,6 @@ extension String {
2744

2845
extension UIViewController {
2946

30-
public func showSnack(message: String) {
31-
LPSnackbar.showSnack(title: message)
32-
}
33-
3447
public func showAlert(title: String, message: String) {
3548
let alert = UIAlertController(title: title, message: message, preferredStyle: .alert)
3649
alert.addAction(UIAlertAction(title: "Oh, okay.", style: .default, handler: nil))
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
//
2+
// ResponderableTextField.swift
3+
// TypeYourCard
4+
//
5+
// Created by Alexej Nenastev on 27.06.2019.
6+
// Copyright © 2019 Alexej Nenastev. All rights reserved.
7+
//
8+
9+
import SwiftUI
10+
11+
struct ResponderableTextField: UIViewRepresentable {
12+
13+
class Coordinator: NSObject, UITextFieldDelegate {
14+
15+
@Binding var text: String
16+
var didBecomeFirstResponder = false
17+
18+
init(text: Binding<String>) {
19+
$text = text
20+
}
21+
22+
func textFieldDidChangeSelection(_ textField: UITextField) {
23+
text = textField.text ?? ""
24+
}
25+
}
26+
27+
28+
@Binding var text: String
29+
var isFirstResponder: Bool = false
30+
var keyboardType: UIKeyboardType = .default
31+
32+
func makeUIView(context: UIViewRepresentableContext<ResponderableTextField>) -> UITextField {
33+
let textField = UITextField(frame: .zero)
34+
textField.delegate = context.coordinator
35+
textField.keyboardType = keyboardType
36+
// textField.backgroundColor = .white
37+
return textField
38+
}
39+
40+
func makeCoordinator() -> ResponderableTextField.Coordinator {
41+
return Coordinator(text: $text)
42+
}
43+
44+
func updateUIView(_ uiView: UITextField, context: UIViewRepresentableContext<ResponderableTextField>) {
45+
uiView.text = text
46+
if isFirstResponder && !context.coordinator.didBecomeFirstResponder {
47+
uiView.becomeFirstResponder()
48+
context.coordinator.didBecomeFirstResponder = true
49+
}
50+
}
51+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /