Skip to content

Commit 8d4508f

Browse files
committed
flesh out auth components + minor css
1 parent a5b301b commit 8d4508f

33 files changed

+758
-98
lines changed

FrontEnd/main.scss

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,4 @@ $mobile-breakpoint: 740px;
4646
@import 'styles/tab_bar';
4747
@import 'styles/validate_manifest';
4848
@import 'styles/vega_charts';
49+
@import 'styles/manage';

FrontEnd/styles/manage.scss

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Copyright Dave Verwer, Sven A. Schmidt, and other contributors.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
// -------------------------------------------------------------------------
16+
// Styles for authentication pages (login, signup, etc.)
17+
// -------------------------------------------------------------------------
18+
19+
.manage-page {
20+
height: 55vh;
21+
padding-top: 10%;
22+
}
23+
24+
.manage-form-inputs {
25+
display: flex;
26+
flex-direction: column;
27+
width: 50%;
28+
margin-bottom: 15px;
29+
}

Sources/App/Controllers/Authentication/LoginController.swift

Lines changed: 0 additions & 8 deletions
This file was deleted.

Sources/App/Controllers/Authentication/SignupController.swift

Lines changed: 0 additions & 8 deletions
This file was deleted.

Sources/App/Controllers/Authentication/VerifyController.swift

Lines changed: 0 additions & 8 deletions
This file was deleted.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import Foundation
2+
import Fluent
3+
import Plot
4+
import Vapor
5+
import SotoCognitoAuthentication
6+
import SotoCognitoIdentityProvider
7+
import SotoCognitoIdentity
8+
9+
enum DeleteAccountController {
10+
@Sendable
11+
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())
18+
}
19+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import Fluent
2+
import Plot
3+
import Vapor
4+
import SotoCognitoAuthentication
5+
import SotoCognitoIdentityProvider
6+
import SotoCognitoIdentity
7+
8+
enum ForgotPasswordController {
9+
@Sendable
10+
static func show(req: Request) async throws -> HTML {
11+
return ForgotPassword.View(path: req.url.path).document()
12+
}
13+
14+
@Sendable
15+
static func forgotPasswordEmail(req: Request) async throws -> HTML {
16+
struct Credentials: Content {
17+
var email: String
18+
}
19+
let user = try req.content.decode(Credentials.self)
20+
do {
21+
try await req.application.cognito.authenticatable.forgotPassword(username: user.email)
22+
return Reset.View(path: SiteURL.resetPassword.relativeURL(), model: Reset.Model(email: user.email)).document()
23+
} catch {
24+
// TODO: handle this
25+
return Reset.View(path: SiteURL.resetPassword.relativeURL(), model: Reset.Model(email: user.email)).document()
26+
}
27+
}
28+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import Foundation
2+
import Fluent
3+
import Plot
4+
import Vapor
5+
import SotoCognitoAuthentication
6+
import SotoCognitoIdentityProvider
7+
import SotoCognitoIdentity
8+
9+
enum LoginController {
10+
@Sendable
11+
static func show(req: Request) async throws -> HTML {
12+
return Login.View(path: req.url.path, model: Login.Model(errorMessage: "")).document()
13+
}
14+
15+
@Sendable
16+
static func login(req: Request) async throws -> Response {
17+
struct UserCreds: Content {
18+
var email: String
19+
var password: String
20+
}
21+
let user = try req.content.decode(UserCreds.self)
22+
23+
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+
}
32+
return req.redirect(to: SiteURL.portal.relativeURL(), redirectType: .normal)
33+
} catch let error as SotoCognitoError {
34+
var model = Login.Model(errorMessage: "There was an error. Please try again.")
35+
36+
switch error {
37+
case .unauthorized(let reason):
38+
model = Login.Model(errorMessage: reason ?? "There was an error. Please try again.")
39+
case .unexpectedResult(let reason):
40+
model = Login.Model(errorMessage: reason ?? "There was an error. Please try again.")
41+
case .invalidPublicKey:
42+
break
43+
}
44+
return Login.View(path: req.url.path, model: model).document().encodeResponse(status: .unauthorized)
45+
} catch {
46+
return Login.View(path: SiteURL.signup.relativeURL(), model: Login.Model(errorMessage: "An unknown error occurred. Please try again.")).document().encodeResponse(status: .unauthorized)
47+
}
48+
49+
}
50+
}
51+
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import Foundation
2+
import Fluent
3+
import Plot
4+
import Vapor
5+
import SotoCognitoAuthentication
6+
import SotoCognitoIdentityProvider
7+
import SotoCognitoIdentity
8+
9+
enum LogoutController {
10+
@Sendable
11+
static func logout(req: Request) async throws -> Response {
12+
req.auth.logout(AuthenticatedUser.self)
13+
req.session.unauthenticate(AuthenticatedUser.self)
14+
req.session.destroy()
15+
return req.redirect(to: SiteURL.home.relativeURL())
16+
}
17+
}
18+

Sources/App/Controllers/Authentication/PortalController.swift renamed to Sources/App/Controllers/Manage/PortalController.swift

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,7 @@
1-
//
2-
// PortalController.swift
3-
//
4-
//
5-
6-
71
import Fluent
82
import Plot
93
import Vapor
4+
import SotoCognitoAuthenticationKit
105

116
enum PortalController {
127
@Sendable

0 commit comments

Comments
 (0)