Skip to content
Open
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
53 changes: 46 additions & 7 deletions src/c2dl/app/Http/Controllers/Auth/LoginController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use App\Models\User;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use \Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use mysql_xdevapi\Exception;
use Illuminate\Http\Response as HttpResponse;
use Illuminate\Support\Facades\Auth;

class LoginController extends Controller
{
Expand Down Expand Up @@ -80,7 +81,7 @@ public function logout(Request $request)
*/
public function username()
{
return 'user';
return 'username';
}

/**
Expand All @@ -98,13 +99,49 @@ protected function validateLogin(Request $request)
$this->username() => 'required|string',
'password' => 'required|string',
]);
}
catch (\Illuminate\Validation\ValidationException $err) {
} catch (\Illuminate\Validation\ValidationException $err) {
$err->redirectTo = $this->redirectToFailed;
throw $err;
}
}

/**
* Login through /api/login.
*
* @param \Illuminate\Http\Request
*
* @return \Illuminate\Http\JsonResponse
*/
public function apiLogin(Request $request)
{
if ($this->attemptLogin($request)) {
$user = Auth::user();
$token = $user->createToken("{$request->username}_token");
$user->current_token = $token->accessToken->token;
$user->save();

return response()->json([
'name' => "{$request->username}_token",
'token' => $token->accessToken->token
]);
} else {
return response()->json([
'error' => 'Something went wrong.'
], HttpResponse::HTTP_FORBIDDEN);
}
}

/**
* Get a user by their token using /api/validate.
*
* @param \Illuminate\Http\Request
*/
public function userByToken(Request $request)
{
$user = User::where('current_token', $request->get('token'))->first();
return $user->toJson();
}

/**
* Handle a login request to the application.
*
Expand All @@ -123,8 +160,10 @@ public function login(Request $request)
// If the class is using the ThrottlesLogins trait, we can automatically throttle
// the login attempts for this application. We'll key this by the username and
// the IP address of the client making these requests into this application.
if (method_exists($this, 'hasTooManyLoginAttempts') &&
$this->hasTooManyLoginAttempts($request)) {
if (
method_exists($this, 'hasTooManyLoginAttempts') &&
$this->hasTooManyLoginAttempts($request)
) {
$this->fireLockoutEvent($request);

return $this->sendLockoutResponse($request);
Expand Down
9 changes: 5 additions & 4 deletions src/c2dl/app/Http/Controllers/Auth/RegisterController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use App\User;
use App\Models\User;
use Illuminate\Foundation\Auth\RegistersUsers;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;
Expand Down Expand Up @@ -59,7 +59,7 @@ public function showRegistrationForm()
protected function validator(array $data)
{
return Validator::make($data, [
'name' => ['required', 'string', 'max:255'],
'username' => ['required', 'string', 'max:255'],
'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
'password' => ['required', 'string', 'min:8', 'confirmed'],
]);
Expand All @@ -69,14 +69,15 @@ protected function validator(array $data)
* Create a new user instance after a valid registration.
*
* @param array $data
* @return \App\User
* @return \App\Models\User
*/
protected function create(array $data)
{
return User::create([
'name' => $data['name'],
'username' => $data['username'],
'email' => $data['email'],
'password' => Hash::make($data['password']),
'active' => 1
]);
}
}
4 changes: 2 additions & 2 deletions src/c2dl/app/Http/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class Kernel extends HttpKernel
\App\Http\Middleware\EncryptCookies::class,
\Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
\Illuminate\Session\Middleware\StartSession::class,
// \Illuminate\Session\Middleware\AuthenticateSession::class,
\Illuminate\Session\Middleware\AuthenticateSession::class,
\Illuminate\View\Middleware\ShareErrorsFromSession::class,
\App\Http\Middleware\VerifyCsrfToken::class,
\Illuminate\Routing\Middleware\SubstituteBindings::class,
Expand All @@ -42,7 +42,7 @@ class Kernel extends HttpKernel
\App\Http\Middleware\EncryptCookies::class,
\Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
\Illuminate\Session\Middleware\StartSession::class,
// \Illuminate\Session\Middleware\AuthenticateSession::class,
\Illuminate\Session\Middleware\AuthenticateSession::class,
\Illuminate\View\Middleware\ShareErrorsFromSession::class,
\App\Http\Middleware\VerifyCsrfToken::class,
\Illuminate\Routing\Middleware\SubstituteBindings::class,
Expand Down
16 changes: 12 additions & 4 deletions src/c2dl/app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,28 @@

namespace App\Models;

use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Sanctum\HasApiTokens;

class User extends Authenticatable
{
protected $connection = 'acc';
protected $table = 'acc_user';
protected $table = 'users';
protected $primaryKey = 'user_id';

use HasFactory, Notifiable;
use HasApiTokens, HasFactory, Notifiable;

/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name',
'username',
'email',
'password',
];

/**
Expand All @@ -30,6 +32,7 @@ class User extends Authenticatable
* @var array
*/
protected $hidden = [
'password',
'remember_token',
];

Expand All @@ -41,4 +44,9 @@ class User extends Authenticatable
protected $casts = [
'validate_at' => 'datetime',
];

public function currentAccessToken()
{
return $this->current_token;
}
}
6 changes: 5 additions & 1 deletion src/c2dl/app/Providers/AuthServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Providers;

use Illuminate\Auth\Notifications\ResetPassword;
use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;
use Illuminate\Support\Facades\Gate;

Expand All @@ -27,6 +28,9 @@ public function boot()
{
$this->registerPolicies();

//
// Properly format the button link in the email.
ResetPassword::createUrlUsing(function ($_user, string $token) {
return route('password.request', $token);
});
}
}
2 changes: 1 addition & 1 deletion src/c2dl/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"ext-dom": "*",
"fruitcake/laravel-cors": "^3.0",
"guzzlehttp/guzzle": "^7.2",
"laravel/framework": "^9.2",
"laravel/framework": "^9.3",
"laravel/tinker": "^2.7",
"laravel/sanctum": "^2.14.1",
"laravel/ui": "^3.0",
Expand Down
5 changes: 4 additions & 1 deletion src/c2dl/database/factories/UserFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,11 @@ class UserFactory extends Factory
public function definition()
{
return [
'name' => $this->faker->name,
'name' => $this->faker->name(),
'email' => $this->faker->unique()->safeEmail(),
'active' => false,
'validate_at' => now(),
'password' => '$2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi', // password
'remember_token' => Str::random(10),
];
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('password_resets', function (Blueprint $table) {
$table->string('email')->index();
$table->string('token');
$table->timestamp('created_at')->nullable();
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('password_resets');
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::connection('acc')->rename('acc_user', 'users');
Schema::connection('acc')->table('users', function (Blueprint $table) {
$table->dropColumn('created_at', 'updated_at');

$table->string('email')->unique()->comment('Unique user email');
$table->string('password')->comment('User chosen password');
});

// Had to split this into another call to `table` due to "duplicate column" errors.
Schema::connection('acc')->table('users', function (Blueprint $table) {
$table->timestamps();
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::connection('acc')->table('users', function (Blueprint $table) {
$table->dropColumn('email', 'password', 'created_at', 'updated_at');
});

Schema::connection('acc')->table('users', function (Blueprint $table) {
$table->dateTime('created_at')->useCurrent()->comment('Created');
$table->dateTime('updated_at')->nullable()->comment('Updated, null if not');
});

Schema::connection('acc')->rename('users', 'acc_user');
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
//
Schema::connection('acc')->table('users', function (Blueprint $table) {
$table->string('current_token', 64)->unique()->nullable();
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::connection('acc')->table('users', function (Blueprint $table) {
$table->dropColumn('current_token');
});
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::connection('acc')->table('users', function (Blueprint $table) {
$table->dropColumn('name');

$table->string('username', 64)->unique()->comment('Unique username');
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::connection('acc')->table('users', function (Blueprint $table) {
$table->dropColumn('username');

$table->string('name', 64)->unique()->comment('Unique user name');
});
}
};
Loading