Skip to content

Commit

Permalink
error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
rahafjrw committed Dec 3, 2024
1 parent 98ae917 commit 78ef4d4
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 12 deletions.
16 changes: 10 additions & 6 deletions Sources/App/Controllers/Manage/DeleteAccountController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,15 @@ import SotoCognitoIdentity
enum DeleteAccountController {
@Sendable
static func deleteAccount(req: Request) async throws -> Response {
let request = try CognitoIdentityProvider.DeleteUserRequest(accessToken: req.auth.require(AuthenticatedUser.self).sessionID)
try await req.application.cognito.authenticatable.configuration.cognitoIDP.deleteUser(request)
req.auth.logout(AuthenticatedUser.self)
req.session.unauthenticate(AuthenticatedUser.self)
req.session.destroy()
return req.redirect(to: SiteURL.home.relativeURL())
do {
let request = try CognitoIdentityProvider.DeleteUserRequest(accessToken: req.auth.require(AuthenticatedUser.self).sessionID)
try await req.application.cognito.authenticatable.configuration.cognitoIDP.deleteUser(request)
req.auth.logout(AuthenticatedUser.self)
req.session.unauthenticate(AuthenticatedUser.self)
req.session.destroy()
return req.redirect(to: SiteURL.home.relativeURL())
} catch {
return Portal.View(path: SiteURL.portal.relativeURL(), model: Portal.Model(errorMessage: "An unknown error occurred: \(error.localizedDescription)")).document().encodeResponse(status: .internalServerError)

Check failure on line 20 in Sources/App/Controllers/Manage/DeleteAccountController.swift

View workflow job for this annotation

GitHub Actions / Test

extra argument 'model' in call

Check failure on line 20 in Sources/App/Controllers/Manage/DeleteAccountController.swift

View workflow job for this annotation

GitHub Actions / Test

type 'Portal' has no member 'Model'

Check failure on line 20 in Sources/App/Controllers/Manage/DeleteAccountController.swift

View workflow job for this annotation

GitHub Actions / Test

extra argument 'model' in call

Check failure on line 20 in Sources/App/Controllers/Manage/DeleteAccountController.swift

View workflow job for this annotation

GitHub Actions / Test

type 'Portal' has no member 'Model'

Check failure on line 20 in Sources/App/Controllers/Manage/DeleteAccountController.swift

View workflow job for this annotation

GitHub Actions / Test

extra argument 'model' in call

Check failure on line 20 in Sources/App/Controllers/Manage/DeleteAccountController.swift

View workflow job for this annotation

GitHub Actions / Test

type 'Portal' has no member 'Model'

Check failure on line 20 in Sources/App/Controllers/Manage/DeleteAccountController.swift

View workflow job for this annotation

GitHub Actions / Test

extra argument 'model' in call

Check failure on line 20 in Sources/App/Controllers/Manage/DeleteAccountController.swift

View workflow job for this annotation

GitHub Actions / Test

type 'Portal' has no member 'Model'

Check failure on line 20 in Sources/App/Controllers/Manage/DeleteAccountController.swift

View workflow job for this annotation

GitHub Actions / Test

extra argument 'model' in call

Check failure on line 20 in Sources/App/Controllers/Manage/DeleteAccountController.swift

View workflow job for this annotation

GitHub Actions / Test

type 'Portal' has no member 'Model'
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ enum ForgotPasswordController {
try await req.application.cognito.authenticatable.forgotPassword(username: user.email)
return Reset.View(path: SiteURL.resetPassword.relativeURL(), model: Reset.Model(email: user.email)).document()
} catch {
return ForgotPassword.View(path: req.url.path, model: ForgotPassword.Model(errorMessage: "There was an error. Please try again.")).document()
return ForgotPassword.View(path: req.url.path, model: ForgotPassword.Model(errorMessage: "An error occurred: \(error.localizedDescription)")).document()
}
}
}
4 changes: 2 additions & 2 deletions Sources/App/Controllers/Manage/LoginController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,10 @@ enum LoginController {
return Login.View(path: req.url.path, model: model).document().encodeResponse(status: .unauthorized)
} catch let error as AWSClientError {
try await awsClient.shutdown()
return Login.View(path: SiteURL.signup.relativeURL(), model: Login.Model(errorMessage: "An AWS client error occurred: \(error.errorCode)")).document().encodeResponse(status: .unauthorized)
return Login.View(path: SiteURL.login.relativeURL(), model: Login.Model(errorMessage: "An AWS client error occurred: \(error.errorCode)")).document().encodeResponse(status: .unauthorized)
} catch {
try await awsClient.shutdown()
return Login.View(path: SiteURL.signup.relativeURL(), model: Login.Model(errorMessage: "An unknown error occurred: \(error.localizedDescription)")).document().encodeResponse(status: .unauthorized)
return Login.View(path: SiteURL.login.relativeURL(), model: Login.Model(errorMessage: "An unknown error occurred: \(error.localizedDescription)")).document().encodeResponse(status: .unauthorized)
}

}
Expand Down
2 changes: 1 addition & 1 deletion Sources/App/Controllers/Manage/ResetController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ enum ResetController {
let model = Reset.Model(errorMessage: error.message ?? "There was an error.")
return Reset.View(path: req.url.path, model: model).document()
} catch {
let model = Reset.Model(errorMessage: "An unknown error occurred.")
let model = Reset.Model(errorMessage: "An unknown error occurred: \(error.localizedDescription)")
return Reset.View(path: req.url.path, model: model).document()
}
}
Expand Down
14 changes: 13 additions & 1 deletion Sources/App/Controllers/Manage/SignupController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,30 @@ enum SignupController {
@Sendable
static func signup(req: Request) async throws -> HTML {
@Dependency(\.cognito) var cognito
let awsClient = AWSClient(httpClientProvider: .shared(req.application.http.client.shared))
let awsCognitoConfiguration = CognitoConfiguration(
userPoolId: Environment.get("POOL_ID")!,
clientId: Environment.get("CLIENT_ID")!,
clientSecret: Environment.get("CLIENT_SECRET")!,
cognitoIDP: CognitoIdentityProvider(client: awsClient, region: .useast2),
adminClient: true
)
req.application.cognito.authenticatable = CognitoAuthenticatable(configuration: awsCognitoConfiguration)
struct UserCreds: Content {
var email: String
var password: String
}
do {
let user = try req.content.decode(UserCreds.self)
try await cognito.signup(req: req, username: user.email, password: user.password)
try await awsClient.shutdown()
return Verify.View(path: SiteURL.verify.relativeURL(), model: Verify.Model(email: user.email)).document()
} catch let error as AWSErrorType {
let model = Signup.Model(errorMessage: error.message ?? "There was an error.")
try await awsClient.shutdown()
return Signup.View(path: req.url.path, model: model).document()
} catch {
} catch {
try await awsClient.shutdown()
return Signup.View(path: SiteURL.signup.relativeURL(), model: Signup.Model(errorMessage: "An unknown error occurred: \(error.localizedDescription)")).document()
}

Expand Down
2 changes: 1 addition & 1 deletion Sources/App/Controllers/Manage/VerifyController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ enum VerifyController {
let model = Verify.Model(email: info.email, errorMessage: error.message ?? "There was an error.")
return Verify.View(path: req.url.path, model: model).document()
} catch {
let model = Verify.Model(email: info.email, errorMessage: "An unknown error occurred.")
let model = Verify.Model(email: info.email, errorMessage: "An unknown error occurred: \(error.localizedDescription)")
return Verify.View(path: req.url.path, model: model).document()
}
}
Expand Down

0 comments on commit 78ef4d4

Please sign in to comment.