Skip to content

Commit

Permalink
Merge pull request #24 from MatsMoll/feature/user-profile
Browse files Browse the repository at this point in the history
[feature] Added web controller for user profile
  • Loading branch information
MatsMoll authored Feb 13, 2020
2 parents 6695524 + 8cd62b1 commit b55ae82
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions Sources/App/User/UserWebController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,16 @@ import KognitaViews
import Mailgun
import FluentPostgreSQL
import KognitaAPI
import Authentication

final class UserWebController: RouteCollection {

func boot(router: Router) {

let redirectMiddle = router.grouped(RedirectMiddleware<User>(path: "/login"))

redirectMiddle.get("profile", use: profilePage)

router.get("signup", use: signupForm)
router.get("login", use: loginForm)
router.get("start-reset-password", use: startResetPasswordForm)
Expand All @@ -29,13 +34,32 @@ final class UserWebController: RouteCollection {
router.post("signup", use: create)
router.post("start-reset-password", use: startResetPassword)
router.post("reset-password", use: resetPassword)

}

func signupForm(_ req: Request) throws -> EventLoopFuture<View> {
User.Templates.Signup()
.render(with: .init(), for: req)
}

func profilePage(_ req: Request) throws -> EventLoopFuture<View> {
let user = try req.requireAuthenticated(User.self)

return try Subject.DatabaseRepository
.allActive(for: user, on: req)
.map { subjects in

try req.renderer()
.render(
view: User.Templates.Profile.self,
with: .init(
user: user,
subjects: subjects
)
)
}
}

func loginForm(_ req: Request) throws -> EventLoopFuture<Response> {

if try req.authenticated(User.self) != nil {
Expand Down

0 comments on commit b55ae82

Please sign in to comment.