-
Notifications
You must be signed in to change notification settings - Fork 1
/
eloquentUserProvider.js
109 lines (82 loc) · 2.41 KB
/
eloquentUserProvider.js
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
101
102
103
104
105
106
107
108
109
class EloquentUserProvider {
$model;
$hasher;
constructor($hasher, $model) {
this.$model = $model;
this.$hasher = $hasher;
}
retrieveById($identifier) {
let $model = this.createModel();
return this.newModelQuery($model)
.where($model.getAuthIdentifierName(), $identifier)
.first();
}
async retrieveByToken($identifier, $token) {
let $model = this.createModel();
let $retrievedModel = await this.newModelQuery($model).where(
$model.getAuthIdentifierName(), $identifier
).first();
if (!$retrievedModel) {
return;
}
let $rememberToken = $retrievedModel.getRememberToken();
return $rememberToken && $rememberToken == $token ?
$retrievedModel : null;
}
updateRememberToken($user, $token) {
$user.setRememberToken($token);
return $user.save();
}
retrieveByCredentials($credentials) {
if (empty($credentials) ||
(count($credentials) === 1 &&
String.contains(this.firstCredentialKey($credentials), 'password'))) {
return;
}
let $query = this.newModelQuery();
for (let $key in $credentials) {
let $value = $credentials[$key]
if (String.contains($key, 'password')) {
continue;
}
if (is_array($value) || $value instanceof Array) {
$query.whereIn($key, $value);
} else {
$query.where($key, $value);
}
}
return $query.first();
}
firstCredentialKey($credentials) {
for (let $key in $credentials) {
return $key;
}
}
validateCredentials($user, $credentials) {
let $plain = $credentials['password'];
return this.$hasher.check($plain, $user.getAuthPassword());
}
newModelQuery($model = null) {
return is_null($model) ?
this.createModel().newQuery() :
$model.newQuery();
}
createModel() {
return new this.$model;
}
getHasher() {
return this.$hasher;
}
setHasher($hasher) {
this.$hasher = $hasher;
return this;
}
getModel() {
return this.$model;
}
setModel($model) {
this.$model = $model;
return this;
}
}
module.exports = EloquentUserProvider