Skip to content

Commit 5a4ac50

Browse files
committed
adding /me route
1 parent de2ca12 commit 5a4ac50

2 files changed

Lines changed: 52 additions & 0 deletions

File tree

Sources/LocalAuthentication/Routing/Routes.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,16 @@ public func mainAuthenticationRoutes() -> [[String: Any]] {
2323
routes.append(["method":"post", "uri":"/registrationCompletion", "handler":LocalAuthWebHandlers.registerCompletion])
2424

2525
// JSON
26+
27+
/// Loads current session & csrf for headers
2628
routes.append(["method":"get", "uri":"/api/v1/session", "handler":LocalAuthJSONHandlers.session])
29+
/// Loads info about current user
30+
routes.append(["method":"get", "uri":"/api/v1/me", "handler":LocalAuthJSONHandlers.me])
31+
/// Ends current session
2732
routes.append(["method":"get", "uri":"/api/v1/logout", "handler":LocalAuthJSONHandlers.logout])
33+
/// Initiates registration process
2834
routes.append(["method":"post", "uri":"/api/v1/register", "handler":LocalAuthJSONHandlers.register])
35+
/// Login post route
2936
routes.append(["method":"post", "uri":"/api/v1/login", "handler":LocalAuthJSONHandlers.login])
3037

3138

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
//
2+
// JSONHandler.me.swift
3+
// LocalAuthentication
4+
//
5+
// Created by Jonathan Guthrie on 2017-07-06.
6+
//
7+
8+
import PerfectHTTP
9+
import PerfectSession
10+
import PerfectCrypto
11+
import PerfectSessionPostgreSQL
12+
13+
14+
extension LocalAuthJSONHandlers {
15+
16+
// GET request for current user info
17+
public static func me(data: [String:Any]) throws -> RequestHandler {
18+
return {
19+
request, response in
20+
print("request.session?.userid: \(request.session?.userid)")
21+
if let i = request.session?.userid, !i.isEmpty {
22+
let acc = Account()
23+
do {
24+
try acc.get(i)
25+
_ = try? response.setBody(json: [
26+
"userid":acc.id,
27+
"username":acc.username,
28+
"email":acc.email,
29+
"usertype":"\(acc.usertype)"
30+
])
31+
response.completed()
32+
return
33+
} catch {
34+
LocalAuthHandlers.error(request, response, error: "AccountError", code: .badRequest)
35+
return
36+
}
37+
} else {
38+
LocalAuthHandlers.error(request, response, error: "NotLoggedIn", code: .badRequest)
39+
return
40+
}
41+
}
42+
}
43+
44+
45+
}

0 commit comments

Comments
 (0)