Created
April 9, 2020 08:41
-
-
Save loicgeek/42df57cc635e412d262cb13813af5b8c to your computer and use it in GitHub Desktop.
This file contains hidden or 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
<?php | |
use Illuminate\Http\Request; | |
use Illuminate\Support\Facades\Route; | |
/* | |
|-------------------------------------------------------------------------- | |
| API Routes | |
|-------------------------------------------------------------------------- | |
| | |
| Here is where you can register API routes for your application. These | |
| routes are loaded by the RouteServiceProvider within a group which | |
| is assigned the "api" middleware group. Enjoy building your API! | |
| | |
*/ | |
Route::middleware('auth:api')->get('/user', function (Request $request) { | |
return $request->user(); | |
}); | |
Route::post('auth/login','Api\AuthController@login'); | |
Route::post('auth/register','Api\AuthController@register'); | |
Route::post('auth/logout','Api\AuthController@logout'); | |
Route::post('auth/refresh','Api\AuthController@refresh'); | |
Route::post('auth/social/{provider}','Api\AuthController@socialLogin'); | |
This file contains hidden or 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
<?php | |
namespace App\Http\Controllers\Api; | |
use App\Http\Controllers\Controller; | |
use App\Models\User; | |
use Illuminate\Http\Request; | |
use Laravel\Socialite\Facades\Socialite; | |
use Illuminate\Support\Facades\Auth; | |
use Tymon\JWTAuth\Facades\JWTAuth; | |
class AuthController extends Controller | |
{ | |
public function __construct() | |
{ | |
$this->middleware('auth:api',['except'=>['login','register','socialLogin']]); | |
} | |
public function login(Request $request){ | |
$this->validate($request,[ | |
"email"=>"required", | |
"password"=>"required" | |
]); | |
$credentials = $request->only(['email','password']); | |
if($token = $this->guard()->attempt($credentials)){ | |
return $this->respondWithToken($token); | |
} | |
return response()->json(['error'=>'Unauthorized'],403); | |
} | |
public function register(Request $request){ | |
$this->validate($request,[ | |
"name"=>"required", | |
"email"=>"required|email|unique:users", | |
"password"=>"required|min:6" | |
]); | |
$credentials = $request->only(['name','email','password']); | |
$user = new User($credentials); | |
$user->save(); | |
return $this->respondWithToken(JWTAuth::fromUser($user)); | |
} | |
public function logout(){ | |
$this->guard()->logout(); | |
return response()->json(['message'=>'Successfully logged out'],403); | |
} | |
public function refresh(){ | |
return $this->respondWithToken($this->guard()->refresh()); | |
} | |
public function socialLogin($provider,Request $request){ | |
$user = Socialite::driver($provider)->userFromToken($request->get('access_token')); | |
if(!$user->getEmail()){ | |
return response()->json(['error'=>'You must give access to your email'],403); | |
} | |
$exist = User::query()->where(['email'=>$user->getEmail()])->first(); | |
if(!$exist){ | |
User::create([ | |
'name' => $user->getName(), | |
'email' => $user->getEmail(), | |
'provider_id' => $user->getId(), | |
'provider' => $provider, | |
]); | |
} | |
return $this->respondWithToken(JWTAuth::fromUser($user)); | |
} | |
public function respondWithToken($token){ | |
return response()->json([ | |
'access_token'=>$token, | |
'token_type'=>'Bearer', | |
'expires_in'=>$this->guard()->factory()->getTTL()*60 | |
]); | |
} | |
public function guard(){ | |
return Auth::guard(); | |
} | |
} |
This file contains hidden or 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
<?php | |
namespace App\Http\Controllers\Api; | |
use App\Http\Controllers\Controller; | |
use App\Models\User; | |
use Illuminate\Http\Request; | |
use Laravel\Socialite\Facades\Socialite; | |
use Illuminate\Support\Facades\Auth; | |
use Tymon\JWTAuth\Facades\JWTAuth; | |
class AuthController extends Controller | |
{ | |
public function __construct() | |
{ | |
$this->middleware('auth:api',['except'=>['login','register','socialLogin']]); | |
} | |
public function login(Request $request){ | |
$this->validate($request,[ | |
"email"=>"required", | |
"password"=>"required" | |
]); | |
$credentials = $request->only(['email','password']); | |
if($token = $this->guard()->attempt($credentials)){ | |
return $this->respondWithToken($token); | |
} | |
return response()->json(['error'=>'Unauthorized'],403); | |
} | |
public function register(Request $request){ | |
$this->validate($request,[ | |
"name"=>"required", | |
"email"=>"required|email|unique:users", | |
"password"=>"required|min:6" | |
]); | |
$credentials = $request->only(['name','email','password']); | |
$user = new User($credentials); | |
$user->save(); | |
return $this->respondWithToken(JWTAuth::fromUser($user)); | |
} | |
public function logout(){ | |
$this->guard()->logout(); | |
return response()->json(['message'=>'Successfully logged out'],403); | |
} | |
public function refresh(){ | |
return $this->respondWithToken($this->guard()->refresh()); | |
} | |
public function socialLogin($provider,Request $request){ | |
$user = Socialite::driver($provider)->userFromToken($request->get('access_token')); | |
if(!$user->getEmail()){ | |
return response()->json(['error'=>'You must give access to your email'],403); | |
} | |
$exist = User::query()->where(['email'=>$user->getEmail()])->first(); | |
if(!$exist){ | |
User::create([ | |
'name' => $user->getName(), | |
'email' => $user->getEmail(), | |
'provider_id' => $user->getId(), | |
'provider' => $provider, | |
]); | |
} | |
return $this->respondWithToken(JWTAuth::fromUser($user)); | |
} | |
public function respondWithToken($token){ | |
return response()->json([ | |
'access_token'=>$token, | |
'token_type'=>'Bearer', | |
'expires_in'=>$this->guard()->factory()->getTTL()*60 | |
]); | |
} | |
public function guard(){ | |
return Auth::guard(); | |
} | |
} |
This file contains hidden or 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
<?php | |
namespace App\Models; | |
use Illuminate\Foundation\Auth\User as Authenticatable; | |
use Illuminate\Notifications\Notifiable; | |
use Illuminate\Support\Facades\Hash; | |
use Illuminate\Support\Str; | |
use Tymon\JWTAuth\Contracts\JWTSubject; | |
class User extends Authenticatable implements JWTSubject | |
{ | |
use Notifiable; | |
/** | |
* The attributes that are mass assignable. | |
* | |
* @var array | |
*/ | |
protected $fillable = [ | |
'name', 'email', 'password','provider','provider_id' | |
]; | |
/** | |
* The attributes that should be hidden for arrays. | |
* | |
* @var array | |
*/ | |
protected $hidden = [ | |
'password', 'remember_token', | |
]; | |
/** | |
* The attributes that should be cast to native types. | |
* | |
* @var array | |
*/ | |
protected $casts = [ | |
'email_verified_at' => 'datetime', | |
]; | |
protected static function boot() | |
{ | |
parent::boot(); | |
static::creating(function ($model) { | |
$model->{$model->getKeyName()} = (string) Str::uuid(); | |
}); | |
} | |
public function getJWTIdentifier(){ | |
return $this->getKey(); | |
} | |
/** | |
* @inheritDoc | |
*/ | |
public function getJWTCustomClaims() | |
{ | |
return []; | |
} | |
public function setPasswordAttribute($value){ | |
$this->attributes['password'] = Hash::make($value); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment