-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
210 additions
and
0 deletions.
There are no files selected for viewing
210 changes: 210 additions & 0 deletions
210
sqlphp/developer-notes/php/laravel/laravel10 -Sanctum-Auth-Tested.txt
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,210 @@ | ||
|
||
>>>> Laravel 10 Sanctum Auth Tested <<<<< | ||
|
||
|
||
--------------------------------------------------------------------------------------------------------- | ||
|
||
key: | ||
|
||
--------------------------------------------------------------------------------------------------------- | ||
--------------------------------------------------------------------------------------------------------- | ||
|
||
--------------------------------------------------------------------------------------------------------- | ||
|
||
>> Laravel 10 Sanctum Auth Working | ||
|
||
|
||
api.php | ||
|
||
<?php | ||
|
||
use Illuminate\Http\Request; | ||
use Illuminate\Support\Facades\Route; | ||
use App\Http\Controllers\Api\AuthController; | ||
|
||
/* | ||
|-------------------------------------------------------------------------- | ||
| API Routes | ||
|-------------------------------------------------------------------------- | ||
| | ||
| Here is where you can register API routes for your application. These | ||
| routes are loaded by the RouteServiceProvider and all of them will | ||
| be assigned to the "api" middleware group. Make something great! | ||
| | ||
*/ | ||
|
||
Route::post('/auth/register', [AuthController::class, 'createUser']); | ||
Route::post('/auth/login', [AuthController::class, 'loginUser']); | ||
|
||
|
||
Route::middleware('auth:sanctum')->get('/user', function (Request $request) { | ||
return $request->user(); | ||
}); | ||
|
||
Route::apiResource('posts', PostController::class)->middleware('auth:sanctum'); | ||
|
||
|
||
|
||
//AuthController.php | ||
|
||
<?php | ||
|
||
namespace App\Http\Controllers\Api; | ||
|
||
use App\Models\User; | ||
use Illuminate\Http\Request; | ||
use App\Http\Controllers\Controller; | ||
use Illuminate\Support\Facades\Auth; | ||
use Illuminate\Support\Facades\Hash; | ||
use Illuminate\Support\Facades\Validator; | ||
|
||
class AuthController extends Controller | ||
{ | ||
|
||
public function createUser(Request $request) | ||
{ | ||
try { | ||
//Validated | ||
$validateUser = Validator::make($request->all(), | ||
[ | ||
'name' => 'required', | ||
'email' => 'required|email|unique:users,email', | ||
'password' => 'required' | ||
]); | ||
|
||
if($validateUser->fails()){ | ||
return response()->json([ | ||
'status' => false, | ||
'message' => 'validation error', | ||
'errors' => $validateUser->errors() | ||
], 401); | ||
} | ||
|
||
$user = User::create([ | ||
'name' => $request->name, | ||
'email' => $request->email, | ||
'password' => Hash::make($request->password) | ||
]); | ||
|
||
return response()->json([ | ||
'status' => true, | ||
'message' => 'User Created Successfully', | ||
'token' => $user->createToken("API TOKEN")->plainTextToken | ||
], 200); | ||
|
||
} catch (\Throwable $th) { | ||
return response()->json([ | ||
'status' => false, | ||
'message' => $th->getMessage() | ||
], 500); | ||
} | ||
} | ||
|
||
|
||
public function loginUser(Request $request) | ||
{ | ||
try { | ||
$validateUser = Validator::make($request->all(), | ||
[ | ||
'email' => 'required|email', | ||
'password' => 'required' | ||
]); | ||
|
||
if($validateUser->fails()){ | ||
return response()->json([ | ||
'status' => false, | ||
'message' => 'validation error', | ||
'errors' => $validateUser->errors() | ||
], 401); | ||
} | ||
|
||
if(!Auth::attempt($request->only(['email', 'password']))){ | ||
return response()->json([ | ||
'status' => false, | ||
'message' => 'Email & Password does not match with our record.', | ||
], 401); | ||
} | ||
|
||
$user = User::where('email', $request->email)->first(); | ||
|
||
return response()->json([ | ||
'status' => true, | ||
'message' => 'User Logged In Successfully', | ||
'token' => $user->createToken("API TOKEN")->plainTextToken | ||
], 200); | ||
|
||
} catch (\Throwable $th) { | ||
return response()->json([ | ||
'status' => false, | ||
'message' => $th->getMessage() | ||
], 500); | ||
} | ||
} | ||
} | ||
|
||
|
||
|
||
//User.php | ||
|
||
<?php | ||
|
||
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 | ||
{ | ||
use HasApiTokens, HasFactory, Notifiable; | ||
|
||
/** | ||
* The attributes that are mass assignable. | ||
* | ||
* @var array<int, string> | ||
*/ | ||
protected $fillable = [ | ||
'name', | ||
'email', | ||
'password', | ||
]; | ||
|
||
/** | ||
* The attributes that should be hidden for serialization. | ||
* | ||
* @var array<int, string> | ||
*/ | ||
protected $hidden = [ | ||
'password', | ||
'remember_token', | ||
]; | ||
|
||
/** | ||
* The attributes that should be cast. | ||
* | ||
* @var array<string, string> | ||
*/ | ||
protected $casts = [ | ||
'email_verified_at' => 'datetime', | ||
'password' => 'hashed', | ||
]; | ||
} | ||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
--------------------------------------------------------------------------------------------------------- | ||
|
||
|
||
|