1

我正在为 Web 服务使用服务器端 Swift 完美框架。用于提供静态/动态内容的 Mustache 模块。

我想在成功身份验证重定向到主页后实现登录功能。“我到处搜索,但没有找到任何重定向到 url 的功能”

这是我用于实现登录的代码-

func signin(request:HTTPRequest, response: HTTPResponse) {
    do {
        var errorMessage:String;
        var values = MustacheEvaluationContext.MapType()
        let email:String = request.param(name: "email")!
        let password:String = request.param(name: "password")!
        print("Email -> \(email) & Password -> \(password)")
        //After Authentication
        //Yay I want to go back to home page.
        mustacheRequest(request: request, response: response, handler: MustacheHelper(values: values), templatePath: webroot + "/index.html")
        // Sadly this doesn't work, it just renders the homepage without changing the url or 'without redirecting'
        response.completed()
} catch {
        print(error)
        logError(error.localizedDescription)
        response.setBody(string: "An error occured \(error)").completed()
    }
}

我认为PerfectlySoft 公司fogot 把这个功能。也许我应该报告它。任何人都知道我的问题可能是什么解决方案?请告诉。谢谢。

4

1 回答 1

2

最后我自己想出了解决方案。PerfectHTTP 或 PerfectHTTPServer 模块本身不包含 url 重定向的此功能。您必须通过 PerfectlySoft导入另一个模块 -> Perfect-OAuth2 。'redirect' 方法直接在 HTTPResponse 扩展下声明。或者你可以通过像这样添加自己的来做到这一点,

extension HTTPResponse {
    public func redirect(path: String) {
        self.status = .found
        self.setHeader(.location, value: path)
        self.completed()
    }
}

我希望这有帮助!

于 2018-09-01T10:29:53.077 回答