-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: user info from dict to struct type #28
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -79,3 +79,23 @@ public extension Web3AuthNetwork { | |
return convertWeb3AuthNetworkToTorusNetWork(network: self) | ||
} | ||
} | ||
|
||
|
||
func dictionaryToJsonData(_ dictionary: [String: Any]) -> Data? { | ||
return try? JSONSerialization.data(withJSONObject: dictionary, options: []) | ||
} | ||
|
||
func convertUserDataType(dictionary: [String: Any]) throws -> UserInfo? { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should not return an optional: |
||
if let jsonData = dictionaryToJsonData(dictionary) { | ||
do { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This do {} catch {} can be removed. i.e
|
||
let decoder = JSONDecoder() | ||
let userInfo = try decoder.decode(UserInfo.self, from: jsonData) | ||
return userInfo | ||
} catch { | ||
print("Error decoding JSON: \(error)") | ||
throw "Invalid UserInfo Data structure"; | ||
} | ||
} else { | ||
throw "Invalid UserInfo Data structure"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Needs to be of the form:
Even better is it doesn't accept a message, then it fits nicely in the error handling:
|
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -176,3 +176,94 @@ public struct enableMFARecoveryFactor { | |
self.additionalMetadata = additionalMetadata | ||
} | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Many of the structs declared here will be in the next version of customauth. I'd suggest we wait with this PR for that and then update the dependency instead and use it from there. |
||
public enum LoginType: String, Codable { | ||
case google = "google" | ||
case facebook = "facebook" | ||
case reddit = "reddit" | ||
case discord = "discord" | ||
case twitch = "twitch" | ||
case apple = "apple" | ||
case line = "line" | ||
case github = "github" | ||
case kakao = "kakao" | ||
case linkedin = "linkedin" | ||
case twitter = "twitter" | ||
case weibo = "weibo" | ||
case wechat = "wechat" | ||
case farcaster = "farcaster" | ||
case emailPasswordless = "email_passwordless" | ||
case smsPasswordless = "sms_passwordless" | ||
case webauthn = "webauthn" | ||
case jwt = "jwt" | ||
} | ||
|
||
public struct WebAuthnExtraParams: Codable { | ||
public let authenticatorAttachment: String? | ||
public let attestation: String? | ||
public let userVerification: String? | ||
} | ||
|
||
public struct TorusGenericObject: Codable { | ||
public let type: String? | ||
public let verifier: String? | ||
public let verifierIdField: String? | ||
} | ||
|
||
public struct TorusVerifierResponse: Codable { | ||
public let email: String | ||
public let name: String | ||
public let profileImage: String | ||
public let aggregateVerifier: String? | ||
public let verifier: String | ||
public let verifierId: String | ||
public let typeOfLogin: LoginType | ||
public let ref: String? | ||
public let registerOnly: Bool? | ||
public let extraVerifierParams: WebAuthnExtraParams? | ||
} | ||
|
||
public struct LoginWindowResponse: Codable { | ||
public let accessToken: String | ||
public let idToken: String? | ||
public let ref: String? | ||
public let extraParams: String? | ||
public let extraParamsPassed: String? | ||
public let state: TorusGenericObject | ||
} | ||
|
||
public struct UserInfo: Codable { | ||
public let email: String? | ||
public let name: String? | ||
public let profileImage: String? | ||
public let aggregateVerifier: String? | ||
public let verifier: String | ||
public let verifierId: String | ||
public let typeOfLogin: LoginType? | ||
public let ref: String? | ||
public let registerOnly: Bool? | ||
public let extraVerifierParams: WebAuthnExtraParams? | ||
public let accessToken: String? | ||
public let idToken: String? | ||
public let extraParams: String? | ||
public let extraParamsPassed: String? | ||
public let state: TorusGenericObject? | ||
} | ||
|
||
enum UserInfoConversionError: Error, LocalizedError { | ||
case missingData | ||
case invalidData | ||
case decodingError(Error) | ||
|
||
var errorDescription: String? { | ||
switch self { | ||
case .missingData: | ||
return "UserInfo dictionary is missing." | ||
case .invalidData: | ||
return "UserInfo dictionary is invalid." | ||
case .decodingError(let error): | ||
return "Error decoding UserInfo: \(error.localizedDescription)" | ||
} | ||
} | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -140,11 +140,25 @@ public struct MpcCoreKit { | |
return try await self.login(userData: TorusKeyData(torusKey: torusKey, userInfo: modUserInfo)) | ||
} | ||
|
||
public func getUserInfo() throws -> [String: Any] { | ||
guard let userInfo = self.userInfo else { | ||
throw ("user is not logged in.") | ||
public func getUserInfo() throws -> UserInfo { | ||
do { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The do {} catch {} can be removed here. |
||
guard let userInfoDict = self.userInfo else { | ||
throw UserInfoConversionError.missingData | ||
} | ||
|
||
guard let userInfoStruct = try convertUserDataType(dictionary: userInfoDict) else { | ||
throw UserInfoConversionError.missingData | ||
} | ||
return userInfoStruct | ||
} catch UserInfoConversionError.missingData { | ||
throw("Error: UserInfo dictionary is missing.") | ||
} catch UserInfoConversionError.invalidData { | ||
throw("Error: UserInfo dictionary is invalid.") | ||
} catch UserInfoConversionError.decodingError(let error) { | ||
throw("Error decoding UserInfo: \(error)") | ||
} catch { | ||
throw("An unexpected error occurred: \(error)") | ||
} | ||
return userInfo | ||
} | ||
|
||
public func getKeyDetails() async throws -> MpcKeyDetails { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.