2

我是 Vapor 的新手,我已经实现了注册和登录路由。注册工作正常。每次我调用登录路由时,它似乎都有问题。每次,我尝试使用基本身份验证以注册用户身份登录,它只返回 401。在下面附上我的代码。

应用用户模型:

extension AppUser: ModelAuthenticatable {
    static let usernameKey = \AppUser.$email
    static let passwordHashKey = \AppUser.$passwordHash
    func verify(password: String) throws -> Bool {
        try Bcrypt.verify(password, created: self.passwordHash)
    } 
}
extension AppUser: JWTPayload {
    func verify(using signer: JWTSigner) throws {
    } 
}

路线配置:

   //MARK: Unprotected API
    let unprotectedApi = app.routes
    try unprotectedApi.register(collection: AppUserController.Unprotected())
    //MARK: Password Protected API
    let passwordProtectedApi = unprotectedApi.grouped(AppUser.authenticator())
    try passwordProtectedApi.register(collection: AppUserController.PasswordProtected())

登录逻辑:

    extension AppUserController.PasswordProtected: RouteCollection {
    func login(req: Request) throws -> EventLoopFuture<Response> {
        let user = try req.auth.require(AppUser.self)
        let token = try req.jwt.sign(user)
        let loginResponse = AppUserLoginResponse(user: user.response, accessToken: token)
        return DataWrapper.encodeResponse(data: loginResponse, for: req)
    }
    func boot(routes: RoutesBuilder) throws {
        routes.post(Endpoint.API.Users.login, use: login)
    }
}
4

1 回答 1

2

您的login路线现在返回 401,因为您已将其包含在受保护的组中,这要求用户已经登录。它通常是不受保护的。您需要一些代码来进行登录。此函数假定用户由电子邮件地址标识并以某种方式提供了密码:

private func loginExample( email: String, password: String, on: req Request) -> EventLoopFuture<Bool> {
    return AppUser.query(on: req).filter(\.$email == email).first().flatMap { user in
        // user will be nil if not found, following line test for this
        if let user = user {
            // user was identified by email
            if try! user.verify(password: password) {
                // password matches what is stored
                request.auth.login(user)
                // login has succeeded
                return true
            }
        }
        // login has failed - because either email did not match a user or the password was incorrect
        return false
    }
}

我通过强制try调用验证(避免 do-catch 等)来保持简单。您需要在您的登录路径中使用类似此代码的代码,可能从 HTML 表单中解码电子邮件和密码。

于 2021-08-07T11:43:33.053 回答