This repository has been archived by the owner on Sep 12, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from FaZeRs/develop
API Authentication
- Loading branch information
Showing
42 changed files
with
11,380 additions
and
4,789 deletions.
There are no files selected for viewing
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
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 @@ | ||
preset: laravel |
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
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
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
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
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
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
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,31 @@ | ||
<?php | ||
|
||
namespace App\Http\Controllers\Api; | ||
|
||
use App\Models\User; | ||
use Illuminate\Http\Request; | ||
use App\Http\Controllers\Controller; | ||
use Illuminate\Support\Facades\Hash; | ||
|
||
class LoginController extends Controller | ||
{ | ||
public function login(Request $request) | ||
{ | ||
$this->validate($request, [ | ||
'email' => 'required|email', | ||
'password' => 'required', | ||
]); | ||
|
||
$user = User::where('email', $request->get('email'))->first(); | ||
|
||
if ($user) { | ||
if (Hash::check($request->get('password'), $user->password)) { | ||
$success['token'] = $user->createToken('Portfolio')->accessToken; | ||
|
||
return response()->json(['success' => $success], 200); | ||
} | ||
} | ||
|
||
return response()->json(['error' => 'These credentials do not match our records.'], 401); | ||
} | ||
} |
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,27 @@ | ||
<?php | ||
|
||
namespace App\Http\Controllers\Api; | ||
|
||
use App\Models\User; | ||
use Illuminate\Http\Request; | ||
use Illuminate\Validation\Rule; | ||
use App\Http\Controllers\Controller; | ||
|
||
class RegisterController extends Controller | ||
{ | ||
public function register(Request $request) | ||
{ | ||
$data = $this->validate($request, [ | ||
'name' => 'required|string|max:191', | ||
'email' => ['required', 'string', 'email', 'max:191', Rule::unique('users')], | ||
'password' => 'required|string|min:9|confirmed|strong_password', | ||
]); | ||
|
||
$data['password'] = bcrypt($data['password']); | ||
$user = User::create($data); | ||
$success['token'] = $user->createToken('Portfolio')->accessToken; | ||
$success['name'] = $user->name; | ||
|
||
return response()->json(['success' => $success], 200); | ||
} | ||
} |
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,29 @@ | ||
<?php | ||
|
||
namespace App\Http\Controllers\Api; | ||
|
||
use Illuminate\Support\Facades\DB; | ||
use App\Http\Controllers\Controller; | ||
|
||
class UserController extends Controller | ||
{ | ||
public function details() | ||
{ | ||
$user = auth()->user(); | ||
|
||
return response()->json(['details' => $user], 200); | ||
} | ||
|
||
public function logout() | ||
{ | ||
$accessToken = auth()->user()->token(); | ||
|
||
DB::table('oauth_refresh_tokens')->where('access_token_id', $accessToken->id)->update([ | ||
'revoked' => true, | ||
]); | ||
|
||
$accessToken->revoke(); | ||
|
||
return response()->json(['success' => 'You have successfully logged out'], 200); | ||
} | ||
} |
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,24 @@ | ||
<?php | ||
|
||
namespace App\Http\Middleware; | ||
|
||
use Closure; | ||
|
||
class AdminMiddleware | ||
{ | ||
/** | ||
* Handle an incoming request. User must be logged in to do admin check. | ||
* | ||
* @param \Illuminate\Http\Request $request | ||
* @param \Closure $next | ||
* @return mixed | ||
*/ | ||
public function handle($request, Closure $next) | ||
{ | ||
if (auth()->user()->isAdmin()) { | ||
return $next($request); | ||
} | ||
|
||
return response('Unauthorized.', 401); | ||
} | ||
} |
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,44 @@ | ||
<?php | ||
|
||
namespace App\Http\Middleware; | ||
|
||
use Closure; | ||
use Illuminate\Contracts\Auth\Factory as Auth; | ||
|
||
class Authenticate | ||
{ | ||
/** | ||
* The authentication guard factory instance. | ||
* | ||
* @var \Illuminate\Contracts\Auth\Factory | ||
*/ | ||
protected $auth; | ||
|
||
/** | ||
* Create a new middleware instance. | ||
* | ||
* @param \Illuminate\Contracts\Auth\Factory $auth | ||
* @return void | ||
*/ | ||
public function __construct(Auth $auth) | ||
{ | ||
$this->auth = $auth; | ||
} | ||
|
||
/** | ||
* Handle an incoming request. | ||
* | ||
* @param \Illuminate\Http\Request $request | ||
* @param \Closure $next | ||
* @param string|null $guard | ||
* @return mixed | ||
*/ | ||
public function handle($request, Closure $next, $guard = null) | ||
{ | ||
if ($this->auth->guard($guard)->guest()) { | ||
return response('Unauthorized.', 401); | ||
} | ||
|
||
return $next($request); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
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
Oops, something went wrong.