Swiftpack.co - aronbudinszky/hcaptcha-swift as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
See all packages published by aronbudinszky.
aronbudinszky/hcaptcha-swift v1.0.2
A lightweight, UI-free implementation of hCaptcha verification for use in server-side Swift applications.
⭐️ 1
🕓 1 year ago
iOS macOS
.package(url: "https://github.com/aronbudinszky/hcaptcha-swift.git", from: "v1.0.2")

HCaptchaSwift

A lightweight implementation of hCaptcha for use in server-side Swift applications. As opposed to the official HCaptcha-ios-sdk, this package does not depend on WKWebView or any other UI-layer elements and instead merely implements the backend verification part of HCaptcha.

Add the package

Only SwiftPM is supported at this time.

To add the package, simply select File / Add Packages... in Xcode and paste this URL into the search / url box: https://github.com/aronbudinszky/hcaptcha-swift .

Usage

  • Note: this is intended for server-side Swift use. If you are building an iOS or macOS app just use the official HCaptcha-ios-sdk.

Step 1

You should display the HCaptcha widget on your site via the standard documented methods and/or one of the frontend wrappers/plugins.

This will generate a token client side which you can then submit and verify server-side.

Step 2

Once you've generated the client-side widget, use this package to easily verify the token it generated via any chosen server-side Swift solution (for example Vapor) like so:

        // Create the client
        let hCaptchaClient = HCaptchaSwift.Client(secret: "- your secret token -")

        // Now try to verify
        let response = try await hCaptchaClient.verify(
            "- verification token you received when the user completed the captcha -",
            remoteIp: "- user's remote ip (optional) -",
            siteKey: "- your site key (optional, but recommended)"
        )
        // - Note: In case of any errors (including when verification fails) the returned hCaptcha error codes will be thrown (see docs). But generally non-spam users won't be sending invalid verification tokens anyway (unless your implementation is weird / wrong), so regular users won't see these errors.

        guard response else {
            print("Verification failed.")
            // This usually won't happen, but good to check just in case. Typically an error should already be thrown above if this is false. (Unfortunately hCaptcha docs are a bit unclear on this.)
            return
        }
        
        print("Yay! You are not a bot!")

GitHub

link
Stars: 1
Last commit: 1 year ago
Advertisement: IndiePitcher.com - Cold Email Software for Startups

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics