|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Support\Auth; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Support\Facades\Validator; |
|
6
|
|
|
use Illuminate\Support\Str; |
|
7
|
|
|
use Propaganistas\LaravelPhone\PhoneNumber; |
|
8
|
|
|
|
|
9
|
|
|
trait MultipleIdentifier |
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* Return the identifier field type based on the given request. |
|
13
|
|
|
* |
|
14
|
|
|
* @param mixed $value |
|
15
|
|
|
* @return string |
|
16
|
|
|
*/ |
|
17
|
|
|
protected function getIdentifierField($value): string |
|
18
|
|
|
{ |
|
19
|
|
|
if (filter_var($value, FILTER_VALIDATE_EMAIL) !== false) { |
|
20
|
|
|
return 'email'; |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
if (!Validator::make(['phone' => $value], ['phone' => 'phone:ID'])->fails()) { |
|
24
|
|
|
return 'phone'; |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
return 'username'; |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Return list of necessary identifier rule for validation. |
|
32
|
|
|
* |
|
33
|
|
|
* @param mixed $value |
|
34
|
|
|
* @return array |
|
35
|
|
|
*/ |
|
36
|
|
|
protected function getIdentifierRule($value): array |
|
37
|
|
|
{ |
|
38
|
|
|
$field = $this->getIdentifierField($value); |
|
39
|
|
|
|
|
40
|
|
|
$rules = ['required', 'string', 'exists:users,' . $field]; |
|
41
|
|
|
|
|
42
|
|
|
switch ($field) { |
|
43
|
|
|
case 'email': |
|
44
|
|
|
$rules[] = 'email'; |
|
45
|
|
|
break; |
|
46
|
|
|
|
|
47
|
|
|
case 'phone': |
|
48
|
|
|
$rules[] = 'phone:ID'; |
|
49
|
|
|
break; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
return $rules; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Return identifier value for authentication process. |
|
57
|
|
|
* |
|
58
|
|
|
* @param mixed $value |
|
59
|
|
|
* @param string $country |
|
60
|
|
|
* @return mixed |
|
61
|
|
|
*/ |
|
62
|
|
|
protected function getIdentifierValue($value, string $country = 'ID') |
|
63
|
|
|
{ |
|
64
|
|
|
$field = $this->getIdentifierField($value); |
|
65
|
|
|
|
|
66
|
|
|
if ($field === 'phone') { |
|
67
|
|
|
return (string) PhoneNumber::make($value, $country); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
return $value; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Return identifier and password data as the credentials. |
|
75
|
|
|
* |
|
76
|
|
|
* @param \Illuminate\Http\Request $request |
|
77
|
|
|
* @param string $identifierField |
|
78
|
|
|
* @return array |
|
79
|
|
|
*/ |
|
80
|
|
|
protected function getCredentials($request, string $identifiedField = 'identifier'): array |
|
81
|
|
|
{ |
|
82
|
|
|
$value = $this->getIdentifierValue($request->input($identifiedField)); |
|
83
|
|
|
|
|
84
|
|
|
return [ |
|
85
|
|
|
$this->getIdentifierField($value) => $value, |
|
86
|
|
|
'password' => $request->input('password'), |
|
87
|
|
|
]; |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
|