Advertisement
Guest User

Untitled

a guest
Feb 7th, 2016
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.23 KB | None | 0 0
  1. namespace AppHttpControllers;
  2.  
  3. use IlluminateHttpRequest;
  4. use AppHttpControllersController;
  5. use AppAuthenticateUser;
  6.  
  7. class AuthController extends Controller {
  8.  
  9. public function login(AuthenticateUser $authenticateUser, Request $request) {
  10. $authenticateUser->execute($request->has('code'), $this);
  11. return Socialite::with('github')->redirect();
  12. }
  13.  
  14. public function logout() {
  15. if (Auth::check()) Auth::logout();
  16. return view('auth.loggedout');
  17. }
  18.  
  19. }
  20.  
  21. <?php
  22.  
  23. namespace App;
  24.  
  25. use IlluminateContractsAuthGuard;
  26. use LaravelSocialiteContractsFactory as Socialite;
  27. use AppRepositoriesUserRepository;
  28. use Request;
  29.  
  30. class AuthenticateUser {
  31.  
  32. private $socialite;
  33. private $auth;
  34.  
  35. public function __construct(UserRepository $users, Socialite $socialite, Guard $auth) {
  36. $this->users = $users;
  37. $this->socialite = $socialite;
  38. $this->auth = $auth;
  39. }
  40.  
  41. public function execute($hasCode) {
  42.  
  43. if (!$hasCode) {
  44. return $this->getAuthorizationFirst();
  45. }
  46.  
  47. $user = $this->users->findbyEmail($this->getUser());
  48.  
  49. $this->auth->login($user, true);
  50.  
  51. return redirect('/');
  52. }
  53.  
  54. public function getAuthorizationFirst() {
  55. return $this->socialite->driver('github')->redirect();
  56. }
  57.  
  58. public
  59. function getUser() {
  60. return $user = $this->socialite->driver('github')->user();
  61. }
  62.  
  63. }
  64.  
  65. Route::get('o365/connect', 'AuthController@login');
  66. Route::get('o365/callback', 'AuthController@login');
  67. Route::get('auth/logout', 'AuthController@logout');
  68.  
  69. <?php
  70.  
  71. namespace AppHttpMiddleware;
  72.  
  73. use Closure;
  74. use IlluminateContractsAuthGuard;
  75.  
  76. class Authenticate {
  77.  
  78. /**
  79. * The Guard implementation.
  80. *
  81. * @var Guard
  82. */
  83. protected $auth;
  84.  
  85. /**
  86. * Create a new filter instance.
  87. *
  88. * @param Guard $auth
  89. * @return void
  90. */
  91. public function __construct(Guard $auth) {
  92. $this->auth = $auth;
  93. }
  94.  
  95. /**
  96. * Handle an incoming request.
  97. *
  98. * @param IlluminateHttpRequest $request
  99. * @param Closure $next
  100. * @return mixed
  101. */
  102. public function handle($request, Closure $next) {
  103.  
  104. if ($this->auth->guest()) {
  105. if ($request->ajax()) {
  106. return response('Unauthorized.', 401);
  107. } else {
  108. return redirect()->guest('o365/connect');
  109. }
  110. }
  111.  
  112. if (Auth::check()) {
  113. return $next($request);
  114. }
  115. }
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement