Skip to content

Commit fa7e60e

Browse files
authored
Merge pull request #900 from maheini/auto_login
Auto login
2 parents 85702b2 + 1a95b05 commit fa7e60e

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

README.md

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

src/Tqdev/PhpCrudApi/Middleware/DbAuthMiddleware.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
6262
$passwordLength = $this->getProperty('passwordLength', '12');
6363
$pkName = $table->getPk()->getName();
6464
$registerUser = $this->getProperty('registerUser', '');
65+
$loginAfterRegistration = $this->getProperty('loginAfterRegistration', '');
6566
$condition = new ColumnCondition($usernameColumn, 'eq', $username);
6667
$returnedColumns = $this->getProperty('returnedColumns', '');
6768
if (!$returnedColumns) {
@@ -90,8 +91,17 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
9091
$this->db->createSingle($table, $data);
9192
$users = $this->db->selectAll($table, $columnNames, $condition, $columnOrdering, 0, 1);
9293
foreach ($users as $user) {
93-
unset($user[$passwordColumnName]);
94-
return $this->responder->success($user);
94+
if($loginAfterRegistration){
95+
if (!headers_sent()) {
96+
session_regenerate_id(true);
97+
}
98+
unset($user[$passwordColumnName]);
99+
$_SESSION['user'] = $user;
100+
return $this->responder->success($user);
101+
} else {
102+
unset($user[$passwordColumnName]);
103+
return $this->responder->success($user);
104+
}
95105
}
96106
return $this->responder->error(ErrorCode::AUTHENTICATION_FAILED, $username);
97107
}

0 commit comments

Comments
 (0)