Skip to content

Commit b8bf4e9

Browse files
author
Ajay Marathe
committed
added auth
1 parent 3534de5 commit b8bf4e9

File tree

7 files changed

+1019
-31
lines changed

7 files changed

+1019
-31
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php
2+
3+
namespace App\Http\Controllers\API;
4+
5+
use Illuminate\Http\Request;
6+
use App\Http\Controllers\Controller;
7+
use Illuminate\Support\Facades\Auth;
8+
use App\User;
9+
use Validator;
10+
11+
class AuthController extends Controller
12+
{
13+
public $successStatus = 200;
14+
/**
15+
* login function
16+
*
17+
* @return \Illuminate\Http\Response
18+
*/
19+
public function login(){
20+
if(Auth::attempt(['email' => request('email'), 'password' => request('password')])){
21+
$user = Auth::user();
22+
$success['token'] = $user->createToken('MyApp')-> accessToken;
23+
return response()->json(['success' => $success], $this-> successStatus);
24+
}
25+
else{
26+
return response()->json(['error'=>'Unauthorised'], 401);
27+
}
28+
}
29+
/**
30+
* Register function
31+
*
32+
* @return \Illuminate\Http\Response
33+
*/
34+
public function register(Request $request)
35+
{
36+
$validator = Validator::make($request->all(), [
37+
'name' => 'required',
38+
'email' => 'required|email',
39+
'password' => 'required',
40+
'c_password' => 'required|same:password',
41+
]);
42+
43+
if ($validator->fails()) {
44+
return response()->json(['error'=>$validator->errors()], 401);
45+
}
46+
47+
$input = $request->all();
48+
$input['password'] = bcrypt($input['password']);
49+
$user = User::create($input);
50+
$success['token'] = $user->createToken('MyApp')-> accessToken;
51+
$success['name'] = $user->name;
52+
return response()->json(['success'=>$success], $this-> successStatus);
53+
}
54+
/**
55+
* details api
56+
*
57+
* @return \Illuminate\Http\Response
58+
*/
59+
public function details()
60+
{
61+
$user = Auth::user();
62+
return response()->json(['success' => $user], $this-> successStatus);
63+
}
64+
}

app/Providers/AuthServiceProvider.php

+4-3
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22

33
namespace App\Providers;
44

5-
use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;
5+
use Laravel\Passport\Passport;
66
use Illuminate\Support\Facades\Gate;
7+
use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;
78

89
class AuthServiceProvider extends ServiceProvider
910
{
@@ -13,7 +14,7 @@ class AuthServiceProvider extends ServiceProvider
1314
* @var array
1415
*/
1516
protected $policies = [
16-
// 'App\Model' => 'App\Policies\ModelPolicy',
17+
'App\Model' => 'App\Policies\ModelPolicy',
1718
];
1819

1920
/**
@@ -25,6 +26,6 @@ public function boot()
2526
{
2627
$this->registerPolicies();
2728

28-
//
29+
Passport::routes();
2930
}
3031
}

app/User.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22

33
namespace App;
44

5-
use Illuminate\Contracts\Auth\MustVerifyEmail;
6-
use Illuminate\Foundation\Auth\User as Authenticatable;
5+
use Laravel\Passport\HasApiTokens;
76
use Illuminate\Notifications\Notifiable;
7+
use Illuminate\Foundation\Auth\User as Authenticatable;
88

99
class User extends Authenticatable
1010
{
11-
use Notifiable;
11+
use HasApiTokens, Notifiable;
1212

1313
/**
1414
* The attributes that are mass assignable.

composer.json

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
"fideloper/proxy": "^4.0",
1313
"laravel/framework": "^6.2",
1414
"laravel/helpers": "^1.1",
15+
"laravel/passport": "^8.0",
1516
"laravel/tinker": "^2.0",
1617
"nesbot/carbon": "^2.27"
1718
},

0 commit comments

Comments
 (0)