PageRenderTime 48ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/app/Http/Controllers/Auth/AuthController.php

https://bitbucket.org/coredeveloper2013/navipi-test
PHP | 299 lines | 149 code | 33 blank | 117 comment | 21 complexity | 19c571eea2b37a45a65bad0c0e5b55d2 MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, LGPL-2.1
  1. <?php
  2. namespace App\Http\Controllers\Auth;
  3. use URL;
  4. use App\User;
  5. use App\EmailTemplate;
  6. use Validator;
  7. use App\Http\Controllers\Controller;
  8. use Illuminate\Foundation\Auth\ThrottlesLogins;
  9. use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;
  10. use Illuminate\Support\Facades\Mail;
  11. use Illuminate\Http\Request;
  12. use Illuminate\Support\Facades\Auth;
  13. use Illuminate\Support\Facades\Lang;
  14. class AuthController extends Controller
  15. {
  16. /*
  17. |--------------------------------------------------------------------------
  18. | Registration & Login Controller
  19. |--------------------------------------------------------------------------
  20. |
  21. | This controller handles the registration of new users, as well as the
  22. | authentication of existing users. By default, this controller uses
  23. | a simple trait to add these behaviors. Why don't you explore it?
  24. |
  25. */
  26. use AuthenticatesAndRegistersUsers, ThrottlesLogins;
  27. /**
  28. * Where to redirect users after login / registration.
  29. *
  30. * @var string
  31. */
  32. protected $redirectTo = '/';
  33. /**
  34. * Create a new authentication controller instance.
  35. *
  36. * @return void
  37. */
  38. public function __construct()
  39. {
  40. $this->middleware($this->guestMiddleware(), ['except' => 'logout']);
  41. }
  42. /**
  43. * Get a validator for an incoming registration request.
  44. *
  45. * @param array $data
  46. * @return \Illuminate\Contracts\Validation\Validator
  47. */
  48. protected function validator(array $data)
  49. {
  50. return Validator::make($data, [
  51. //'first_name' => 'required|max:255',
  52. //'last_name' => 'required|max:255',
  53. 'email' => 'required|email|max:255|unique:users',
  54. //'user_name' => 'required|max:255|unique:users',
  55. //'address' => 'required',
  56. //'utc_timezone' => 'required',
  57. 'password' => 'required|min:6|confirmed',
  58. //'g-recaptcha-response' => 'required',
  59. 'terms_of_services' => 'required',
  60. //'address' => 'required|max:255',
  61. //'user_type' => 'required|in:1,2'
  62. ],[
  63. //'user_type.required' => 'Register as normal user or client',
  64. //'user_type.in' => 'Register as normal user or client'
  65. ]);
  66. }
  67. /**
  68. * Create a new user instance after a valid registration.
  69. *
  70. * @param array $data
  71. * @return User
  72. */
  73. protected function create(array $data)
  74. {
  75. /* return User::create([
  76. 'name' => $data['name'],
  77. 'email' => $data['email'],
  78. 'password' => bcrypt($data['password']),
  79. ]); */
  80. }
  81. public function register(Request $request)
  82. {
  83. $data=$request->all();
  84. // dd($data);
  85. //
  86. $validator = $this->validator($request->all());
  87. if ($validator->fails()) {
  88. $this->throwValidationException(
  89. $request, $validator
  90. );
  91. }
  92. //dd($data);
  93. $confirmation_code = str_random(30);
  94. $user_data = [
  95. //'first_name' => $data['first_name'],
  96. //'last_name' => $data['last_name'],
  97. //'user_name' => $data['user_name'],
  98. 'email' => $data['email'],
  99. 'password' => bcrypt($data['password']),
  100. //'user_type' => $data['user_type'],
  101. /* 'city' => $data['city'],
  102. 'province' => $data['province'],*/
  103. //'address' => $data['address'],
  104. //'utc_timezone' => $data['utc_timezone'],
  105. 'status' => 'E',
  106. 'in_step' => 1,
  107. //'confirmation_code' => $confirmation_code
  108. ];
  109. /* if($data['user_type']==2)
  110. {
  111. $user_data['business_name']= $data['business_name'];
  112. } */
  113. //dd($user_data);
  114. // Add optional address inputs
  115. if(!empty($data['phone']))
  116. $user_data['phone'] = $data['phone'];
  117. if(!empty($data['website']))
  118. $user_data['website'] = $data['website'];
  119. if(!empty($data['fax']))
  120. $user_data['fax'] = $data['fax'];
  121. if(!empty($data['postal_code']))
  122. $user_data['postal_code'] = $data['postal_code'];
  123. if(!empty($data['lat']))
  124. $user_data['lat'] = $data['lat'];
  125. if(!empty($data['lng']))
  126. $user_data['lng'] = $data['lng'];
  127. $user = User::create($user_data);
  128. Auth::login($user);
  129. /* if($user !== null) {
  130. session(['register_success' => 'Your registration successful. Please check your email to active your account.']);
  131. $setting = app('settings');
  132. $mail_data = [
  133. 'confirmation_code' => $confirmation_code,
  134. 'user' => $user,
  135. 'setting' => $setting,
  136. 'utype' => $data['utype']
  137. ];
  138. $email_template = EmailTemplate::find(1);
  139. $mailcontent=htmlspecialchars_decode($email_template->description);
  140. $mailcontent=str_replace('{USER_NAME}',$user->first_name,$mailcontent);
  141. $mailcontent=str_replace('{SITE_URL}',URL::to('/'),$mailcontent);
  142. $mailcontent=str_replace('{SITE_TITLE}',$setting->site_title,$mailcontent);
  143. $mailcontent=str_replace('{CONTACT_MAIL}',$setting->contact_email,$mailcontent);
  144. $mailcontent=str_replace('{ACTIVATION_LINK}',URL::to('/register/verify/' . $confirmation_code.'/'.$data['utype']),$mailcontent);
  145. //Mail::send('auth.emails.verify', $mail_data, function($message) use($user) {
  146. Mail::raw($mailcontent,$mail_data, function($message) use($user) {
  147. $setting = app('settings');
  148. $subject = $setting->site_title . 'Verify your email address';
  149. $message->from($setting->contact_email, $setting->contact_name);
  150. $message->to($user->email, $user->first_name.' '. $user->last_name);
  151. $message->replyTo($setting->contact_email, $setting->contact_name);
  152. $message->subject($subject);
  153. });
  154. } */
  155. //return $user;
  156. return redirect(url('user/signup-step/1'));
  157. }
  158. public function showRegistrationForm($type=0)
  159. {
  160. $timezones= \App\Timezone::all();
  161. return view('register', compact('timezones','type'));
  162. }
  163. public function postLogin(Request $request)
  164. {
  165. $inputdata = $request->all();
  166. //dd($inputdata);
  167. $this->validate($request, [
  168. $this->loginUsername() => 'required', 'password' => 'required',
  169. ]);
  170. // If the class is using the ThrottlesLogins trait, we can automatically throttle
  171. // the login attempts for this application. We'll key this by the username and
  172. // the IP address of the client making these requests into this application.
  173. $throttles = $this->isUsingThrottlesLoginsTrait();
  174. if ($throttles && $this->hasTooManyLoginAttempts($request)) {
  175. return $this->sendLockoutResponse($request);
  176. }
  177. $credentials = $this->getCredentials($request);
  178. $user_det = User::where('email',$credentials['email'])->first();
  179. if(count($user_det) > 0)
  180. {
  181. //unset();$credentials['username']
  182. $credentials['email'] = $request->input('email');
  183. }
  184. $remember_me = $request->has('rem') ? true : false;
  185. if (Auth::guard($this->getGuard())->attempt($credentials, $remember_me))
  186. {
  187. if(Auth::user()->status == 'Y')
  188. {
  189. $this->handleUserWasAuthenticated($request, $throttles);
  190. $this->redirectTo='/user/question-home';
  191. }
  192. else
  193. {
  194. $in_step = Auth::user()->in_step;
  195. return redirect(url('user/signup-step/'.$in_step));
  196. /* auth::logout();
  197. return redirect($this->redirectPath())
  198. ->withInput($request->only($this->loginUsername(), 'remember'))
  199. ->withErrors([
  200. $this->loginUsername() => 'Your account is not activated.'
  201. ])->with('inactive','inactive'); */
  202. }
  203. }
  204. // If the login attempt was unsuccessful we will increment the number of attempts
  205. // to login and redirect the user back to the login form. Of course, when this
  206. // user surpasses their maximum number of attempts they will get locked out.
  207. if ($throttles) {
  208. $this->incrementLoginAttempts($request);
  209. }
  210. //dd($this->redirectPath());
  211. return redirect($this->redirectPath())
  212. ->withInput($request->only($this->loginUsername(), 'remember'))
  213. ->withErrors([
  214. $this->loginUsername() => $this->getFailedLoginMessage(),
  215. ]);
  216. //return response()->json($response);
  217. }
  218. public function socialLogin(Request $request)
  219. {
  220. $data = $request->all();
  221. //print_r($data); exit;
  222. $has_error = 1;
  223. $return_data = array();
  224. /* && $data['social_type']=='vk' */
  225. if(!empty($data['social_type']) )
  226. {
  227. if(!empty($data['name']))
  228. $uname = $data['name'] ;
  229. if(!empty($data['screen_name']))
  230. $uname = $data['screen_name'];
  231. $name_arr = explode(' ',$uname);
  232. if(!isset($data['first_name']))
  233. $data['first_name'] = $name_arr[0];
  234. if(!empty($data['email']))
  235. $email = $data['email'];
  236. else $email = strtolower($data['first_name'])."@demo.com";
  237. //$user = User::where('user_name',$uname)->orWhere('email',$email)->first();
  238. $user = User::where('email',$email)->first();
  239. $password = 'ABC'.rand(100,9999);
  240. if(empty($user))
  241. {
  242. $user_data = [
  243. 'first_name' => $data['first_name'],
  244. 'last_name' => $data['last_name'],
  245. 'nickname' => $uname,
  246. 'email' => $email,
  247. 'password' => bcrypt($password),
  248. 'user_type' => 1,
  249. 'status' => 'Y',
  250. 'in_step' => 5,
  251. ];
  252. if(!empty($data['mobile_phone']))
  253. $user_data['phone'] = $data['mobile_phone'];
  254. $user = User::create($user_data);
  255. }
  256. //dd($user->toArray());
  257. Auth::login($user);
  258. return $user;
  259. }
  260. }
  261. }