Skip to content

Commit fe533db

Browse files
committed
Divide into appropriately split files
1 parent dff6d49 commit fe533db

2 files changed

Lines changed: 67 additions & 43 deletions

File tree

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
//
2+
// JSONHandler.changePassword.swift
3+
// LocalAuthentication
4+
//
5+
// Created by Jonathan Guthrie on 2017-07-14.
6+
//
7+
8+
//
9+
// JSONHandler.me.swift
10+
// LocalAuthentication
11+
//
12+
// Created by Jonathan Guthrie on 2017-07-06.
13+
//
14+
15+
import PerfectHTTP
16+
import PerfectSession
17+
import PerfectCrypto
18+
import PerfectSessionPostgreSQL
19+
20+
21+
extension LocalAuthJSONHandlers {
22+
23+
// POST request for current user change password
24+
public static func changePassword(data: [String:Any]) throws -> RequestHandler {
25+
return {
26+
request, response in
27+
if let i = request.session?.userid, !i.isEmpty {
28+
let acc = Account()
29+
do {
30+
try acc.get(i)
31+
32+
// start chpwd
33+
if let postBody = request.postBodyString, !postBody.isEmpty {
34+
do {
35+
let postBodyJSON = try postBody.jsonDecode() as? [String: String] ?? [String: String]()
36+
if let password = postBodyJSON["password"], !password.isEmpty {
37+
acc.makePassword(password)
38+
try acc.save()
39+
_ = try response.setBody(json: ["error": "none", "msg":"Your password has been changed."])
40+
response.completed()
41+
return
42+
} else {
43+
LocalAuthHandlers.error(request, response, error: "Please supply a vaid password", code: .badRequest)
44+
return
45+
}
46+
} catch {
47+
LocalAuthHandlers.error(request, response, error: "Invalid JSON", code: .badRequest)
48+
return
49+
}
50+
} else {
51+
LocalAuthHandlers.error(request, response, error: "Change Password Error: Insufficient Data", code: .badRequest)
52+
return
53+
}
54+
// end chpwd
55+
} catch {
56+
LocalAuthHandlers.error(request, response, error: "AccountError", code: .badRequest)
57+
return
58+
}
59+
} else {
60+
LocalAuthHandlers.error(request, response, error: "NotLoggedIn", code: .badRequest)
61+
return
62+
}
63+
}
64+
}
65+
66+
}
67+

Sources/LocalAuthentication/Routing/jsonroutes/JSONHandler.me.swift

Lines changed: 0 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -40,47 +40,4 @@ extension LocalAuthJSONHandlers {
4040
}
4141
}
4242

43-
// POST request for current user change password
44-
public static func changePassword(data: [String:Any]) throws -> RequestHandler {
45-
return {
46-
request, response in
47-
if let i = request.session?.userid, !i.isEmpty {
48-
let acc = Account()
49-
do {
50-
try acc.get(i)
51-
52-
// start chpwd
53-
if let postBody = request.postBodyString, !postBody.isEmpty {
54-
do {
55-
let postBodyJSON = try postBody.jsonDecode() as? [String: String] ?? [String: String]()
56-
if let password = postBodyJSON["password"], !password.isEmpty {
57-
acc.makePassword(password)
58-
try acc.save()
59-
_ = try response.setBody(json: ["error": "none", "msg":"Your password has been changed."])
60-
response.completed()
61-
return
62-
} else {
63-
LocalAuthHandlers.error(request, response, error: "Please supply a vaid password", code: .badRequest)
64-
return
65-
}
66-
} catch {
67-
LocalAuthHandlers.error(request, response, error: "Invalid JSON", code: .badRequest)
68-
return
69-
}
70-
} else {
71-
LocalAuthHandlers.error(request, response, error: "Change Password Error: Insufficient Data", code: .badRequest)
72-
return
73-
}
74-
// end chpwd
75-
} catch {
76-
LocalAuthHandlers.error(request, response, error: "AccountError", code: .badRequest)
77-
return
78-
}
79-
} else {
80-
LocalAuthHandlers.error(request, response, error: "NotLoggedIn", code: .badRequest)
81-
return
82-
}
83-
}
84-
}
85-
8643
}

0 commit comments

Comments
 (0)