vapor / auth

👤 Authentication and Authorization framework for Fluent.
53 stars 34 forks source link

BasicAuthenticatable for non-final classes #72

Closed reni99 closed 4 years ago

reni99 commented 4 years ago

How would we conform the class User to BasicAuthenticatable in the following case:

import Vapor
import FluentPostgreSQL
import Authentication

class User: Codable {
    var id: Int?

    var username: String
    var email: String
    var passwordHash: String

    init(username: String, email: String, passwordHash: String) {
        self.username = username
        self.email = email
        self.passwordHash = passwordHash
    }
}

final class Student: User {

    struct Public {
        let id: Int
        let email: String
        let username: String
        let nationality: String?
    }

    let nationality: String?

    enum CodingKeys: CodingKey {
        case nationality
    }

    init(username: String, email: String, passwordHash: String, nationality: String?) {
        self.nationality = nationality
        super.init(username: username, email: email, passwordHash: passwordHash)
    }

    required init(from decoder: Decoder) throws {
        let container = try decoder.container(keyedBy: Student.CodingKeys.self)
        self.nationality = try container.decode(String.self, forKey: .nationality)
        try super.init(from: decoder)
    }

    override func encode(to encoder: Encoder) throws {
        try super.encode(to: encoder)
        var container = encoder.container(keyedBy: Student.CodingKeys.self)
        try container.encode(nationality, forKey: .nationality)
    }
}

extension Student.Public: Content {}
extension Student: PostgreSQLModel {}
extension Student: Content {}
extension Student: Parameter {}
extension Student: Migration {}

If I do the following:

extension User: BasicAuthenticatable {
    static let usernameKey: UsernameKey = \User.username
    static let passwordKey: PasswordKey = \User.passwordHash
}

I get the error, that BasicAuthenticatable can only be implemented by final-classes. How should I work around this? Thx in advance :)

jonny7 commented 4 years ago

This is probably more suited for the discord, https://discord.gg/BnXmVGA 😄

0xTim commented 4 years ago

@reni99 to follow up - because of Vapor's heavy use of protocols a lot of things don't work with inheritance, so most models have to be final classes