Skip to content

Commit 0df16af

Browse files
committed
Laravel 10 REST API using Passport Authentication
0 parents  commit 0df16af

File tree

84 files changed

+12498
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

84 files changed

+12498
-0
lines changed

.editorconfig

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
root = true
2+
3+
[*]
4+
charset = utf-8
5+
end_of_line = lf
6+
indent_size = 4
7+
indent_style = space
8+
insert_final_newline = true
9+
trim_trailing_whitespace = true
10+
11+
[*.md]
12+
trim_trailing_whitespace = false
13+
14+
[*.{yml,yaml}]
15+
indent_size = 2
16+
17+
[docker-compose.yml]
18+
indent_size = 4

.env.example

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
APP_NAME=Laravel
2+
APP_ENV=local
3+
APP_KEY=
4+
APP_DEBUG=true
5+
APP_URL=http://localhost
6+
7+
LOG_CHANNEL=stack
8+
LOG_DEPRECATIONS_CHANNEL=null
9+
LOG_LEVEL=debug
10+
11+
DB_CONNECTION=mysql
12+
DB_HOST=127.0.0.1
13+
DB_PORT=3306
14+
DB_DATABASE=laravel
15+
DB_USERNAME=root
16+
DB_PASSWORD=
17+
18+
BROADCAST_DRIVER=log
19+
CACHE_DRIVER=file
20+
FILESYSTEM_DISK=local
21+
QUEUE_CONNECTION=sync
22+
SESSION_DRIVER=file
23+
SESSION_LIFETIME=120
24+
25+
MEMCACHED_HOST=127.0.0.1
26+
27+
REDIS_HOST=127.0.0.1
28+
REDIS_PASSWORD=null
29+
REDIS_PORT=6379
30+
31+
MAIL_MAILER=smtp
32+
MAIL_HOST=mailpit
33+
MAIL_PORT=1025
34+
MAIL_USERNAME=null
35+
MAIL_PASSWORD=null
36+
MAIL_ENCRYPTION=null
37+
MAIL_FROM_ADDRESS="hello@example.com"
38+
MAIL_FROM_NAME="${APP_NAME}"
39+
40+
AWS_ACCESS_KEY_ID=
41+
AWS_SECRET_ACCESS_KEY=
42+
AWS_DEFAULT_REGION=us-east-1
43+
AWS_BUCKET=
44+
AWS_USE_PATH_STYLE_ENDPOINT=false
45+
46+
PUSHER_APP_ID=
47+
PUSHER_APP_KEY=
48+
PUSHER_APP_SECRET=
49+
PUSHER_HOST=
50+
PUSHER_PORT=443
51+
PUSHER_SCHEME=https
52+
PUSHER_APP_CLUSTER=mt1
53+
54+
VITE_APP_NAME="${APP_NAME}"
55+
VITE_PUSHER_APP_KEY="${PUSHER_APP_KEY}"
56+
VITE_PUSHER_HOST="${PUSHER_HOST}"
57+
VITE_PUSHER_PORT="${PUSHER_PORT}"
58+
VITE_PUSHER_SCHEME="${PUSHER_SCHEME}"
59+
VITE_PUSHER_APP_CLUSTER="${PUSHER_APP_CLUSTER}"

.gitattributes

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
* text=auto eol=lf
2+
3+
*.blade.php diff=html
4+
*.css diff=css
5+
*.html diff=html
6+
*.md diff=markdown
7+
*.php diff=php
8+
9+
/.github export-ignore
10+
CHANGELOG.md export-ignore
11+
.styleci.yml export-ignore

.gitignore

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/.phpunit.cache
2+
/node_modules
3+
/public/build
4+
/public/hot
5+
/public/storage
6+
/storage/*.key
7+
/vendor
8+
.env
9+
.env.backup
10+
.env.production
11+
.phpunit.result.cache
12+
Homestead.json
13+
Homestead.yaml
14+
auth.json
15+
npm-debug.log
16+
yarn-error.log
17+
/.fleet
18+
/.idea
19+
/.vscode

.rnd

1 KB
Binary file not shown.

README.md

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Laravel 10 REST API using Passport Authentication
2+
Learn how to develop Laravel 10 REST API using Passport authentication application
3+
4+
> The complete step by step guide is available on my blog. [AllPHPTricks Laravel 10 Passport API authentication](https://www.allphptricks.com/laravel-10-rest-api-using-passport-authentication/)
5+
6+
## Blog
7+
https://www.allphptricks.com/
8+
9+
10+
## Installation
11+
Make sure that you have setup the environment properly. You will need minimum PHP 8.1, PHP’s Sodium Extension, MySQL/MariaDB, and composer.
12+
13+
1. Download the project (or clone using GIT)
14+
2. Copy `.env.example` into `.env` and configure your database credentials
15+
3. Go to the project's root directory using terminal window/command prompt
16+
4. Run `composer install`
17+
5. Set the application key by running `php artisan key:generate --ansi`
18+
6. Run migrations `php artisan migrate`
19+
7. Start local server by executing `php artisan serve`

app/Console/Kernel.php

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace App\Console;
4+
5+
use Illuminate\Console\Scheduling\Schedule;
6+
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
7+
8+
class Kernel extends ConsoleKernel
9+
{
10+
/**
11+
* Define the application's command schedule.
12+
*/
13+
protected function schedule(Schedule $schedule): void
14+
{
15+
// $schedule->command('inspire')->hourly();
16+
}
17+
18+
/**
19+
* Register the commands for the application.
20+
*/
21+
protected function commands(): void
22+
{
23+
$this->load(__DIR__.'/Commands');
24+
25+
require base_path('routes/console.php');
26+
}
27+
}

app/Exceptions/Handler.php

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
namespace App\Exceptions;
4+
5+
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
6+
use Throwable;
7+
8+
class Handler extends ExceptionHandler
9+
{
10+
/**
11+
* The list of the inputs that are never flashed to the session on validation exceptions.
12+
*
13+
* @var array<int, string>
14+
*/
15+
protected $dontFlash = [
16+
'current_password',
17+
'password',
18+
'password_confirmation',
19+
];
20+
21+
/**
22+
* Register the exception handling callbacks for the application.
23+
*/
24+
public function register(): void
25+
{
26+
$this->reportable(function (Throwable $e) {
27+
//
28+
});
29+
}
30+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
<?php
2+
3+
namespace App\Http\Controllers\Auth;
4+
5+
use App\Models\User;
6+
use Illuminate\Http\Request;
7+
use App\Http\Controllers\Controller;
8+
use Illuminate\Support\Facades\Hash;
9+
use Validator;
10+
11+
class LoginRegisterController extends Controller
12+
{
13+
/**
14+
* Register a new user.
15+
*
16+
* @param \Illuminate\Http\Request $request
17+
* @return \Illuminate\Http\Response
18+
*/
19+
public function register(Request $request)
20+
{
21+
$validate = Validator::make($request->all(), [
22+
'name' => 'required|string|max:250',
23+
'email' => 'required|string|email:rfc,dns|max:250|unique:users,email',
24+
'password' => 'required|string|min:8|confirmed'
25+
]);
26+
27+
if($validate->fails()){
28+
return response()->json([
29+
'status' => 'failed',
30+
'message' => 'Validation Error!',
31+
'data' => $validate->errors(),
32+
], 403);
33+
}
34+
35+
$user = User::create([
36+
'name' => $request->name,
37+
'email' => $request->email,
38+
'password' => Hash::make($request->password)
39+
]);
40+
41+
$data['token'] = $user->createToken($request->email)->accessToken;
42+
$data['user'] = $user;
43+
44+
$response = [
45+
'status' => 'success',
46+
'message' => 'User is created successfully.',
47+
'data' => $data,
48+
];
49+
50+
return response()->json($response, 201);
51+
}
52+
53+
/**
54+
* Authenticate the user.
55+
*
56+
* @param \Illuminate\Http\Request $request
57+
* @return \Illuminate\Http\Response
58+
*/
59+
public function login(Request $request)
60+
{
61+
$validate = Validator::make($request->all(), [
62+
'email' => 'required|string|email',
63+
'password' => 'required|string'
64+
]);
65+
66+
if($validate->fails()){
67+
return response()->json([
68+
'status' => 'failed',
69+
'message' => 'Validation Error!',
70+
'data' => $validate->errors(),
71+
], 403);
72+
}
73+
74+
// Check email exist
75+
$user = User::where('email', $request->email)->first();
76+
77+
// Check password
78+
if(!$user || !Hash::check($request->password, $user->password)) {
79+
return response()->json([
80+
'status' => 'failed',
81+
'message' => 'Invalid credentials'
82+
], 401);
83+
}
84+
85+
$data['token'] = $user->createToken($request->email)->accessToken;
86+
$data['user'] = $user;
87+
88+
$response = [
89+
'status' => 'success',
90+
'message' => 'User is logged in successfully.',
91+
'data' => $data,
92+
];
93+
94+
return response()->json($response, 200);
95+
}
96+
97+
/**
98+
* Log out the user from application.
99+
*
100+
* @param \Illuminate\Http\Request $request
101+
* @return \Illuminate\Http\Response
102+
*/
103+
public function logout(Request $request)
104+
{
105+
auth()->user()->tokens()->delete();
106+
return response()->json([
107+
'status' => 'success',
108+
'message' => 'User is logged out successfully'
109+
], 200);
110+
}
111+
}

app/Http/Controllers/Controller.php

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
namespace App\Http\Controllers;
4+
5+
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
6+
use Illuminate\Foundation\Validation\ValidatesRequests;
7+
use Illuminate\Routing\Controller as BaseController;
8+
9+
class Controller extends BaseController
10+
{
11+
use AuthorizesRequests, ValidatesRequests;
12+
}

0 commit comments

Comments
 (0)