【问题标题】:Vapor 4 User Authentication and AuthorisationVapor 4 用户认证和授权
【发布时间】:2020-05-29 08:56:54
【问题描述】:

我最近开始使用 vapor4(没有使用任何旧版本),我正在尝试弄清楚如何实现用户授权和身份验证。虽然我了解基本概念,但在使用 Laravel 之前我仍然无法弄清楚在蒸汽中做什么。

我扩展了我的用户。我没有 pw 散列,这是为了测试和基本理解。我们暂时忽略它。

extension User: ModelAuthenticatable
{
    static let usernameKey = \User.$name
    static let passwordHashKey = \User.$password

    func verify(password: String) throws -> Bool {
        return password == self.password
    }
}

问题是我找不到如何使用此身份验证的教程。我只是尝试一些东西让它工作,但没有成功。这是在我的路线文件中。

let auth = app.grouped(User.authenticator())        
auth.get("sign-in") { req in
    "I'm authenticated"
}

我的第一个目标只是在尝试这条路线时收到成功或失败的答案。最终我想切换到基于令牌的解决方案,但一步一步。


我读到的内容是:https://docs.vapor.codes/4.0/authentication/https://theswiftdev.com/all-about-authentication-in-vapor-4/。无论如何,我无法弄清楚如何使用所描述的身份验证器。

【问题讨论】:

    标签: swift authentication basic-authentication vapor


    【解决方案1】:

    在写这篇文章时,我终于想通了。无论如何,对于绊倒这一点的人来说。就这么简单:

    let auth = app.grouped(User.authenticator(), User.guardMiddleware())
    auth.get("sign-in") { req in
        "I'm authenticated"
    }
    

    您的用户类默认提供guardMiddleware。您不必实现其他任何东西,只需在您的路线中使用它即可。

    【讨论】:

      猜你喜欢
      • 2017-04-11
      • 1970-01-01
      • 2018-07-25
      • 2010-11-14
      • 2021-11-08
      • 2013-08-08
      • 2017-09-11
      • 2023-03-23
      相关资源
      最近更新 更多