Last active
January 7, 2016 16:22
-
-
Save JustSteveKing/f5192862e54d44c8c538 to your computer and use it in GitHub Desktop.
Using polymorphism in Laravel Auth
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
<?php | |
namespace App\Http\Controllers; | |
use Illuminate\Http\Request; | |
use App\Identity; | |
use App\Http\Requests; | |
use App\Http\Controllers\Controller; | |
class AccountController extends Controller { | |
public function __construct() { | |
$this->middleware('auth'); | |
parent::__construct(); | |
} | |
public function index(Identity $identity) { | |
return view('account.index', [ | |
"user" => $identity | |
]); | |
} | |
} |
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
<?php | |
// relevant parts | |
return [ | |
'defaults' => [ | |
'guard' => 'web', | |
'passwords' => 'users', | |
], | |
'guards' => [ | |
'web' => [ | |
'driver' => 'session', | |
'provider' => 'identity', | |
], | |
'api' => [ | |
'driver' => 'token', | |
'provider' => 'identity', | |
], | |
], | |
'providers' => [ | |
'identity' => [ | |
'driver' => 'eloquent', | |
'model' => App\Identity::class, | |
'table' => 'identity', | |
], | |
], | |
'passwords' => [ | |
'identity' => [ | |
'provider' => 'identity', | |
'email' => 'auth.emails.password', | |
'table' => 'password_resets', | |
'expire' => 60, | |
], | |
], | |
]; |
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
<?php | |
namespace App\Http\Controllers\Auth; | |
use App\Identity; | |
use App\Member; | |
use Validator; | |
use App\Http\Controllers\Controller; | |
use Illuminate\Foundation\Auth\ThrottlesLogins; | |
use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers; | |
class AuthController extends Controller | |
{ | |
/* | |
|-------------------------------------------------------------------------- | |
| Registration & Login Controller | |
|-------------------------------------------------------------------------- | |
| | |
| This controller handles the registration of new users, as well as the | |
| authentication of existing users. By default, this controller uses | |
| a simple trait to add these behaviors. Why don't you explore it? | |
| | |
*/ | |
use AuthenticatesAndRegistersUsers, ThrottlesLogins; | |
/** | |
* Where to redirect users after login / registration. | |
* | |
* @var string | |
*/ | |
protected $redirectTo = '/account'; | |
/** | |
* Create a new authentication controller instance. | |
* | |
* @return void | |
*/ | |
public function __construct() | |
{ | |
parent::__construct(); | |
$this->middleware('guest', ['except' => 'logout']); | |
} | |
/** | |
* Get a validator for an incoming registration request. | |
* | |
* @param array $data | |
* @return \Illuminate\Contracts\Validation\Validator | |
*/ | |
protected function validator(array $data) | |
{ | |
return Validator::make($data, [ | |
'name' => 'required|max:255', | |
'email' => 'required|email|max:255|unique:identity', | |
'password' => 'required|confirmed|min:6', | |
]); | |
} | |
/** | |
* Create a new identity instance after a valid registration. | |
* | |
* @param array $data | |
* @return Identity | |
*/ | |
protected function create(array $data) | |
{ | |
$member = Member::create([ | |
'name' => $data['name'] | |
]); | |
return Identity::create([ | |
'email' => $data['email'], | |
'password' => bcrypt($data['password']), | |
'identifiable_id' => $member->id, | |
'identifiable_type' => Member::class | |
]); | |
} | |
} |
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
<?php | |
namespace App\Http\Controllers; | |
use Illuminate\Foundation\Bus\DispatchesJobs; | |
use Illuminate\Routing\Controller as BaseController; | |
use Illuminate\Foundation\Validation\ValidatesRequests; | |
use Illuminate\Foundation\Auth\Access\AuthorizesRequests; | |
abstract class Controller extends BaseController { | |
use AuthorizesRequests, DispatchesJobs, ValidatesRequests; | |
public function __construct() { | |
view()->share('signedIn', \Auth::check()); | |
view()->share('currentUser', \Auth::User()); | |
} | |
} |
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
<?php | |
namespace App; | |
use Illuminate\Auth\Authenticatable; | |
use Illuminate\Database\Eloquent\Model; | |
use Illuminate\Foundation\Auth\Access\Authorizable; | |
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract; | |
use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract; | |
class Identity extends Model implements AuthenticatableContract, AuthorizableContract { | |
use Authenticatable, Authorizable; | |
protected $table = 'identity'; | |
protected $fillable = [ | |
'email', 'password', 'identifiable_id', 'identifiable_type' | |
]; | |
public function user() { | |
return $this->morphTo('identifiable'); | |
} | |
} |
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
<?php | |
namespace App; | |
use App\Identity; | |
use Illuminate\Database\Eloquent\Model; | |
class Member extends Model { | |
protected $table = 'member'; | |
protected $fillable = [ | |
'name', 'last_login' | |
]; | |
public function identity() { | |
return $this->morphOne(Identity::class, 'identifiable'); | |
} | |
} |
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
<?php | |
Route::auth(); | |
Route::group(['prefix' => 'account'], function () { | |
Route::get('/', 'AccountController@index')->name('account'); | |
Route::get('/settings', 'AccountController@settings')->name('account-settings'); | |
Route::get('/wishlist', 'AccountController@wishlist')->name('account-wishlist'); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment