Skip to content

Commit 65a8e9f

Browse files
committed
Added: Laratrust for multiauth. Validation 404, 403 Page
1 parent 7525d8a commit 65a8e9f

File tree

20 files changed

+896
-21
lines changed

20 files changed

+896
-21
lines changed

backend/app/Http/Controllers/Api/LoginController.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,24 @@ public function __invoke(Request $request)
3535
if(!$token = JWTAuth::attempt($credentials)) {
3636
return response()->json([
3737
'success' => false,
38-
'message' => 'Email atau Password Anda salah'
38+
'message' => 'Your email or password is wrong!'
3939
], 401);
4040
}
4141

42+
$user = auth()->user();
43+
44+
$role = '';
45+
if ($user->hasRole('admin')) {
46+
$role = 'admin';
47+
} else if ($user->hasRole('user')) {
48+
$role = 'user';
49+
}
50+
4251
//if auth success
4352
return response()->json([
4453
'success' => true,
45-
'user' => auth()->user(),
54+
'user' => $user,
55+
'role' => $role,
4656
'token' => $token
4757
], 200);
4858
}

backend/app/Http/Controllers/Api/RegisterController.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,14 @@ public function __invoke(Request $request)
3636
'password' => bcrypt($request->password)
3737
]);
3838

39+
$user->addRole($request->role);
40+
3941
//return response JSON user is created
4042
if($user) {
4143
return response()->json([
4244
'success' => true,
43-
'user' => $user,
45+
'user' => $user,
46+
'role' => $request->role
4447
], 201);
4548
}
4649

backend/app/Models/Permission.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace App\Models;
4+
5+
use Laratrust\Models\Permission as PermissionModel;
6+
7+
class Permission extends PermissionModel
8+
{
9+
public $guarded = [];
10+
}

backend/app/Models/Role.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace App\Models;
4+
5+
use Laratrust\Models\Role as RoleModel;
6+
7+
class Role extends RoleModel
8+
{
9+
public $guarded = [];
10+
}

backend/app/Models/User.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,13 @@
88
use Illuminate\Foundation\Auth\User as Authenticatable;
99
use Illuminate\Notifications\Notifiable;
1010
use Laravel\Sanctum\HasApiTokens;
11+
use Laratrust\Contracts\LaratrustUser;
12+
use Laratrust\Traits\HasRolesAndPermissions;
1113

12-
class User extends Authenticatable implements JWTSubject
14+
15+
class User extends Authenticatable implements JWTSubject, LaratrustUser
1316
{
17+
use HasRolesAndPermissions;
1418
use HasApiTokens, HasFactory, Notifiable;
1519

1620
/**

backend/composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
"laravel/framework": "^10.0",
1111
"laravel/sanctum": "^3.2",
1212
"laravel/tinker": "^2.8",
13+
"santigarcor/laratrust": "^8.1",
1314
"tymon/jwt-auth": "^2.0"
1415
},
1516
"require-dev": {

backend/composer.lock

Lines changed: 126 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)