@@ -12,37 +12,46 @@ final public function __invoke(ServerRequestInterface $request, array $args): Re
1212 $ postBody = $ request ->getParsedBody ();
1313 $ response = $ this ->getResponse ();
1414
15- // var_dump($_SESSION);
16- if (isset ($ _SESSION ['userid ' ])) {
17- $ user = $ _SESSION ['userid ' ];
18- if ($ request ->getQueryParams ()['returnUrl ' ]) {
19- $ response = $ response ->withStatus (302 , "Redirecting " );
20- $ response = $ response ->withHeader ("Location " , $ request ->getQueryParams ()['returnUrl ' ]);
21- return $ response ;
22- }
23- $ response ->getBody ()->write ("<h1>Already logged in as $ user</h1> " );
24- } else if (
25- ($ postBody ['username ' ] == $ _ENV ['USERNAME ' ] && $ postBody ['password ' ] == $ _ENV ['PASSWORD ' ]) ||
26- ($ postBody ['username ' ] == $ _SERVER ['USERNAME ' ] && $ postBody ['password ' ] == $ _SERVER ['PASSWORD ' ])
27- ) {
28- $ user = $ postBody ['username ' ];
29- $ _SESSION ['userid ' ] = $ user ;
30- if ($ request ->getQueryParams ()['returnUrl ' ]) {
31- $ response = $ response ->withStatus (302 , "Redirecting " );
32- $ response = $ response ->withHeader ("Location " , $ request ->getQueryParams ()['returnUrl ' ]);
33- return $ response ;
34- }
35- $ response ->getBody ()->write ("<h1>Welcome $ user</h1> \n" );
36- // echo("session started\n");
37- //var_dump($_SESSION);
15+ if ($ request ->getMethod () === 'POST ' ) {
16+ if (isset ($ _SESSION ['userid ' ])) {
17+ $ user = $ _SESSION ['userid ' ];
18+
19+ if (isset ($ request ->getQueryParams ()['returnUrl ' ])) {
20+ return $ response
21+ ->withHeader ("Location " , $ request ->getQueryParams ()['returnUrl ' ])
22+ ->withStatus (302 )
23+ ;
24+ }
25+
26+ $ response ->getBody ()->write ("<h1>Already logged in as $ user</h1> " );
27+ } elseif ($ postBody ['username ' ] && $ postBody ['password ' ]) {
28+ $ user = $ postBody ['username ' ];
29+ $ password = $ postBody ['password ' ];
30+
31+ if (
32+ ($ user === $ _ENV ['USERNAME ' ] && $ password === $ _ENV ['PASSWORD ' ])
33+ || ($ user === $ _SERVER ['USERNAME ' ] && $ password === $ _SERVER ['PASSWORD ' ])
34+ ) {
35+ $ _SESSION ['userid ' ] = $ user ;
36+
37+ if (isset ($ request ->getQueryParams ()['returnUrl ' ])) {
38+ return $ response
39+ ->withHeader ("Location " , $ request ->getQueryParams ()['returnUrl ' ])
40+ ->withStatus (302 )
41+ ;
42+ }
43+
44+ $ response ->getBody ()->write ("<h1>Welcome $ user</h1> \n" );
45+ } else {
46+ $ response ->getBody ()->write ("<h1>Login as $ user failed</h1> \n" );
47+ }
48+ } else {
49+ $ response ->getBody ()->write ("<h1>Login failed</h1> \n" );
50+ }
3851 } else {
39- // var_dump($postBody);
40- //echo("cookie:\n");
41- //var_dump($_COOKIE);
42- //echo("session:\n");
43- //var_dump($_SESSION);
44- $ response ->getBody ()->write ("<h1>No (try posting username=alice&password=alice123)</h1> \n" );
52+ return $ this ->createTemplateResponse ('login.html ' );
4553 }
54+
4655 return $ response ;
4756 }
4857}
0 commit comments