From b74ca5900f5dc42cba943f88d619b505f1c184fb Mon Sep 17 00:00:00 2001 From: rishabhpoddar Date: Tue, 12 Mar 2024 13:31:14 +0530 Subject: [PATCH] fixes ios snippets --- v2/mfa/frontend-setup.mdx | 14 +++++++++++--- v2/mfa/protect-routes.mdx | 17 ++++++++++++----- v2/mfa/step-up-auth.mdx | 11 +++++++---- 3 files changed, 30 insertions(+), 12 deletions(-) diff --git a/v2/mfa/frontend-setup.mdx b/v2/mfa/frontend-setup.mdx index b00b3d6b3..da273ba9f 100644 --- a/v2/mfa/frontend-setup.mdx +++ b/v2/mfa/frontend-setup.mdx @@ -457,10 +457,18 @@ import UIKit import SuperTokensIOS fileprivate class ViewController: UIViewController { - func isAllMFACompleted() { - if let accessTokenPayload: [String: Any] = try? SuperTokens.getAccessTokenPayloadSecurely(), let mfaObject: [String: Any] = accessTokenPayload["st-mfa"] as? [String: Any], let isMFACompleted: Boolean = mfaObject["v"] as? Boolean { - return isMFACompleted; + func isAllMFACompleted() -> Bool { + // Attempt to retrieve the access token payload securely + if let accessTokenPayload: [String: Any] = try? SuperTokens.getAccessTokenPayloadSecurely(), + // Extract the mfaObject from the accessTokenPayload + let mfaObject: [String: Any] = accessTokenPayload["st-mfa"] as? [String: Any], + // Determine if MFA has been completed + let isMFACompleted: Bool = mfaObject["v"] as? Bool { + // Return the MFA completion status + return isMFACompleted } + // Return false if any of the unwrapping fails, indicating MFA completion status cannot be confirmed + return false } } ``` diff --git a/v2/mfa/protect-routes.mdx b/v2/mfa/protect-routes.mdx index 464a4c312..565704585 100644 --- a/v2/mfa/protect-routes.mdx +++ b/v2/mfa/protect-routes.mdx @@ -1052,11 +1052,18 @@ import SuperTokensIOS fileprivate class ViewController: UIViewController { func checkIfMFAIsCompleted() { - if let accessTokenPayload: [String: Any] = try? SuperTokens.getAccessTokenPayloadSecurely(), let mfaObject: [String: Any] = mfaObject["st-mfa"] as? [String: Any], let isMFACompleted: Bool = mfaObject["v"] as? Bool { - if isMFACompleted { - // All required factors for MFA have been completed - } else { - // You can check the `c` object from ["st-mfa"] prop to see which factors have been completed by the user + // Attempt to retrieve the access token payload securely + if let accessTokenPayload: [String: Any] = try? SuperTokens.getAccessTokenPayloadSecurely() { + // Extract the mfaObject from the accessTokenPayload + if let mfaObject: [String: Any] = accessTokenPayload["st-mfa"] as? [String: Any] { + // Determine if MFA has been completed + if let isMFACompleted: Bool = mfaObject["v"] as? Bool { + if isMFACompleted { + // All required factors for MFA have been completed + } else { + // You can check the `c` object from ["st-mfa"] prop to see which factors have been completed by the user + } + } } } } diff --git a/v2/mfa/step-up-auth.mdx b/v2/mfa/step-up-auth.mdx index cdc35d267..f7bd28d51 100644 --- a/v2/mfa/step-up-auth.mdx +++ b/v2/mfa/step-up-auth.mdx @@ -810,12 +810,15 @@ import SuperTokensIOS fileprivate class ViewController: UIViewController { func checkIfMFAIsCompleted() { - if let accessTokenPayload: [String: Any] = try? SuperTokens.getAccessTokenPayloadSecurely(), let mfaObject: [String: Any] = mfaObject["st-mfa"] as? [String: Any], let isMFACompleted: Bool = mfaObject["v"] as? Bool { + if let accessTokenPayload: [String: Any] = try? SuperTokens.getAccessTokenPayloadSecurely(), let mfaObject = accessTokenPayload["st-mfa"] as? [String: Any], let isMFACompleted = mfaObject["v"] as? Bool { + // Corrected the extraction of mfaObject from the accessTokenPayload if isMFACompleted { // All required factors for MFA have been completed - let mfaCompletedFactors: [String: Any] = mfaObject["c"] as? [String: Any]; - if mfaCompletedFactors["totp"] == nil || mfaCompletedFactors["totp"] < (Date().timeIntervalSince1970 - 1000*60*5) { - // user has not finished TOTP MFA in the last 5 minutes + if let mfaCompletedFactors = mfaObject["c"] as? [String: Any], let totpTime = mfaCompletedFactors["totp"] as? Double { + // Corrected unwrapping of mfaCompletedFactors and casting of totpTime + if totpTime < (Date().timeIntervalSince1970 - 1000*60*5) { + // user has not finished TOTP MFA in the last 5 minutes + } } } else { // You can check the `c` object from ["st-mfa"] prop to see which factors have been completed by the user