March 2020
Intermediate to advanced
392 pages
10h 10m
English
For a user to log in, our controller needs to verify the credentials and then issue two JWTs: the access token and the refresh token. Luckily, pretty much all of this is done through existing providers and middleware.
Let's add the logic for the login function:
let data = try request.content.decode(LoginInput.self)return User.query(on: request.db).filter(\.$email == data.email).all().flatMap { users in if users.count == 0 { return request.eventLoop.makeFailedFuture( Abort(.unauthorized)) } let user = users.first! var check = false do { check = try Bcrypt.verify(data.password, created: user.password) } catch {} if check { } return request.eventLoop.makeFailedFuture( Abort(.internalServerError)) ...Read now
Unlock full access