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

Cesargb patch 1 #10

Closed
wants to merge 10 commits into from
Closed
Show file tree
Hide file tree
Changes from 9 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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
/build
composer.lock
.phpunit.result.cache
.idea
14 changes: 11 additions & 3 deletions src/MagicLink.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public function add($user, $redirect_url = '', $lifetime = 0)
if (is_int($user)) {
$MagicLink->user_id = $user;
} else {
$MagicLink->user_id = $user->id;
$MagicLink->user_id = $user->getKey();
}

$MagicLink->token = Str::random(config('magiclink.token.length', 64));
Expand Down Expand Up @@ -51,12 +51,20 @@ public function auth($token)
->first();

if ($magicLink) {
$user = config('auth.providers.users.model')::find($magicLink->user_id);

$authProvider = "auth.providers." . config('magiclink.auth_provider').".model";

$user = config($authProvider)::find($magicLink->user_id);

if ($user) {
app()->make('auth')->loginUsingId($magicLink->user_id);
app()->make('auth')->guard(config('magiclink.auth_guard'))->loginUsingId($magicLink->user_id);

//invalidating link after clicked once
$magicLink->available_at = Carbon::now()->subMinute();
$magicLink->save();

if ($magicLink->redirect_url !== null && $magicLink->redirect_url != '') {

return $magicLink->redirect_url;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Models/MagicLink.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ class MagicLink extends Model
{
public function user()
{
return $this->hasOne(config('auth.providers.users.model'), config('magiclink.user_primarykey', 'id'), 'users_id');
return $this->hasOne(config(config('magicklink.auth_provider').'.model'), config('magiclink.user_primarykey', 'id'), 'users_id');
}
}
15 changes: 15 additions & 0 deletions src/config/magiclink.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,21 @@
<?php

return [
/*
|--------------------------------------------------------------------------
| Application auth provider
|--------------------------------------------------------------------------
|
| By default users is auth provider (can be found at config/auth with providers key).
| If you want magick link for other auth guard you can change that here.
|
|
*/

'auth_provider' => 'users',

'auth_guard' => 'web',

/*
|--------------------------------------------------------------------------
| Application magiclink Table
Expand Down
1 change: 1 addition & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ protected function getEnvironmentSetUp($app)
{
$app['config']->set('app.key', 'base64:mJlbzP1TMXUPouK3KK6e9zS/VvxtWTfzfVlkn1JTqpM=');
$app['config']->set('auth.providers.users.model', 'Cesargb\MagicLink\Test\User');
$app['config']->set('magiclink.auth_provider', 'users');
$app['config']->set('database.default', 'sqlite');
$app['config']->set('database.connections.sqlite', [
'driver' => 'sqlite',
Expand Down