Skip to content

Instantly share code, notes, and snippets.

@iwasherefirst2
Created September 26, 2019 11:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iwasherefirst2/37fe403c2831ab35ccb2dcb4660584f1 to your computer and use it in GitHub Desktop.
Save iwasherefirst2/37fe403c2831ab35ccb2dcb4660584f1 to your computer and use it in GitHub Desktop.
<?php
namespace App\Http\Controllers\Auth;
use App\User;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Validator;
use Illuminate\Foundation\Auth\RegistersUsers;
use Illuminate\Http\Request;
use App\Helper\GeoCoordinates;
use App\Mail\Register\Confirm;
use App\Registration\Apply;
use App\Registration\GroupUploads\User as GroupUploadUser;
use Illuminate\Support\Facades\Hash;
/**
* Handles login and creates application row if not activation request exists
*/
class ApplicationLoginController extends Controller
{
/**
* User Interface starts with asking for email
* @return [type] [description]
*/
public function start()
{
$bread = __('list.default_37');
// if user is logged in?
if ($user = \Auth::user()) {
return $this->startApp(['user_id' => $user->id, 'email' => $user->email]);
}
return view('auth.application.login.start', compact('bread'));
}
/**
* [restoring description]
* @param Request $request [description]
* @return [type] [description]
*/
public function restoring(Request $request)
{
$bread = __('list.default_37');
$email = $request->input('email');
// send email with url
$app = Apply::where('email', '=', $email)->first();
if (!empty($request->input('next')) && !empty($app)) {
$app->restoreSession();
return view('auth.application.login.emailSend', compact('bread'));
}
optional($app)->deleteCurrentRequest();
return $this->startApp(['email' => $email]);
}
public function startApp($attributes)
{
// First try to find applicant by user.
// this is necessary in case that user changed email..
if (!empty($attributes['user_id'])) {
$app = Apply::where('user_id', '=', $attributes['user_id'])->first();
if (!empty($app)) {
$app->email = null;
$app->save();
}
}
if (!empty($attributes['email']) && empty($app)) {
$app = Apply::updateOrCreate(['email' => $attributes['email']], [ 'user_id' => $attributes['user_id'] ?? null]);
}
return redirect('/apply/' . $app->getStep() . '/' . $app->id . '/' . $app->token);
}
/**
* [indentifyEmail description]
* @param Request $request [description]
* @return [type] [description]
*/
public function identifyEmail(Request $request)
{
$email = $request->input('email');
$bread = __('list.default_37');
// Send activation mail if user is in an groupupload
$user = GroupUploadUser::where('email', '=', $email)->with('groupUpload')->open()->first();
if (!empty($user)) {
if (empty($user->last_mail) || ($user->last_mail->diffInDays(new \Carbon\Carbon('now')) >= 1)) {
$msg = __('list.we_send_activation_before');
$user->sendActivation();
} else {
$msg = __('list.we_send_activation_today');
}
return view('auth.application.login.activate', compact(['msg','bread', 'email']));
}
// Restore session if saved
$application = Apply::where('email', '=', $email)->withOpenRequest()->first();
if (!empty($application)) {
return view('auth.application.login.restoreSession', compact('email', 'bread'));
}
$user = \App\User::where('email', '=', $email)->get(['id'])->first();
return $this->startApp(['email' => $email, 'user_id' => optional($user)->id]);
}
public function loginMask()
{
$bread = __('list.default_37');
return view('auth.application.login.login', compact(['bread']));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment