Skip to content

Commit 78ef4d4

Browse files
committed
error handling
1 parent 98ae917 commit 78ef4d4

File tree

6 files changed

+28
-12
lines changed

6 files changed

+28
-12
lines changed

Sources/App/Controllers/Manage/DeleteAccountController.swift

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,15 @@ import SotoCognitoIdentity
99
enum DeleteAccountController {
1010
@Sendable
1111
static func deleteAccount(req: Request) async throws -> Response {
12-
let request = try CognitoIdentityProvider.DeleteUserRequest(accessToken: req.auth.require(AuthenticatedUser.self).sessionID)
13-
try await req.application.cognito.authenticatable.configuration.cognitoIDP.deleteUser(request)
14-
req.auth.logout(AuthenticatedUser.self)
15-
req.session.unauthenticate(AuthenticatedUser.self)
16-
req.session.destroy()
17-
return req.redirect(to: SiteURL.home.relativeURL())
12+
do {
13+
let request = try CognitoIdentityProvider.DeleteUserRequest(accessToken: req.auth.require(AuthenticatedUser.self).sessionID)
14+
try await req.application.cognito.authenticatable.configuration.cognitoIDP.deleteUser(request)
15+
req.auth.logout(AuthenticatedUser.self)
16+
req.session.unauthenticate(AuthenticatedUser.self)
17+
req.session.destroy()
18+
return req.redirect(to: SiteURL.home.relativeURL())
19+
} catch {
20+
return Portal.View(path: SiteURL.portal.relativeURL(), model: Portal.Model(errorMessage: "An unknown error occurred: \(error.localizedDescription)")).document().encodeResponse(status: .internalServerError)
21+
}
1822
}
1923
}

Sources/App/Controllers/Manage/ForgotPasswordController.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ enum ForgotPasswordController {
2121
try await req.application.cognito.authenticatable.forgotPassword(username: user.email)
2222
return Reset.View(path: SiteURL.resetPassword.relativeURL(), model: Reset.Model(email: user.email)).document()
2323
} catch {
24-
return ForgotPassword.View(path: req.url.path, model: ForgotPassword.Model(errorMessage: "There was an error. Please try again.")).document()
24+
return ForgotPassword.View(path: req.url.path, model: ForgotPassword.Model(errorMessage: "An error occurred: \(error.localizedDescription)")).document()
2525
}
2626
}
2727
}

Sources/App/Controllers/Manage/LoginController.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,10 @@ enum LoginController {
4848
return Login.View(path: req.url.path, model: model).document().encodeResponse(status: .unauthorized)
4949
} catch let error as AWSClientError {
5050
try await awsClient.shutdown()
51-
return Login.View(path: SiteURL.signup.relativeURL(), model: Login.Model(errorMessage: "An AWS client error occurred: \(error.errorCode)")).document().encodeResponse(status: .unauthorized)
51+
return Login.View(path: SiteURL.login.relativeURL(), model: Login.Model(errorMessage: "An AWS client error occurred: \(error.errorCode)")).document().encodeResponse(status: .unauthorized)
5252
} catch {
5353
try await awsClient.shutdown()
54-
return Login.View(path: SiteURL.signup.relativeURL(), model: Login.Model(errorMessage: "An unknown error occurred: \(error.localizedDescription)")).document().encodeResponse(status: .unauthorized)
54+
return Login.View(path: SiteURL.login.relativeURL(), model: Login.Model(errorMessage: "An unknown error occurred: \(error.localizedDescription)")).document().encodeResponse(status: .unauthorized)
5555
}
5656

5757
}

Sources/App/Controllers/Manage/ResetController.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ enum ResetController {
2727
let model = Reset.Model(errorMessage: error.message ?? "There was an error.")
2828
return Reset.View(path: req.url.path, model: model).document()
2929
} catch {
30-
let model = Reset.Model(errorMessage: "An unknown error occurred.")
30+
let model = Reset.Model(errorMessage: "An unknown error occurred: \(error.localizedDescription)")
3131
return Reset.View(path: req.url.path, model: model).document()
3232
}
3333
}

Sources/App/Controllers/Manage/SignupController.swift

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,30 @@ enum SignupController {
1515
@Sendable
1616
static func signup(req: Request) async throws -> HTML {
1717
@Dependency(\.cognito) var cognito
18+
let awsClient = AWSClient(httpClientProvider: .shared(req.application.http.client.shared))
19+
let awsCognitoConfiguration = CognitoConfiguration(
20+
userPoolId: Environment.get("POOL_ID")!,
21+
clientId: Environment.get("CLIENT_ID")!,
22+
clientSecret: Environment.get("CLIENT_SECRET")!,
23+
cognitoIDP: CognitoIdentityProvider(client: awsClient, region: .useast2),
24+
adminClient: true
25+
)
26+
req.application.cognito.authenticatable = CognitoAuthenticatable(configuration: awsCognitoConfiguration)
1827
struct UserCreds: Content {
1928
var email: String
2029
var password: String
2130
}
2231
do {
2332
let user = try req.content.decode(UserCreds.self)
2433
try await cognito.signup(req: req, username: user.email, password: user.password)
34+
try await awsClient.shutdown()
2535
return Verify.View(path: SiteURL.verify.relativeURL(), model: Verify.Model(email: user.email)).document()
2636
} catch let error as AWSErrorType {
2737
let model = Signup.Model(errorMessage: error.message ?? "There was an error.")
38+
try await awsClient.shutdown()
2839
return Signup.View(path: req.url.path, model: model).document()
29-
} catch {
40+
} catch {
41+
try await awsClient.shutdown()
3042
return Signup.View(path: SiteURL.signup.relativeURL(), model: Signup.Model(errorMessage: "An unknown error occurred: \(error.localizedDescription)")).document()
3143
}
3244

Sources/App/Controllers/Manage/VerifyController.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ enum VerifyController {
2727
let model = Verify.Model(email: info.email, errorMessage: error.message ?? "There was an error.")
2828
return Verify.View(path: req.url.path, model: model).document()
2929
} catch {
30-
let model = Verify.Model(email: info.email, errorMessage: "An unknown error occurred.")
30+
let model = Verify.Model(email: info.email, errorMessage: "An unknown error occurred: \(error.localizedDescription)")
3131
return Verify.View(path: req.url.path, model: model).document()
3232
}
3333
}

0 commit comments

Comments
 (0)