@@ -17,7 +17,6 @@ extension LocalAuthJSONHandlers {
1717 public static func me( data: [ String : Any ] ) throws -> RequestHandler {
1818 return {
1919 request, response in
20- print ( " request.session?.userid: \( request. session? . userid) " )
2120 if let i = request. session? . userid, !i. isEmpty {
2221 let acc = Account ( )
2322 do {
@@ -41,5 +40,47 @@ extension LocalAuthJSONHandlers {
4140 }
4241 }
4342
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+ }
4485
4586}
0 commit comments