Hi
I'm trying to make a middleware that checks if a request contains a valid apple jwt token + check if a user already exists with User.appleId == token.subject.value
and create a new user if no user was found + make the token.subject.value
available in my routes.
struct AppleSubjectKey: StorageKey {
typealias Value = String
}
extension Request {
var appleSubject: String? {
get {
self.storage[AppleSubjectKey.self]
}
set {
self.storage[AppleSubjectKey.self] = newValue
}
}
}
struct SignInWithAppleMiddleware: AsyncMiddleware {
func respond(to request: Request, chainingTo next: AsyncResponder) async throws -> Response {
guard let token = try? await request.jwt.apple.verify() else {
throw Abort(.unauthorized)
}
// check if user with appleId == token.subject.value
// create new user if no user was found
request.appleSubject = token.subject.value
return try await next.respond(to: request)
}
}
but while working on this middleware I read somewhere that using this approach the app would risk crashing if the server receive a lot of requests - is this true or would I be fine with this solution?