-
-
Notifications
You must be signed in to change notification settings - Fork 17
/
Signin.php
100 lines (94 loc) · 3.4 KB
/
Signin.php
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
<?php
/**
* This file is part of the alphaz Framework.
*
* @author Muhammad Umer Farooq (Malik) <mumerfarooqlablnet01@gmail.com>
*
* @link https://github.com/alphazframework/framework
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @license MIT
*/
namespace alphaz\Auth;
use alphaz\Hashing\Hash;
use alphaz\http\Request;
use alphaz\Session\Session;
use alphaz\Validation\Validation;
class Signin extends Handler
{
/**
* Store the errors msgs.
*
* @since 1.0.0
*
* @var array
*/
protected $errors = [];
/**
* Signin the users.
*
* @param (string) $username username of user
* @param (mixed) $password password of user
*
* @since 1.0.0
*
* @return void
*/
public function signin($username, $password)
{
$rules = [
'username' => ['required' => true],
'password' => ['required' => true],
];
$inputs = [
'username' => $username,
'password' => $password,
];
$requireValidate = new Validation($inputs, $rules);
if ($requireValidate->fail()) {
Error::set($requireValidate->error()->get());
}
$user = new User();
if (!$user->isUsername($username)) {
Error::set(__printl('auth:error:username:not:exists'), 'username');
} else {
$password_hash = $user->getByWhere('username', $username)[0]['password'];
if (!Hash::verify($password, $password_hash)) {
Error::set(__printl('auth:error:password:confirm'), 'password');
} else {
$token = $user->getByWhere('username', $username)[0]['token'];
$email = $user->getByWhere('username', $username)[0]['email'];
if (__config()->auth->is_verify_email === true) {
if ($token !== 'NULL') {
$subject = __printl('auth:subject:need:verify');
$link = site_base_url().__config()->auth->verification_link.'/'.$token;
$html = __printl('auth:body:need:verify');
$html = str_replace(':email', $email, $html);
$html = str_replace(':link', $link, $html);
new EmailHandler($subject, $html, $email);
Error::set(__printl('auth:error:need:verification'), 'email');
}
}
}
}
if (!$user->isLogin()) {
if ($this->fail() !== true) {
$salts = $user->getByWhere('username', $username)[0]['salts'];
Session::set('user', $salts);
$request = new Request();
set_cookie('user', $salts, 31104000, '/', $request->getServerName(), false, true);
$password_hash = $user->getByWhere('username', $username)[0]['password'];
if (Hash::needsRehash($password_hash) === true) {
$hashed = Hash::make($password);
$update = new Update();
$update->update(['password'=>$hashed], $user->getByWhere('username', $username)[0]['id']);
}
Success::set(__printl('auth:success:signin'));
}
} else {
Error::set(__printl('auth:error:already:login'), 'login');
}
}
}