-
-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
update migration and add api login route
- Loading branch information
1 parent
e077f9e
commit 7d0a59c
Showing
2 changed files
with
46 additions
and
13 deletions.
There are no files selected for viewing
28 changes: 28 additions & 0 deletions
28
database/migrations/2024_04_06_051250_add_expires_at_to_personal_access_tokens.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
|
||
use Illuminate\Database\Migrations\Migration; | ||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Support\Facades\Schema; | ||
|
||
return new class extends Migration | ||
{ | ||
/** | ||
* Run the migrations. | ||
*/ | ||
public function up(): void | ||
{ | ||
Schema::table('personal_access_tokens', function (Blueprint $table) { | ||
$table->timestamp('expires_at')->nullable()->after('last_used_at'); | ||
}); | ||
} | ||
|
||
/** | ||
* Reverse the migrations. | ||
*/ | ||
public function down(): void | ||
{ | ||
Schema::table('personal_access_tokens', function (Blueprint $table) { | ||
$table->dropColumn('expires_at'); | ||
}); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,24 @@ | ||
<?php | ||
|
||
use App\Models\User; | ||
use Illuminate\Http\Request; | ||
use Illuminate\Support\Facades\Route; | ||
use Illuminate\Support\Facades\Hash; | ||
use Illuminate\Validation\ValidationException; | ||
|
||
/* | ||
|-------------------------------------------------------------------------- | ||
| API Routes | ||
|-------------------------------------------------------------------------- | ||
| | ||
| Here is where you can register API routes for your application. These | ||
| routes are loaded by the RouteServiceProvider within a group which | ||
| is assigned the "api" middleware group. Enjoy building your API! | ||
| | ||
*/ | ||
Route::post('/login', function (Request $request) { | ||
$request->validate([ | ||
'email' => 'required|email', | ||
'password' => 'required', | ||
'device_name' => 'required', | ||
]); | ||
|
||
Route::middleware('auth:sanctum')->get('/user', function (Request $request) { | ||
return $request->user(); | ||
$user = User::where('email', $request->email)->first(); | ||
|
||
if (! $user || ! Hash::check($request->password, $user->password)) { | ||
throw ValidationException::withMessages([ | ||
'email' => ['The provided credentials are incorrect.'], | ||
]); | ||
} | ||
|
||
return $user->createToken($request->device_name)->plainTextToken; | ||
}); |