-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Adds a new CoilpackUserProvider callback to Laravel's Authentication Provider. - Respects the Member model (default Coilpack Member model or one added in the config) - Uses EE's native authentication functions to create the session --------- Co-authored-by: Stephen Galbraith <[email protected]>
- Loading branch information
1 parent
5faebd6
commit 99ede38
Showing
6 changed files
with
121 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
<?php | ||
|
||
namespace Expressionengine\Coilpack\Auth; | ||
|
||
use Illuminate\Auth\EloquentUserProvider; | ||
use Illuminate\Contracts\Auth\Authenticatable; | ||
|
||
class CoilpackUserProvider extends EloquentUserProvider | ||
{ | ||
/** | ||
* Retrieve a user by the given credentials. | ||
* | ||
* @param array $credentials | ||
* @return \Illuminate\Contracts\Auth\Authenticatable|null | ||
*/ | ||
public function retrieveByCredentials(array $credentials) | ||
{ | ||
$key = array_key_exists('username', $credentials) ? 'username' : 'email'; | ||
$value = $credentials[$key]; | ||
|
||
return $this->newModelQuery() | ||
->where('username', $value) | ||
->when(strpos($value, '@'), function ($query) use ($value) { | ||
$query->orWhere('email', $value); | ||
}) | ||
->first(); | ||
} | ||
|
||
/** | ||
* Validate a user against the given credentials. | ||
* | ||
* @param \Illuminate\Contracts\Auth\Authenticatable $user | ||
* @param array $credentials | ||
* @return bool | ||
*/ | ||
public function validateCredentials(Authenticatable $user, array $credentials) | ||
{ | ||
ee()->load->library('auth'); | ||
|
||
$result = false; | ||
|
||
if (array_key_exists('username', $credentials)) { | ||
$result = ee()->auth->authenticate_username($credentials['username'], $credentials['password']); | ||
} elseif (array_key_exists('email', $credentials)) { | ||
$result = ee()->auth->authenticate_email($credentials['email'], $credentials['password']); | ||
} | ||
|
||
if (! $result) { | ||
return false; | ||
} | ||
|
||
$result->start_session(); | ||
|
||
return $result !== false; | ||
} | ||
|
||
/** | ||
* Retrieve a user by their unique identifier and "remember me" token. | ||
* | ||
* @param mixed $identifier | ||
* @param string $token | ||
* @return \Illuminate\Contracts\Auth\Authenticatable|null | ||
*/ | ||
public function retrieveByToken($identifier, $token) | ||
{ | ||
if (! ee()->remember->exists()) { | ||
return null; | ||
} | ||
|
||
$model = $this->createModel(); | ||
|
||
return $this->newModelQuery($model)->where( | ||
$model->getAuthIdentifierName(), | ||
ee()->remember->data('member_id') | ||
)->first(); | ||
} | ||
|
||
/** | ||
* Update the "remember me" token for the given user in storage. | ||
* | ||
* @param \Illuminate\Contracts\Auth\Authenticatable $user | ||
* @param string $token | ||
* @return void | ||
*/ | ||
public function updateRememberToken(Authenticatable $user, $token) | ||
{ | ||
ee()->remember->exists() ? ee()->remember->refresh() : ee()->remember->create(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters