-
-
Notifications
You must be signed in to change notification settings - Fork 172
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
asyncCredentialsAuthenticator
to `ModelCredentialsAuthenticatab…
…le` (#744) * Extend ModelCredentialsAuthenticatable with asyncCredentialsAuthenticator #743 * compiler guards for test
- Loading branch information
Showing
2 changed files
with
93 additions
and
0 deletions.
There are no files selected for viewing
33 changes: 33 additions & 0 deletions
33
Sources/Fluent/Concurrency/ModelCredentialsAuthenticatable+Concurrency.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
#if compiler(>=5.5) && canImport(_Concurrency) | ||
import NIOCore | ||
import Vapor | ||
|
||
@available(macOS 12, iOS 15, watchOS 8, tvOS 15, *) | ||
extension ModelCredentialsAuthenticatable { | ||
public static func asyncCredentialsAuthenticator( | ||
_ database: DatabaseID? = nil | ||
) -> AsyncAuthenticator { | ||
AsyncModelCredentialsAuthenticator<Self>(database: database) | ||
} | ||
} | ||
|
||
@available(macOS 12, iOS 15, watchOS 8, tvOS 15, *) | ||
private struct AsyncModelCredentialsAuthenticator<User>: AsyncCredentialsAuthenticator | ||
where User: ModelCredentialsAuthenticatable | ||
{ | ||
typealias Credentials = ModelCredentials | ||
|
||
public let database: DatabaseID? | ||
|
||
func authenticate(credentials: ModelCredentials, for request: Request) async throws { | ||
if let user = try await User.query(on: request.db(self.database)).filter(\._$username == credentials.username).first() { | ||
guard try user.verify(password: credentials.password) else { | ||
return | ||
} | ||
request.auth.login(user) | ||
} | ||
} | ||
} | ||
|
||
#endif | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters