LoginController.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. <?php
  2. namespace App\Http\Controllers\Auth;
  3. use App\Http\Controllers\Controller;
  4. use Illuminate\Foundation\Auth\AuthenticatesUsers;
  5. use Socialite;
  6. use App\Models\User;
  7. use App\Models\Customer;
  8. use App\Models\Cart;
  9. use Session;
  10. use Illuminate\Http\Request;
  11. use CoreComponentRepository;
  12. use Illuminate\Support\Str;
  13. class LoginController extends Controller
  14. {
  15. /*
  16. |--------------------------------------------------------------------------
  17. | Login Controller
  18. |--------------------------------------------------------------------------
  19. |
  20. | This controller handles authenticating users for the application and
  21. | redirecting them to your home screen. The controller uses a trait
  22. | to conveniently provide its functionality to your applications.
  23. |
  24. */
  25. use AuthenticatesUsers;
  26. /**
  27. * Where to redirect users after login.
  28. *
  29. * @var string
  30. */
  31. /*protected $redirectTo = '/';*/
  32. /**
  33. * Redirect the user to the Google authentication page.
  34. *
  35. * @return \Illuminate\Http\Response
  36. */
  37. public function redirectToProvider($provider)
  38. {
  39. if(request()->get('query') == 'mobile_app'){
  40. request()->session()->put('login_from', 'mobile_app');
  41. }
  42. return Socialite::driver($provider)->redirect();
  43. }
  44. /**
  45. * Obtain the user information from Google.
  46. *
  47. * @return \Illuminate\Http\Response
  48. */
  49. public function handleProviderCallback(Request $request, $provider)
  50. {
  51. if (session('login_from') == 'mobile_app') {
  52. return $this->mobileHandleProviderCallback($request, $provider);
  53. }
  54. try {
  55. if ($provider == 'twitter') {
  56. $user = Socialite::driver('twitter')->user();
  57. } else {
  58. $user = Socialite::driver($provider)->stateless()->user();
  59. }
  60. } catch (\Exception $e) {
  61. flash("Something Went wrong. Please try again.")->error();
  62. return redirect()->route('user.login');
  63. }
  64. //check if provider_id exist
  65. $existingUserByProviderId = User::where('provider_id', $user->id)->first();
  66. if ($existingUserByProviderId) {
  67. //proceed to login
  68. auth()->login($existingUserByProviderId, true);
  69. }
  70. else {
  71. //check if email exist
  72. $existingUser = User::where('email', $user->email)->first();
  73. if ($existingUser) {
  74. //update provider_id
  75. $existing_User = $existingUser;
  76. $existing_User->provider_id = $user->id;
  77. $existing_User->save();
  78. //proceed to login
  79. auth()->login($existing_User, true);
  80. } else {
  81. //create a new user
  82. $newUser = new User;
  83. $newUser->name = $user->name;
  84. $newUser->email = $user->email;
  85. $newUser->email_verified_at = date('Y-m-d Hms');
  86. $newUser->provider_id = $user->id;
  87. $newUser->save();
  88. //make user a customer
  89. // $customer = new Customer;
  90. // $customer->user_id = $newUser->id;
  91. // $customer->save();
  92. //proceed to login
  93. auth()->login($newUser, true);
  94. }
  95. }
  96. if (session('temp_user_id') != null) {
  97. Cart::where('temp_user_id', session('temp_user_id'))
  98. ->update([
  99. 'user_id' => auth()->user()->id,
  100. 'temp_user_id' => null
  101. ]);
  102. Session::forget('temp_user_id');
  103. }
  104. if (session('link') != null) {
  105. return redirect(session('link'));
  106. } else {
  107. if(auth()->user()->user_type == 'seller') {
  108. return redirect()->route('seller.dashboard');
  109. }
  110. return redirect()->route('dashboard');
  111. }
  112. }
  113. public function mobileHandleProviderCallback($request, $provider)
  114. {
  115. $return_provider = '';
  116. $result = false;
  117. if($provider) {
  118. $return_provider = $provider;
  119. $result = true;
  120. }
  121. return response()->json([
  122. 'result' => $result,
  123. 'provider' => $return_provider
  124. ]);
  125. }
  126. /**
  127. * Validate the user login request.
  128. *
  129. * @param \Illuminate\Http\Request $request
  130. * @return void
  131. *
  132. * @throws \Illuminate\Validation\ValidationException
  133. */
  134. protected function validateLogin(Request $request)
  135. {
  136. $request->validate([
  137. 'email' => 'required_without:phone',
  138. 'phone' => 'required_without:email',
  139. 'password' => 'required|string',
  140. ]);
  141. }
  142. /**
  143. * Get the needed authorization credentials from the request.
  144. *
  145. * @param \Illuminate\Http\Request $request
  146. * @return array
  147. */
  148. protected function credentials(Request $request)
  149. {
  150. if ($request->get('phone') != null) {
  151. return ['phone' => "+{$request['country_code']}{$request['phone']}", 'password' => $request->get('password')];
  152. } elseif ($request->get('email') != null) {
  153. return $request->only($this->username(), 'password');
  154. }
  155. }
  156. /**
  157. * Check user's role and redirect user based on their role
  158. * @return
  159. */
  160. public function authenticated()
  161. {
  162. if (session('temp_user_id') != null) {
  163. Cart::where('temp_user_id', session('temp_user_id'))
  164. ->update(
  165. [
  166. 'user_id' => auth()->user()->id,
  167. 'temp_user_id' => null
  168. ]
  169. );
  170. Session::forget('temp_user_id');
  171. }
  172. if (auth()->user()->user_type == 'admin' || auth()->user()->user_type == 'staff') {
  173. CoreComponentRepository::instantiateShopRepository();
  174. return redirect()->route('admin.dashboard');
  175. } elseif (auth()->user()->user_type == 'seller') {
  176. return redirect()->route('seller.dashboard');
  177. } else {
  178. if (session('link') != null) {
  179. return redirect(session('link'));
  180. } else {
  181. return redirect()->route('dashboard');
  182. }
  183. }
  184. }
  185. /**
  186. * Get the failed login response instance.
  187. *
  188. * @param \Illuminate\Http\Request $request
  189. * @return \Symfony\Component\HttpFoundation\Response
  190. *
  191. * @throws \Illuminate\Validation\ValidationException
  192. */
  193. protected function sendFailedLoginResponse(Request $request)
  194. {
  195. flash(translate('Invalid login credentials'))->error();
  196. return back();
  197. }
  198. /**
  199. * Log the user out of the application.
  200. *
  201. * @param \Illuminate\Http\Request $request
  202. * @return \Illuminate\Http\Response
  203. */
  204. public function logout(Request $request)
  205. {
  206. if (auth()->user() != null && (auth()->user()->user_type == 'admin' || auth()->user()->user_type == 'staff')) {
  207. $redirect_route = 'login';
  208. } else {
  209. $redirect_route = 'home';
  210. }
  211. //User's Cart Delete
  212. if (auth()->user()) {
  213. Cart::where('user_id', auth()->user()->id)->delete();
  214. }
  215. $this->guard()->logout();
  216. $request->session()->invalidate();
  217. Session::flush();
  218. return $this->loggedOut($request) ?: redirect()->route($redirect_route);
  219. }
  220. /**
  221. * Create a new controller instance.
  222. *
  223. * @return void
  224. */
  225. public function __construct()
  226. {
  227. $this->middleware('guest')->except('logout');
  228. }
  229. }