Nav apraksta

LoginController.php 2.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. <?php
  2. namespace App\Http\Controllers\Auth;
  3. use App\Http\Controllers\Controller;
  4. use Socialite;
  5. use Illuminate\Foundation\Auth\AuthenticatesUsers;
  6. use Illuminate\Foundation\Auth\User;
  7. use Illuminate\Http\Request;
  8. class LoginController extends Controller
  9. {
  10. /*
  11. |--------------------------------------------------------------------------
  12. | Login Controller
  13. |--------------------------------------------------------------------------
  14. |
  15. | This controller handles authenticating users for the application and
  16. | redirecting them to your home screen. The controller uses a trait
  17. | to conveniently provide its functionality to your applications.
  18. |
  19. */
  20. use AuthenticatesUsers;
  21. /**
  22. * Where to redirect users after login.
  23. *
  24. * @var string
  25. */
  26. // protected $redirectTo = '/dashboard';
  27. /**
  28. * Create a new controller instance.
  29. *
  30. * @return void
  31. */
  32. public function __construct()
  33. {
  34. $this->middleware('guest')->except('logout');
  35. }
  36. /**
  37. * Redirect the user to the Google authentication page.
  38. *
  39. * @return \Illuminate\Http\Response
  40. */
  41. public function redirectToProvider()
  42. {
  43. return Socialite::driver('google')->redirect();
  44. }
  45. /**
  46. * Obtain the user information from Google.
  47. *
  48. * @return \Illuminate\Http\Response
  49. */
  50. public function handleProviderCallback()
  51. {
  52. try {
  53. $google_user = Socialite::driver('google')->user();
  54. } catch (Exception $e) {
  55. return redirect('/login');
  56. }
  57. // only allow people with @company.com to login
  58. // if(explode("@", $google_user->email)[1] !== 'upr.edu'){
  59. // return redirect()->to('/');
  60. // }
  61. // check if they're in the database
  62. $user = \App\User::where('email', $google_user->email)->first();
  63. if($user){
  64. $user->update([
  65. 'name' => $google_user->name,
  66. 'email' => $google_user->email,
  67. 'google_id' => $google_user->id
  68. ]);
  69. // dd($user);
  70. // log them in
  71. auth()->login($user, true);
  72. } else {
  73. return redirect('/');
  74. }
  75. return redirect()->back();
  76. }
  77. public function logout(Request $request) {
  78. auth()->logout();
  79. return redirect('/');
  80. }
  81. }