Skip to content

Commit bef32ce

Browse files
committed
added Anonymous login
1 parent 9cf7d03 commit bef32ce

File tree

3 files changed

+85
-34
lines changed

3 files changed

+85
-34
lines changed

CosyncJWT/Swift/CosyncJWTiOS.xcodeproj/project.pbxproj

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@
2323
08F58B84277A1E7B00F987ED /* LoginCompleteView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 08F58B80277A1E7B00F987ED /* LoginCompleteView.swift */; };
2424
08F58B87277A1E8700F987ED /* UserManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 08F58B85277A1E8600F987ED /* UserManager.swift */; };
2525
08F58B88277A1E8700F987ED /* RealmManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 08F58B86277A1E8600F987ED /* RealmManager.swift */; };
26-
08F58B94277A5D1D00F987ED /* Realm in Frameworks */ = {isa = PBXBuildFile; productRef = 08F58B93277A5D1D00F987ED /* Realm */; };
27-
08F58B96277A5D1D00F987ED /* RealmSwift in Frameworks */ = {isa = PBXBuildFile; productRef = 08F58B95277A5D1D00F987ED /* RealmSwift */; };
26+
52208FF8292918F5007B1126 /* Realm in Frameworks */ = {isa = PBXBuildFile; productRef = 52208FF7292918F5007B1126 /* Realm */; };
27+
52208FFA292918F5007B1126 /* RealmSwift in Frameworks */ = {isa = PBXBuildFile; productRef = 52208FF9292918F5007B1126 /* RealmSwift */; };
2828
/* End PBXBuildFile section */
2929

3030
/* Begin PBXFileReference section */
@@ -51,9 +51,9 @@
5151
isa = PBXFrameworksBuildPhase;
5252
buildActionMask = 2147483647;
5353
files = (
54+
52208FFA292918F5007B1126 /* RealmSwift in Frameworks */,
55+
52208FF8292918F5007B1126 /* Realm in Frameworks */,
5456
08CA318127873166004E0A92 /* CosyncJWTSwift in Frameworks */,
55-
08F58B96277A5D1D00F987ED /* RealmSwift in Frameworks */,
56-
08F58B94277A5D1D00F987ED /* Realm in Frameworks */,
5757
);
5858
runOnlyForDeploymentPostprocessing = 0;
5959
};
@@ -131,9 +131,9 @@
131131
);
132132
name = CosyncJWTiOS;
133133
packageProductDependencies = (
134-
08F58B93277A5D1D00F987ED /* Realm */,
135-
08F58B95277A5D1D00F987ED /* RealmSwift */,
136134
08CA318027873166004E0A92 /* CosyncJWTSwift */,
135+
52208FF7292918F5007B1126 /* Realm */,
136+
52208FF9292918F5007B1126 /* RealmSwift */,
137137
);
138138
productName = CosyncJWTiOS;
139139
productReference = 08F58B61277A1C1000F987ED /* CosyncJWTiOS.app */;
@@ -164,8 +164,8 @@
164164
);
165165
mainGroup = 08F58B58277A1C1000F987ED;
166166
packageReferences = (
167-
08F58B92277A5D1D00F987ED /* XCRemoteSwiftPackageReference "realm-cocoa" */,
168167
08CA317F27873166004E0A92 /* XCRemoteSwiftPackageReference "CosyncJWTSwift" */,
168+
52208FF6292918F5007B1126 /* XCRemoteSwiftPackageReference "realm-swift" */,
169169
);
170170
productRefGroup = 08F58B62277A1C1000F987ED /* Products */;
171171
projectDirPath = "";
@@ -415,15 +415,15 @@
415415
repositoryURL = "https://github.com/Cosync/CosyncJWTSwift.git";
416416
requirement = {
417417
kind = upToNextMajorVersion;
418-
minimumVersion = 1.0.0;
418+
minimumVersion = 1.1.4;
419419
};
420420
};
421-
08F58B92277A5D1D00F987ED /* XCRemoteSwiftPackageReference "realm-cocoa" */ = {
421+
52208FF6292918F5007B1126 /* XCRemoteSwiftPackageReference "realm-swift" */ = {
422422
isa = XCRemoteSwiftPackageReference;
423-
repositoryURL = "https://github.com/realm/realm-cocoa.git";
423+
repositoryURL = "https://github.com/realm/realm-swift.git";
424424
requirement = {
425-
branch = master;
426-
kind = branch;
425+
kind = upToNextMajorVersion;
426+
minimumVersion = 10.32.3;
427427
};
428428
};
429429
/* End XCRemoteSwiftPackageReference section */
@@ -434,14 +434,14 @@
434434
package = 08CA317F27873166004E0A92 /* XCRemoteSwiftPackageReference "CosyncJWTSwift" */;
435435
productName = CosyncJWTSwift;
436436
};
437-
08F58B93277A5D1D00F987ED /* Realm */ = {
437+
52208FF7292918F5007B1126 /* Realm */ = {
438438
isa = XCSwiftPackageProductDependency;
439-
package = 08F58B92277A5D1D00F987ED /* XCRemoteSwiftPackageReference "realm-cocoa" */;
439+
package = 52208FF6292918F5007B1126 /* XCRemoteSwiftPackageReference "realm-swift" */;
440440
productName = Realm;
441441
};
442-
08F58B95277A5D1D00F987ED /* RealmSwift */ = {
442+
52208FF9292918F5007B1126 /* RealmSwift */ = {
443443
isa = XCSwiftPackageProductDependency;
444-
package = 08F58B92277A5D1D00F987ED /* XCRemoteSwiftPackageReference "realm-cocoa" */;
444+
package = 52208FF6292918F5007B1126 /* XCRemoteSwiftPackageReference "realm-swift" */;
445445
productName = RealmSwift;
446446
};
447447
/* End XCSwiftPackageProductDependency section */

CosyncJWT/Swift/CosyncJWTiOS/LoggedOutView.swift

Lines changed: 56 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -88,24 +88,8 @@ struct LoginTab: View {
8888
}
8989

9090
Button(action: {
91-
Task {
92-
if self.email.count > 0 && self.password.count > 0 {
93-
isLoggingIn = true
94-
95-
do {
96-
try await UserManager.shared.login(email: self.email, password: self.password)
97-
if let _ = CosyncJWTRest.shared.loginToken {
98-
self.appState.target = .loginComplete
99-
} else {
100-
self.appState.target = .loggedIn
101-
}
102-
} catch {
103-
self.showLoginInvalidParameters()
104-
}
105-
106-
} else {
107-
self.showLoginInvalidParameters()
108-
}
91+
Task{
92+
await login()
10993
}
11094
}) {
11195
Text("Login")
@@ -117,6 +101,20 @@ struct LoginTab: View {
117101
.background(Color.green)
118102
.cornerRadius(8)
119103

104+
Button(action: {
105+
Task{
106+
await loginAnonymous()
107+
}
108+
109+
}) {
110+
Text("Login As Anonymous")
111+
.font(.body)
112+
}
113+
.padding()
114+
.foregroundColor(Color.white)
115+
.background(Color.green)
116+
.cornerRadius(8)
117+
120118
Button(action: {
121119
self.appState.target = .password
122120
}) {
@@ -125,13 +123,53 @@ struct LoginTab: View {
125123
}
126124
.padding()
127125

126+
127+
128+
128129

129130
}
130131
.font(.title)
131132
.alert(item: $message) { message in
132133
Alert(message)
133134
}
134135
}
136+
137+
func login() async {
138+
if self.email.count > 0 && self.password.count > 0 {
139+
isLoggingIn = true
140+
141+
do {
142+
try await UserManager.shared.login(email: self.email, password: self.password)
143+
if let _ = CosyncJWTRest.shared.loginToken {
144+
self.appState.target = .loginComplete
145+
} else {
146+
self.appState.target = .loggedIn
147+
}
148+
} catch {
149+
isLoggingIn = false
150+
self.showLoginInvalidParameters()
151+
}
152+
153+
} else {
154+
self.showLoginInvalidParameters()
155+
}
156+
}
157+
158+
func loginAnonymous() async {
159+
isLoggingIn = true
160+
161+
do {
162+
try await UserManager.shared.loginAnonymous()
163+
if let _ = CosyncJWTRest.shared.loginToken {
164+
self.appState.target = .loginComplete
165+
} else {
166+
self.appState.target = .loggedIn
167+
}
168+
} catch {
169+
isLoggingIn = false
170+
self.showLoginError(message: error.localizedDescription)
171+
}
172+
}
135173
}
136174

137175

CosyncJWT/Swift/CosyncJWTiOS/UserManager.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,19 @@ class UserManager {
7474
}
7575
}
7676

77+
@MainActor func loginAnonymous() async throws -> Void {
78+
79+
let uuid = UUID().uuidString
80+
try await CosyncJWTRest.shared.loginAnonymous("ANON_\(uuid)")
81+
82+
if CosyncJWTRest.shared.loginToken == nil,
83+
let jwt = CosyncJWTRest.shared.jwt {
84+
85+
try await RealmManager.shared.login(jwt)
86+
try await UserManager.shared.loginGetUserData()
87+
}
88+
}
89+
7790
@MainActor func loginComplete(code: String) async throws -> Void {
7891

7992
try await CosyncJWTRest.shared.loginComplete(code)

0 commit comments

Comments
 (0)