Commit ea0b1859 authored by Jacob Priddy's avatar Jacob Priddy 👌

start working on api auth

parent 3a1cbf46
<?php
namespace App;
use Illuminate\Foundation\Auth\User as Authenticatable;
class Door extends Authenticatable {
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'api_token',
];
}
<?php
namespace App\Http\Controllers\Auth;
use App\Http\Controllers\Controller;
use App\Providers\RouteServiceProvider;
use Illuminate\Foundation\Auth\ConfirmsPasswords;
class ConfirmPasswordController extends Controller
{
/*
|--------------------------------------------------------------------------
| Confirm Password Controller
|--------------------------------------------------------------------------
|
| This controller is responsible for handling password confirmations and
| uses a simple trait to include the behavior. You're free to explore
| this trait and override any functions that require customization.
|
*/
use ConfirmsPasswords;
/**
* Where to redirect users when the intended url fails.
*
* @var string
*/
protected $redirectTo = RouteServiceProvider::HOME;
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware('auth');
}
}
<?php
namespace App\Http\Controllers\Auth;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\SendsPasswordResetEmails;
class ForgotPasswordController extends Controller
{
/*
|--------------------------------------------------------------------------
| Password Reset Controller
|--------------------------------------------------------------------------
|
| This controller is responsible for handling password reset emails and
| includes a trait which assists in sending these notifications from
| your application to your users. Feel free to explore this trait.
|
*/
use SendsPasswordResetEmails;
}
<?php
namespace App\Http\Controllers\Auth;
use App\Http\Controllers\Controller;
use App\Providers\RouteServiceProvider;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
class LoginController extends Controller
{
/*
|--------------------------------------------------------------------------
| Login Controller
|--------------------------------------------------------------------------
|
| This controller handles authenticating users for the application and
| redirecting them to your home screen. The controller uses a trait
| to conveniently provide its functionality to your applications.
|
*/
use AuthenticatesUsers;
/**
* Where to redirect users after login.
*
* @var string
*/
protected $redirectTo = RouteServiceProvider::HOME;
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware('guest')->except('logout');
}
}
<?php
namespace App\Http\Controllers\Auth;
use App\Http\Controllers\Controller;
use App\Providers\RouteServiceProvider;
use App\User;
use Illuminate\Foundation\Auth\RegistersUsers;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;
class RegisterController extends Controller
{
/*
|--------------------------------------------------------------------------
| Register Controller
|--------------------------------------------------------------------------
|
| This controller handles the registration of new users as well as their
| validation and creation. By default this controller uses a trait to
| provide this functionality without requiring any additional code.
|
*/
use RegistersUsers;
/**
* Where to redirect users after registration.
*
* @var string
*/
protected $redirectTo = RouteServiceProvider::HOME;
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware('guest');
}
/**
* Get a validator for an incoming registration request.
*
* @param array $data
* @return \Illuminate\Contracts\Validation\Validator
*/
protected function validator(array $data)
{
return Validator::make($data, [
'name' => ['required', 'string', 'max:255'],
'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
'password' => ['required', 'string', 'min:8', 'confirmed'],
]);
}
/**
* Create a new user instance after a valid registration.
*
* @param array $data
* @return \App\User
*/
protected function create(array $data)
{
return User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => Hash::make($data['password']),
]);
}
}
<?php
namespace App\Http\Controllers\Auth;
use App\Http\Controllers\Controller;
use App\Providers\RouteServiceProvider;
use Illuminate\Foundation\Auth\ResetsPasswords;
class ResetPasswordController extends Controller
{
/*
|--------------------------------------------------------------------------
| Password Reset Controller
|--------------------------------------------------------------------------
|
| This controller is responsible for handling password reset requests
| and uses a simple trait to include this behavior. You're free to
| explore this trait and override any methods you wish to tweak.
|
*/
use ResetsPasswords;
/**
* Where to redirect users after resetting their password.
*
* @var string
*/
protected $redirectTo = RouteServiceProvider::HOME;
}
<?php
namespace App\Http\Controllers\Auth;
use App\Http\Controllers\Controller;
use App\Providers\RouteServiceProvider;
use Illuminate\Foundation\Auth\VerifiesEmails;
class VerificationController extends Controller
{
/*
|--------------------------------------------------------------------------
| Email Verification Controller
|--------------------------------------------------------------------------
|
| This controller is responsible for handling email verification for any
| user that recently registered with the application. Emails may also
| be re-sent if the user didn't receive the original email message.
|
*/
use VerifiesEmails;
/**
* Where to redirect users after verification.
*
* @var string
*/
protected $redirectTo = RouteServiceProvider::HOME;
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware('auth');
$this->middleware('signed')->only('verify');
$this->middleware('throttle:6,1')->only('verify', 'resend');
}
}
......@@ -41,6 +41,10 @@ class Kernel extends HttpKernel
'throttle:60,1',
'bindings',
],
'door' => [
'bindings',
],
];
/**
......
......@@ -2,11 +2,10 @@
namespace App\Providers;
use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider;
use Illuminate\Support\Facades\Route;
use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider;
class RouteServiceProvider extends ServiceProvider
{
class RouteServiceProvider extends ServiceProvider {
/**
* This namespace is applied to your controller routes.
*
......@@ -21,17 +20,14 @@ class RouteServiceProvider extends ServiceProvider
*
* @var string
*/
public const HOME = '/home';
public const HOME = '/';
/**
* Define your route model bindings, pattern filters, etc.
*
* @return void
*/
public function boot()
{
//
public function boot() {
parent::boot();
}
......@@ -40,13 +36,12 @@ class RouteServiceProvider extends ServiceProvider
*
* @return void
*/
public function map()
{
public function map() {
$this->mapDoorRoutes();
$this->mapApiRoutes();
$this->mapWebRoutes();
//
}
/**
......@@ -56,8 +51,7 @@ class RouteServiceProvider extends ServiceProvider
*
* @return void
*/
protected function mapWebRoutes()
{
protected function mapWebRoutes(): void {
Route::middleware('web')
->namespace($this->namespace)
->group(base_path('routes/web.php'));
......@@ -70,11 +64,20 @@ class RouteServiceProvider extends ServiceProvider
*
* @return void
*/
protected function mapApiRoutes()
{
Route::prefix('api')
->middleware('api')
protected function mapApiRoutes(): void {
Route::middleware('api')
->namespace($this->namespace)
->group(base_path('routes/api.php'));
}
/**
* Define the routes doors can and do use
*
* @return void
*/
protected function mapDoorRoutes(): void {
Route::middleware('door')
->namespace($this->namespace)
->group(base_path('routes/door.php'));
}
}
......@@ -2,12 +2,10 @@
namespace App;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
class User extends Authenticatable {
use Notifiable;
/**
......@@ -16,7 +14,9 @@ class User extends Authenticatable
* @var array
*/
protected $fillable = [
'name', 'email', 'password',
'name',
'email',
'password',
];
/**
......@@ -25,7 +25,9 @@ class User extends Authenticatable
* @var array
*/
protected $hidden = [
'password', 'remember_token',
'password',
'remember_token',
'api_token',
];
/**
......
......@@ -44,8 +44,14 @@ return [
'api' => [
'driver' => 'token',
'provider' => 'users',
'hash' => false,
'hash' => true,
],
'door' => [
'driver' => 'token',
'provider' => 'doors',
'hash' => true,
]
],
/*
......@@ -71,6 +77,11 @@ return [
'model' => App\User::class,
],
'doors' => [
'driver' => 'eloquent',
'model' => App\Door::class,
]
// 'users' => [
// 'driver' => 'database',
// 'table' => 'users',
......
......@@ -25,6 +25,7 @@ class CreateUsersTable extends Migration
$table->string('password');
// hashed
$table->string('doorcode');
$table->string('api_token')->unique()->nullable()->default(null);
$table->timestamp('expires_at')->nullable();
$table->rememberToken();
$table->timestamps();
......
......@@ -18,9 +18,9 @@ class CreateDoorsTable extends Migration
Schema::create('doors', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('location');
$table->string('name');
$table->string('name')->unique();
// hashed
$table->string('key');
$table->string('api_token')->unique();
$table->timestamps();
});
}
......
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateUserTokensTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('user_tokens', static function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('api_token');
$table->unsignedBigInteger('user_id');
$table->timestamp('expires_at')->nullable();
$table->foreign('user_id')->references('id')->on('users');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('user_tokens');
}
}
......@@ -2,6 +2,7 @@
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\UsersController;
/*
|--------------------------------------------------------------------------
......@@ -13,6 +14,18 @@ use Illuminate\Support\Facades\Route;
| is assigned the "api" middleware group. Enjoy building your API!
|
*/
Route::group(
[
'prefix' => 'users',
],
static function () {
Route::get('/', [UsersController::class, 'index']);
Route::post('/', [UsersController::class, 'store']);
Route::get('{userId}', [UsersController::class, 'get']);
Route::put('{userId}', [UsersController::class, 'update']);
Route::delete('{userId}', [UsersController::class, 'delete']);
}
);
Route::middleware('auth:api')->get('/user', function (Request $request) {
return $request->user();
......
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\UsersController;
/*
|--------------------------------------------------------------------------
......@@ -20,16 +19,3 @@ Route::get(
return ['api endpoint'];
}
);
Route::group(
[
'prefix' => 'users',
],
static function () {
Route::get('/', [UsersController::class, 'index']);
Route::post('/', [UsersController::class, 'store']);
Route::get('{userId}', [UsersController::class, 'get']);
Route::put('{userId}', [UsersController::class, 'update']);
Route::delete('{userId}', [UsersController::class, 'delete']);
}
);
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment