Permalink
Switch branches/tags
Find file Copy path
39b2605 Sep 10, 2018
3 contributors

Users who have contributed to this file

@rappasoft @cheshirec7 @CodeAndWeb
158 lines (132 sloc) 4.3 KB
<?php
namespace App\Http\Controllers\Frontend\Auth;
use App\Helpers\Auth\Auth;
use Illuminate\Http\Request;
use App\Exceptions\GeneralException;
use App\Http\Controllers\Controller;
use App\Helpers\Frontend\Auth\Socialite;
use App\Events\Frontend\Auth\UserLoggedIn;
use App\Events\Frontend\Auth\UserLoggedOut;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use App\Repositories\Frontend\Auth\UserSessionRepository;
/**
* Class LoginController.
*/
class LoginController extends Controller
{
use AuthenticatesUsers;
/**
* Where to redirect users after login.
*
* @return string
*/
public function redirectPath()
{
return route(home_route());
}
/**
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
*/
public function showLoginForm()
{
return view('frontend.auth.login')
->withSocialiteLinks((new Socialite)->getSocialLinks());
}
/**
* Get the login username to be used by the controller.
*
* @return string
*/
public function username()
{
return config('access.users.username');
}
/**
* The user has been authenticated.
*
* @param Request $request
* @param $user
*
* @return \Illuminate\Http\RedirectResponse
* @throws GeneralException
*/
protected function authenticated(Request $request, $user)
{
/*
* Check to see if the users account is confirmed and active
*/
if (! $user->isConfirmed()) {
auth()->logout();
// If the user is pending (account approval is on)
if ($user->isPending()) {
throw new GeneralException(__('exceptions.frontend.auth.confirmation.pending'));
}
// Otherwise see if they want to resent the confirmation e-mail
throw new GeneralException(__('exceptions.frontend.auth.confirmation.resend', ['url' => route('frontend.auth.account.confirm.resend', $user->{$user->getUuidName()})]));
} elseif (! $user->isActive()) {
auth()->logout();
throw new GeneralException(__('exceptions.frontend.auth.deactivated'));
}
event(new UserLoggedIn($user));
// If only allowed one session at a time
if (config('access.users.single_login')) {
resolve(UserSessionRepository::class)->clearSessionExceptCurrent($user);
}
return redirect()->intended($this->redirectPath());
}
/**
* Log the user out of the application.
*
* @param Request $request
*
* @return \Illuminate\Http\Response
*/
public function logout(Request $request)
{
/*
* Remove the socialite session variable if exists
*/
if (app('session')->has(config('access.socialite_session_name'))) {
app('session')->forget(config('access.socialite_session_name'));
}
/*
* Remove any session data from backend
*/
app()->make(Auth::class)->flushTempSession();
/*
* Fire event, Log out user, Redirect
*/
event(new UserLoggedOut($request->user()));
/*
* Laravel specific logic
*/
$this->guard()->logout();
$request->session()->invalidate();
return redirect()->route('frontend.index');
}
/**
* @return \Illuminate\Http\RedirectResponse
*/
public function logoutAs()
{
// If for some reason route is getting hit without someone already logged in
if (! auth()->user()) {
return redirect()->route('frontend.auth.login');
}
// If admin id is set, relogin
if (session()->has('admin_user_id') && session()->has('temp_user_id')) {
// Save admin id
$admin_id = session()->get('admin_user_id');
app()->make(Auth::class)->flushTempSession();
// Re-login admin
auth()->loginUsingId((int) $admin_id);
// Redirect to backend user page
return redirect()->route('admin.auth.user.index');
} else {
app()->make(Auth::class)->flushTempSession();
// Otherwise logout and redirect to login
auth()->logout();
return redirect()->route('frontend.auth.login');
}
}
}