Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enables targeting custom user providers and guards #77

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 13 additions & 11 deletions src/Controllers/CypressController.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,44 +27,46 @@ public function routes()

public function login(Request $request)
{
$provider = $request->input('provider', 'users');
$guard = $request->input('guard');
$attributes = $request->input('attributes', []);

if (empty($attributes)) {
$user = $this->factoryBuilder(
$this->userClassName(),
$this->userClassName($provider),
$request->input('state', [])
)->create();
} else {
$user = app($this->userClassName())
$user = app($this->userClassName($provider))
->newQuery()
->where($attributes)
->first();

if (!$user) {
$user = $this->factoryBuilder(
$this->userClassName(),
$this->userClassName($provider),
$request->input('state', [])
)->create($attributes);
}
}

$user->load($request->input('load', []));

return tap($user, function ($user) {
auth()->login($user);
return tap($user, function ($user) use ($guard) {
auth($guard)->login($user);

$user->setHidden([])->setVisible([]);
});
}

public function currentUser()
public function currentUser(Request $request)
{
return auth()->user()?->setHidden([])->setVisible([]);
return auth($request->input('guard'))->user()?->setHidden([])->setVisible([]);
}

public function logout()
public function logout(Request $request)
{
auth()->logout();
auth($request->input('guard'))->logout();
}

public function factory(Request $request)
Expand Down Expand Up @@ -114,9 +116,9 @@ public function runPhp(Request $request)
]);
}

protected function userClassName()
protected function userClassName($provider)
{
return config('auth.providers.users.model');
return config("auth.providers.{$provider}.model");
}

protected function factoryBuilder($model, $states = [])
Expand Down
4 changes: 3 additions & 1 deletion src/stubs/support/laravel-commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@
*
* @example cy.login();
* cy.login({ name: 'JohnDoe' });
* cy.login({ name: 'JohnDoe' });
* cy.login({ attributes: { name: 'JohnDoe' }, state: 'guest', load: ['comments] });
* cy.login({ attributes: { name: 'JohnDoe' }, provider: 'users', guard: 'custom-guard' });
*/
Cypress.Commands.add('login', (attributes = {}) => {
// Are we using the new object system.
let requestBody = attributes.attributes || attributes.state || attributes.load ? attributes : { attributes };
let requestBody = (attributes.attributes || attributes.state || attributes.load) ? attributes : { attributes };

return cy
.csrfToken()
Expand Down