-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathLoginController.php
More file actions
53 lines (34 loc) · 1.14 KB
/
LoginController.php
File metadata and controls
53 lines (34 loc) · 1.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
<?php
namespace App\Controller;
use App\{Model\User AS UserModel,Helper\AuthHelper};
use Core\View;
class LoginController extends \Core\Controller {
const defaultMethod = 'login';
public function login() {
View::render('login.php');
}
public function loginAction() {
header('Content-Type: application/json');
$response = ['status' => false];
try {
if (empty($_POST) || !isset($_POST['user_name']) || !isset($_POST['password']))
throw new \BadMethodCallException(__METHOD__ . ': eksik parametre');
$modelUser = new UserModel;
$user = $modelUser -> getUserByUserName($_POST['user_name']);
if (!$user)
throw new \Exception(__METHOD__ . ':notFound');
if (password_verify($_POST['password'], $user['password']) === false)
throw new \Exception(__METHOD__ . ':notMatch');
AuthHelper::getInstance()->authenticate($user['id']);
$response['result'] = $user['id'];
$response['status'] = true;
} catch(\Exception $e) {
$response['error'] = $e -> getMessage();
}
echo json_encode($response);
}
public function logout() {
AuthHelper::getInstance()->endSession();
header('Location: login');
}
}