Recaptcha
Library is prepared for support Google Recaptcha service to Vapor applications. First you need to obtain a secret key from: https://www.google.com/recaptcha/admin.
Most part of the code was originally created here: gotranseo/vapor-recaptcha.
Getting started
You need to add library to Package.swift
file:
- add package to dependencies:
.package(url: "https://github.com/Mikroservices/Recaptcha.git", from: "2.0.0")
- and add product to your target:
.target(name: "App", dependencies: [
.product(name: "Vapor", package: "vapor"),
.product(name: "Recaptcha", package: "Recaptcha")
])
Set the Recaptcha configuration (e.g. in main.swift
file).
import App
import Vapor
import Recaptcha
var env = try Environment.detect()
try LoggingSystem.bootstrap(from: &env)
let app = Application(env)
defer { app.shutdown() }
// Recaptcha.
app.captcha.configuration.secretKey = "SecretKey"
try configure(app)
try app.run()
Using Recaptcha client in your controller.
app.get("recaptcha") { req -> EventLoopFuture<String> in
let captchaToken = try req.query.get(String.self, at: "captchaToken")
let validationFuture = req.validate(captchaFormResponse: captchaToken)
return validationFuture.map { result -> String in
return result ? "Recaptcha ok" : "Recaptcha failed"
}
}
Developing
Download the source code and run in command line:
$ git clone https://github.com/Mikroservices/Recaptcha.git
$ swift package update
$ swift build
Run the following command if you want to open project in Xcode:
$ open Package.swift
Contributing
You can fork and clone repository. Do your changes and pull a request.