File tree Expand file tree Collapse file tree 2 files changed +30
-11
lines changed
Sources/App/Controllers/Manage Expand file tree Collapse file tree 2 files changed +30
-11
lines changed Original file line number Diff line number Diff line change 1+ import Vapor
2+ import SotoCognitoAuthentication
3+ import SotoCognitoIdentityProvider
4+ import SotoCognitoIdentity
5+
6+ struct Cognito {
7+ @Sendable
8+ static func authenticate( req: Request , username: String , password: String ) async throws {
9+ let awsClient = AWSClient ( httpClientProvider: . shared( req. application. http. client. shared) )
10+ let awsCognitoConfiguration = CognitoConfiguration (
11+ userPoolId: Environment . get ( " POOL_ID " ) !,
12+ clientId: Environment . get ( " CLIENT_ID " ) !,
13+ clientSecret: Environment . get ( " CLIENT_SECRET " ) !,
14+ cognitoIDP: CognitoIdentityProvider ( client: awsClient, region: . useast2) ,
15+ adminClient: true
16+ )
17+ req. application. cognito. authenticatable = CognitoAuthenticatable ( configuration: awsCognitoConfiguration)
18+ let response = try await req. application. cognito. authenticatable. authenticate ( username: username, password: password)
19+ switch response {
20+ case . authenticated( let authenticatedResponse) :
21+ let user = AuthenticatedUser ( accessToken: authenticatedResponse. accessToken!, refreshToken: authenticatedResponse. refreshToken!)
22+ req. auth. login ( user)
23+ case . challenged( let challengedResponse) : // TODO: handle challenge
24+ break
25+ }
26+ try awsClient. syncShutdown ( )
27+ }
28+ }
Original file line number Diff line number Diff line change @@ -18,21 +18,12 @@ enum LoginController {
1818 var email : String
1919 var password : String
2020 }
21- let user = try req. content. decode ( UserCreds . self)
22-
2321 do {
24- let response = try await req. application. cognito. authenticatable. authenticate ( username: user. email, password: user. password, context: req)
25- switch response {
26- case . authenticated( let authenticatedResponse) :
27- let user = AuthenticatedUser ( accessToken: authenticatedResponse. accessToken!, refreshToken: authenticatedResponse. refreshToken!)
28- req. auth. login ( user)
29- case . challenged( let challengedResponse) : // TODO: handle challenge
30- break
31- }
22+ let user = try req. content. decode ( UserCreds . self)
23+ try await Cognito . authenticate ( req: req, username: user. email, password: user. password)
3224 return req. redirect ( to: SiteURL . portal. relativeURL ( ) , redirectType: . normal)
3325 } catch let error as SotoCognitoError {
3426 var model = Login . Model ( errorMessage: " There was an error. Please try again. " )
35-
3627 switch error {
3728 case . unauthorized( let reason) :
3829 model = Login . Model ( errorMessage: reason ?? " There was an error. Please try again. " )
You can’t perform that action at this time.
0 commit comments