Skip to content

Commit 205ee2d

Browse files
committed
seperate verify and delete cognito functions
1 parent b6b2bc6 commit 205ee2d

File tree

3 files changed

+35
-4
lines changed

3 files changed

+35
-4
lines changed

Sources/App/Controllers/Manage/Cognito.swift

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,4 +79,34 @@ struct Cognito {
7979
try await req.application.cognito.authenticatable.confirmForgotPassword(username: username, newPassword: password, confirmationCode: confirmationCode)
8080
try awsClient.syncShutdown()
8181
}
82+
83+
@Sendable
84+
static func confirmSignUp(req: Request, username: String, confirmationCode: String) async throws {
85+
let awsClient = AWSClient(httpClientProvider: .shared(req.application.http.client.shared))
86+
let awsCognitoConfiguration = CognitoConfiguration(
87+
userPoolId: Environment.get("AWS_COGNITO_POOL_ID")!,
88+
clientId: Environment.get("AWS_COGNITO_CLIENT_ID")!,
89+
clientSecret: Environment.get("AWS_COGNITO_CLIENT_SECRET")!,
90+
cognitoIDP: CognitoIdentityProvider(client: awsClient, region: .useast2),
91+
adminClient: true
92+
)
93+
req.application.cognito.authenticatable = CognitoAuthenticatable(configuration: awsCognitoConfiguration)
94+
try await req.application.cognito.authenticatable.confirmSignUp(username: username, confirmationCode: confirmationCode)
95+
}
96+
97+
@Sendable
98+
static func deleteUser(req: Request, accessToken: String) async throws {
99+
let awsClient = AWSClient(httpClientProvider: .shared(req.application.http.client.shared))
100+
let awsCognitoConfiguration = CognitoConfiguration(
101+
userPoolId: Environment.get("AWS_COGNITO_POOL_ID")!,
102+
clientId: Environment.get("AWS_COGNITO_CLIENT_ID")!,
103+
clientSecret: Environment.get("AWS_COGNITO_CLIENT_SECRET")!,
104+
cognitoIDP: CognitoIdentityProvider(client: awsClient, region: .useast2),
105+
adminClient: true
106+
)
107+
req.application.cognito.authenticatable = CognitoAuthenticatable(configuration: awsCognitoConfiguration)
108+
let request = CognitoIdentityProvider.DeleteUserRequest(accessToken: accessToken)
109+
try await req.application.cognito.authenticatable.configuration.cognitoIDP.deleteUser(request)
110+
try awsClient.syncShutdown()
111+
}
82112
}

Sources/App/Controllers/Manage/DeleteAccountController.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@ enum DeleteAccountController {
1010
@Sendable
1111
static func deleteAccount(req: Request) async throws -> Response {
1212
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)
13+
try await Cognito.deleteUser(req: req, accessToken: req.auth.require(AuthenticatedUser.self).sessionID)
1514
req.auth.logout(AuthenticatedUser.self)
1615
req.session.unauthenticate(AuthenticatedUser.self)
1716
req.session.destroy()

Sources/App/Controllers/Manage/VerifyController.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,17 @@ enum VerifyController {
1818
var email: String
1919
var confirmationCode: String
2020
}
21-
let info = try req.content.decode(VerifyInformation.self)
2221
do {
23-
try await req.application.cognito.authenticatable.confirmSignUp(username: info.email, confirmationCode: info.confirmationCode)
22+
let info = try req.content.decode(VerifyInformation.self)
23+
try await Cognito.confirmSignUp(req: req, username: info.email, confirmationCode: info.confirmationCode)
2424
let model = SuccessfulChange.Model(successMessage: "Successfully confirmed signup")
2525
return SuccessfulChange.View(path: req.url.path, model: model).document()
2626
} catch let error as AWSErrorType {
27+
let info = try req.content.decode(VerifyInformation.self)
2728
let model = Verify.Model(email: info.email, errorMessage: error.message ?? "There was an error.")
2829
return Verify.View(path: req.url.path, model: model).document()
2930
} catch {
31+
let info = try req.content.decode(VerifyInformation.self)
3032
let model = Verify.Model(email: info.email, errorMessage: "An unknown error occurred: \(error.localizedDescription)")
3133
return Verify.View(path: req.url.path, model: model).document()
3234
}

0 commit comments

Comments
 (0)