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

Commit

Permalink
Merge pull request #110 from nodes-vapor/feature/email-validation
Browse files Browse the repository at this point in the history
Feature: Email validation
  • Loading branch information
martinlasek authored Feb 27, 2018
2 parents 72d7773 + 832234a commit 17b5974
Showing 1 changed file with 16 additions and 0 deletions.
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
}
}

0 comments on commit 17b5974

Please sign in to comment.