Skip to content

Commit bfd70f8

Browse files
committed
allow to auto-login after registration
1 parent cd78c8c commit bfd70f8

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -705,6 +705,7 @@ You can tune the middleware behavior using middleware specific configuration par
705705
- "dbAuth.passwordFormField": The name of the form field that holds the password ("password")
706706
- "dbAuth.newPasswordFormField": The name of the form field that holds the new password ("newPassword")
707707
- "dbAuth.registerUser": JSON user data (or "1") in case you want the /register endpoint enabled ("")
708+
- "dbAuth.loginAfterRegistration": 1 or zero if registered users should be logged in after registration ("")
708709
- "dbAuth.passwordLength": Minimum length that the password must have ("12")
709710
- "dbAuth.sessionName": The name of the PHP session that is started ("")
710711
- "jwtAuth.mode": Set to "optional" if you want to allow anonymous access ("required")

src/Tqdev/PhpCrudApi/Middleware/DbAuthMiddleware.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,17 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
8787
$this->db->createSingle($table, $data);
8888
$users = $this->db->selectAll($table, $columnNames, $condition, $columnOrdering, 0, 1);
8989
foreach ($users as $user) {
90-
unset($user[$passwordColumnName]);
91-
return $this->responder->success($user);
90+
if($loginAfterRegistration){
91+
if (!headers_sent()) {
92+
session_regenerate_id(true);
93+
}
94+
unset($user[$passwordColumnName]);
95+
$_SESSION['user'] = $user;
96+
return $this->responder->success($user);
97+
} else {
98+
unset($user[$passwordColumnName]);
99+
return $this->responder->success($user);
100+
}
92101
}
93102
return $this->responder->error(ErrorCode::AUTHENTICATION_FAILED, $username);
94103
}

0 commit comments

Comments
 (0)