Skip to content
This repository has been archived by the owner on Aug 19, 2022. It is now read-only.

Feature/login info social docs #75

Merged
merged 3 commits into from
Mar 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions Sources/App/Controllers/SocialController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,29 @@ class SocialController: RouteCollection {
}

// MARK: Handlers

/// SocialHandler
/// - Parameter: Request with an Authenticated `User`
///
/// - Throws: Error `BadUser` if the `user.id` is not valid
///
/// - Returns: `SocialInformation` of `User`
func socialHandler(_ req: Request) throws -> Future<SocialInformation> {
let user = try req.requireAuthenticated(User.self)

guard let userId = user.id else {
return req.future(error: BadUser())
}

return Future.map(on: req) { user.social ?? SocialInformation(id: userId, username: "", firstName: "", lastName: "", email: "", discordUsername: "", githubUsername: "", tags: [], profileImage: "", biography: "", links: [], location: "") }
}

/// UpdateSocialHandler
/// - Parameter: Request with an Authenticated `User`
/// Body with a `SocialInformation` to update the User's `SocialInformation`
///
/// - Throws: Error `BadUser` if the `\User.id != social.id`
///
/// - Returns: `SocialInformation` of `User`
func updateSocialHandler(_ req: Request) throws -> Future<SocialInformation> {
_ = try req.requireAuthenticated(User.self)

Expand All @@ -44,7 +56,7 @@ class SocialController: RouteCollection {
.first()
.flatMap { result in
guard let result = result else {
return req.future(error: BadPost())
return req.future(error: BadUser())
}

result.social = social
Expand Down
14 changes: 10 additions & 4 deletions Sources/App/Controllers/UserController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,19 +44,25 @@ class UserController: RouteCollection {
}
}

func login(_ req: Request, _ loginRequest: LoginRequest) throws -> Future<HTTPStatus> {
func login(_ req: Request, _ loginRequest: LoginRequest) throws -> Future<HTTPResponse> {
let userService = try req.make(UserService.self)

return userService.authorize(
email: loginRequest.email,
password: loginRequest.password,
on: req
).map { user in
)
.map(to: PublicUserResponse.self) { user in
guard let user = user else {
return .unauthorized
throw Abort(.unauthorized)
}
try req.authenticateSession(user)
return .ok
return PublicUserResponse(id: user.id, email: user.email, social: user.social)
}
.map(to: HTTPResponse.self) { publicUser in
var response = HTTPResponse(status: .created)
try JSONEncoder().encode(publicUser, to: &response, on: req)
return response
}
}

Expand Down