Browse Source

:recycle: Redefine the UserProvider with tokens

master
Clement Desmidt 4 months ago
parent
commit
c75081cf09

+ 1
- 1
app/Http/Controllers/LinksController.php View File

@@ -13,7 +13,7 @@ class LinksController extends Controller
public function add(Request $request)
{
/** @var User $user */
$user = $request->user();
$user = $request->user()->user;
$email = $user->email;

$url = $request->input('u');

+ 2
- 2
app/Http/Controllers/UsersController.php View File

@@ -34,8 +34,8 @@ class UsersController extends Controller
$user->save();
}

if ($user->confirmed ) {
return response()->json(['error' => 'Your account already exists'], 422);
if ( $user->confirmed ) {
return response()->json(['error' => 'Your account already exists.'], 422);
}

Mail::to($user->email)->send(new Confirmation($user));

+ 1
- 1
app/Http/Middleware/Authenticate.php View File

@@ -28,7 +28,7 @@ class Authenticate extends \Illuminate\Auth\Middleware\Authenticate
return response('Unauthorized.', 401);
}

if (! $this->auth->user()->confirmed) {
if (! $this->auth->user()->user->confirmed) {
return response('Unauthorized.', 401);
}


+ 5
- 6
app/Providers/AuthServiceProvider.php View File

@@ -2,11 +2,9 @@

namespace App\Providers;

use App\User;
use App\Services\Auth\TokenGuard;
use App\Token;
use Illuminate\Http\Request;
use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;
use Illuminate\Support\Facades\Auth;

class AuthServiceProvider extends ServiceProvider
{
@@ -28,10 +26,11 @@ class AuthServiceProvider extends ServiceProvider
{
$this->registerPolicies();

$this->app['auth']->viaRequest('custom', function (Request $request) {
if ($request->input('token')) {
return User::where('token', $request->input('token'))->first();
$this->app['auth']->viaRequest('custom', static function (Request $request) {
if ($request->input('api_token')) {
return Token::where('api_token', $request->input('api_token'))->first();
}
return null;
});
}
}

+ 1
- 1
app/Services/Auth/TokenGuard.php View File

@@ -84,7 +84,7 @@ class TokenGuard implements Guard
*/
public function validate(array $credentials = [])
{
if (!array_key_exists('token', $credentials)) {
if (!array_key_exists('api_token', $credentials)) {
return false;
}


+ 80
- 1
app/Token.php View File

@@ -2,9 +2,88 @@

namespace App;

use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

class Token extends Model
class Token extends Model implements Authenticatable
{
protected $table = 'tokens';

public function user(): BelongsTo
{
return $this->belongsTo(User::class);
}

/**
* @return string
*
* @throws \Exception
*/
public static function generateToken()
{
return bin2hex(random_bytes(72));
}

/**
* Get the name of the unique identifier for the user.
*
* @return string
*/
public function getAuthIdentifierName()
{
return 'api_token';
}

/**
* Get the unique identifier for the user.
*
* @return mixed
*/
public function getAuthIdentifier()
{
return $this->{$this->getAuthIdentifierName()};
}

/**
* Get the password for the user.
*
* @return string
*/
public function getAuthPassword()
{
return $this->api_token;
}

/**
* Get the token value for the "remember me" session.
*
* @return string
*/
public function getRememberToken()
{
return null;
}

/**
* Set the token value for the "remember me" session.
*
* @param string $value
*
* @return void
*/
public function setRememberToken($value)
{
return null;
}

/**
* Get the column name for the "remember me" token.
*
* @return string
*/
public function getRememberTokenName()
{
return null;
}
}

+ 2
- 76
app/User.php View File

@@ -2,12 +2,10 @@

namespace App;

use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;

class User extends Model implements Authenticatable
class User extends Model
{

/**
* The table associated with the model.
*
@@ -22,78 +20,6 @@ class User extends Model implements Authenticatable
];

/**
* @return string
*
* @throws \Exception
*/
public static function generateToken()
{
return bin2hex(random_bytes(72));
}

/**
* Get the name of the unique identifier for the user.
*
* @return string
*/
public function getAuthIdentifierName()
{
return 'api_token';
}

/**
* Get the unique identifier for the user.
*
* @return mixed
*/
public function getAuthIdentifier()
{
return $this->{$this->getAuthIdentifierName()};
}

/**
* Get the password for the user.
*
* @return string
*/
public function getAuthPassword()
{
return $this->api_token;
}

/**
* Get the token value for the "remember me" session.
*
* @return string
*/
public function getRememberToken()
{
return null;
}

/**
* Set the token value for the "remember me" session.
*
* @param string $value
*
* @return void
*/
public function setRememberToken($value)
{
return null;
}

/**
* Get the column name for the "remember me" token.
*
* @return string
*/
public function getRememberTokenName()
{
return null;
}

/**
* @return Token[]
*/
public function getTokens()
@@ -111,7 +37,7 @@ class User extends Model implements Authenticatable
try {
$new_token = new Token();
$new_token->user_id = $this->id;
$new_token->api_token = self::generateToken();
$new_token->api_token = Token::generateToken();
$new_token->token_name = $name;

$new_token->save();

+ 2
- 2
config/auth.php View File

@@ -15,7 +15,7 @@ return [

'defaults' => [
'guard' => 'api',
'passwords' => 'users',
'passwords' => 'tokens',
],

/*
@@ -67,7 +67,7 @@ return [
'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => App\User::class,
'model' => App\Token::class,
],

// 'users' => [

+ 1
- 1
database/factories/UserFactory.php View File

@@ -5,6 +5,6 @@ use Faker\Generator as Faker;
$factory->define(App\User::class, function (Faker $faker) {
return [
'email' => $faker->unique()->safeEmail,
'token' => \App\User::generateToken(),
'token' => \App\Token::generateToken(),
];
});

Loading…
Cancel
Save