Skip to content
This repository has been archived by the owner on Apr 20, 2024. It is now read-only.

Feature: Email validation #110

Merged
merged 3 commits into from
Feb 27, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import Forms
import JWTProvider
import Validation
import Vapor

/// Class that implements the behavior for the `APIUserController` for User
Expand Down Expand Up @@ -57,6 +59,14 @@ open class APIUserControllerDelegate<U: JWTKeychainUser>:
passwordResetMailer: PasswordResetMailerType
) throws -> ResponseRepresentable {
do {
if let json = request.json {
let email: String = try json.get(User.Keys.email)

try EmailValidator()
.transformingErrors(to: EmailError.invalidEmailFormat)
.validate(email)
}

let user = try U.find(request: request)
let token = try tokenGenerators
.resetPasswordTokenGenerator
Expand All @@ -66,6 +76,8 @@ open class APIUserControllerDelegate<U: JWTKeychainUser>:
resetToken: token,
subject: "Reset Password"
)
} catch is EmailError {
return status("Invalid Email format.")
} catch let error as AbortError where error.status == .notFound {
// ignore "notFound" errors and pretend the operation succeeded
}
Expand All @@ -87,4 +99,8 @@ extension APIUserControllerDelegate {
func status(_ status: String) -> ResponseRepresentable {
return JSON(["status": .string(status)])
}

private enum EmailError: Error {
case invalidEmailFormat
}
}