Compilation error with ModelTokenAuthenticatable conformance
Describe the issue
I'm getting a compilation error after upgrading to Swift 6.0.1 and Vapor 4.106
Vapor version
4.106.0
Operating system and version
macOS 15.1
Swift version
Swift 6.0.1
Steps to reproduce
Here's the model for my Token entity.
final class Token: Model, @unchecked Sendable {
init() { }
init(
id: UUID? = nil,
userID: UserModel.IDValue,
token: String,
expiresAt: Date?)
{
self.id = id
$user.id = userID
value = token
self.expiresAt = expiresAt
}
// MARK: Internal
static let schema = "tokens"
@ID var id: UUID?
@Parent(key: "user_id")
var user: UserModel
@Field(key: "value")
var value: String
@Field(key: "expires_at")
var expiresAt: Date?
@Timestamp(key: "created_at", on: .create)
var createdAt: Date?
}
// Error: Type 'Token' does not conform to protocol 'ModelTokenAuthenticatable'
extension Token: ModelTokenAuthenticatable {
// Candidate has non-matching type 'any KeyPath<Token, FieldProperty<Token, String>> & Sendable' [with User = <<error type>>]
static let valueKey = \Token.$value
static let userKey = \Token.$user
var isValid: Bool {
guard let expiryDate = expiresAt else {
return true
}
return expiryDate > Date()
}
}
Outcome
Errors inline
Additional notes
No response
@matthewcheok does it work if you change the key paths to
nonisolated(unsafe) static let valueKey = \Token.$value
nonisolated(unsafe) static let userKey = \Token.$user
Unfortunately that does not work.
I'm assuming UserModel is a sendable model as well? What does the fixit suggest?
Yes it is sendable. It simply suggests removing the nonisolated(unsafe) attribute.
On Sat, Oct 5, 2024 at 7:34 PM Tim Condon @.***> wrote:
I'm assuming UserModel is a sendable model as well? What does the fixit suggest?
— Reply to this email directly, view it on GitHub https://github.com/vapor/fluent/issues/780#issuecomment-2395226296, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABCR6LCKVIBIG2HM2P25MPTZ2BZO3AVCNFSM6AAAAABPNSTHZCVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDGOJVGIZDMMRZGY . You are receiving this because you were mentioned.Message ID: @.***>
This does look like an issue with ModelTokenAuthenticatable - I'll see if I can come up with a fix
any updates here?
If I declare these as computed properties, it seems to compile:
static var usernameKey: KeyPath<User, Field<String>> { \.$email }
static var passwordHashKey: KeyPath<User, Field<String>> { \.$password }
@tonyarnold thanks for this!
@matthewcheok sorry for the delay, the solution Tony posted using computed properties is the correct one. You can see an example at https://github.com/0xTim/VaporAuthExample/blob/main/Sources/App/Models/Token.swift#L7-L8