Skip to content

Commit f06be9a

Browse files
committed
descriptive env variables + entirely move cognito config
1 parent 78ef4d4 commit f06be9a

File tree

3 files changed

+6
-31
lines changed

3 files changed

+6
-31
lines changed

Sources/App/Controllers/Manage/Cognito.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ struct Cognito {
88
static func authenticate(req: Request, username: String, password: String) async throws {
99
let awsClient = AWSClient(httpClientProvider: .shared(req.application.http.client.shared))
1010
let awsCognitoConfiguration = CognitoConfiguration(
11-
userPoolId: Environment.get("POOL_ID")!,
12-
clientId: Environment.get("CLIENT_ID")!,
13-
clientSecret: Environment.get("CLIENT_SECRET")!,
11+
userPoolId: Environment.get("AWS_COGNITO_POOL_ID")!,
12+
clientId: Environment.get("AWS_COGNITO_CLIENT_ID")!,
13+
clientSecret: Environment.get("AWS_COGNITO_CLIENT_SECRET")!,
1414
cognitoIDP: CognitoIdentityProvider(client: awsClient, region: .useast2),
1515
adminClient: true
1616
)
@@ -30,9 +30,9 @@ struct Cognito {
3030
static func signup(req: Request, username: String, password: String) async throws {
3131
let awsClient = AWSClient(httpClientProvider: .shared(req.application.http.client.shared))
3232
let awsCognitoConfiguration = CognitoConfiguration(
33-
userPoolId: Environment.get("POOL_ID")!,
34-
clientId: Environment.get("CLIENT_ID")!,
35-
clientSecret: Environment.get("CLIENT_SECRET")!,
33+
userPoolId: Environment.get("AWS_COGNITO_POOL_ID")!,
34+
clientId: Environment.get("AWS_COGNITO_CLIENT_ID")!,
35+
clientSecret: Environment.get("AWS_COGNITO_CLIENT_SECRET")!,
3636
cognitoIDP: CognitoIdentityProvider(client: awsClient, region: .useast2),
3737
adminClient: true
3838
)

Sources/App/Controllers/Manage/LoginController.swift

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,23 +16,13 @@ enum LoginController {
1616
@Sendable
1717
static func login(req: Request) async throws -> Response {
1818
@Dependency(\.cognito) var cognito
19-
let awsClient = AWSClient(httpClientProvider: .shared(req.application.http.client.shared))
20-
let awsCognitoConfiguration = CognitoConfiguration(
21-
userPoolId: Environment.get("POOL_ID")!,
22-
clientId: Environment.get("CLIENT_ID")!,
23-
clientSecret: Environment.get("CLIENT_SECRET")!,
24-
cognitoIDP: CognitoIdentityProvider(client: awsClient, region: .useast2),
25-
adminClient: true
26-
)
27-
req.application.cognito.authenticatable = CognitoAuthenticatable(configuration: awsCognitoConfiguration)
2819
struct UserCreds: Content {
2920
var email: String
3021
var password: String
3122
}
3223
do {
3324
let user = try req.content.decode(UserCreds.self)
3425
try await cognito.authenticate(req: req, username: user.email, password: user.password)
35-
try await awsClient.shutdown()
3626
return req.redirect(to: SiteURL.portal.relativeURL(), redirectType: .normal)
3727
} catch let error as SotoCognitoError {
3828
var model = Login.Model(errorMessage: "There was an error. Please try again.")
@@ -44,13 +34,10 @@ enum LoginController {
4434
case .invalidPublicKey:
4535
break
4636
}
47-
try await awsClient.shutdown()
4837
return Login.View(path: req.url.path, model: model).document().encodeResponse(status: .unauthorized)
4938
} catch let error as AWSClientError {
50-
try await awsClient.shutdown()
5139
return Login.View(path: SiteURL.login.relativeURL(), model: Login.Model(errorMessage: "An AWS client error occurred: \(error.errorCode)")).document().encodeResponse(status: .unauthorized)
5240
} catch {
53-
try await awsClient.shutdown()
5441
return Login.View(path: SiteURL.login.relativeURL(), model: Login.Model(errorMessage: "An unknown error occurred: \(error.localizedDescription)")).document().encodeResponse(status: .unauthorized)
5542
}
5643

Sources/App/Controllers/Manage/SignupController.swift

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,30 +15,18 @@ 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)
2718
struct UserCreds: Content {
2819
var email: String
2920
var password: String
3021
}
3122
do {
3223
let user = try req.content.decode(UserCreds.self)
3324
try await cognito.signup(req: req, username: user.email, password: user.password)
34-
try await awsClient.shutdown()
3525
return Verify.View(path: SiteURL.verify.relativeURL(), model: Verify.Model(email: user.email)).document()
3626
} catch let error as AWSErrorType {
3727
let model = Signup.Model(errorMessage: error.message ?? "There was an error.")
38-
try await awsClient.shutdown()
3928
return Signup.View(path: req.url.path, model: model).document()
4029
} catch {
41-
try await awsClient.shutdown()
4230
return Signup.View(path: SiteURL.signup.relativeURL(), model: Signup.Model(errorMessage: "An unknown error occurred: \(error.localizedDescription)")).document()
4331
}
4432

0 commit comments

Comments
 (0)