Example #1
32
 /**
  * Convert an authentication exception into an unauthenticated response.
  *
  * @param  \Illuminate\Http\Request  $request
  * @param  \Illuminate\Auth\AuthenticationException  $exception
  * @return \Illuminate\Http\Response
  */
 protected function unauthenticated($request, AuthenticationException $exception)
 {
     if ($request->expectsJson()) {
         return response()->json(['error' => 'Unauthenticated.'], 401);
     }
     return redirect()->guest('login');
 }
Example #2
2
 /**
  * Create a response object from the given validation exception.
  *
  * @param  \Illuminate\Validation\ValidationException  $e
  * @param  \Illuminate\Http\Request  $request
  * @return \Symfony\Component\HttpFoundation\Response
  */
 protected function convertValidationExceptionToResponse(ValidationException $e, $request)
 {
     if ($e->response) {
         return $e->response;
     }
     $errors = $e->validator->errors()->getMessages();
     if ($request->expectsJson()) {
         return response()->json($errors, 422);
     }
     return redirect()->back()->withInput($request->input())->withErrors($errors);
 }
Example #3
1
 /**
  * Determine if the current request probably expects a JSON response.
  *
  * @return bool
  * @static
  */
 public static function expectsJson()
 {
     return \Illuminate\Http\Request::expectsJson();
 }
 /**
  * Convert an authentication exception into an unauthenticated response.
  *
  * @param  \Illuminate\Http\Request  $request
  * @param  \Illuminate\Auth\AuthenticationException  $exception
  * @return \Illuminate\Http\Response
  */
 protected function unauthenticated($request, AuthenticationException $exception)
 {
     if ($request->expectsJson() || $this->isApiRoute($request)) {
         return response()->json(['error' => $exception->getMessage()], 401);
     }
     return redirect()->guest('login');
 }
 /**
  * Handle an incoming request.
  *
  * @param  \Illuminate\Http\Request $request
  * @param  \Closure                 $next
  *
  * @return mixed
  */
 public function handle($request, Closure $next)
 {
     # Skip API related requests
     if ($request->expectsJson()) {
         return $next($request);
     }
     return parent::handle($request, $next);
 }
Example #6
0
 /**
  * Convert an authentication exception into an unauthenticated response.
  *
  * @param  \Illuminate\Http\Request  $request
  * @param  \Illuminate\Auth\AuthenticationException  $e
  * @return \Illuminate\Http\Response
  */
 protected function unauthenticated($request, AuthenticationException $e)
 {
     if ($request->expectsJson()) {
         return response()->json(['error' => 'Unauthenticated.'], 401);
     } else {
         return view('canvas::auth.login');
     }
 }
 /**
  * Handle an incoming request.
  *
  * @param  \Illuminate\Http\Request $request
  * @param  \Closure                 $next
  *
  * @return \Closure|\Illuminate\Http\RedirectResponse
  * @throws \App\Exceptions\Users\UserAlreadyLoggedInException
  */
 public function handle(Request $request, \Closure $next)
 {
     if (app('auth.driver')->check()) {
         if ($request->expectsJson()) {
             throw new UserAlreadyLoggedInException();
         }
         return redirect('/');
     }
     return $next($request);
 }
Example #8
0
 /**
  * Handle an incoming request.
  *
  * @param  Request  $request
  * @param  \Closure $next
  *
  * @return mixed
  */
 public function handle(Request $request, \Closure $next)
 {
     if (app('auth.driver')->guest()) {
         if ($request->expectsJson()) {
             throw new UnauthorizedHttpException('Unauthorized');
         } else {
             return redirect()->guest(route(app('translator')->getLocale() . '.login'));
         }
     }
     return $next($request);
 }
 /**
  * Render an exception into an HTTP response.
  *
  * @param  \Illuminate\Http\Request  $request
  * @param  \Exception  $exception
  * @return \Illuminate\Http\Response|\Illuminate\Http\RedirectResponse|null
  */
 public function render($request, Exception $exception)
 {
     if ($exception instanceof RejectedInputException) {
         $errors[$exception->getAttribute()] = 'There was a problem with this field.';
         if ($request->expectsJson()) {
             return response()->json($errors, Response::HTTP_UNPROCESSABLE_ENTITY);
         }
         return redirect()->back()->withInput($request->input())->withErrors($errors);
     }
     return null;
 }
 /**
  * Store a newly created user.
  *
  * @param \Illuminate\Http\Request $request
  *
  * @return \Symfony\Component\HttpFoundation\Response
  * @throws \App\Exceptions\Common\ValidationException
  */
 public function register(Request $request)
 {
     $validator = app('validator')->make($request->all(), ['name' => 'sometimes|required|max:255', 'email' => 'required|email|max:255|unique:users', 'password' => 'required|confirmed|min:' . config('auth.passwords.users.min_length')]);
     if ($validator->fails()) {
         throw new ValidationException($validator);
     }
     $user = new User();
     $request->has('name') && ($user->name = $request->input('name'));
     $user->email = $request->input('email');
     $user->password = app('hash')->make($request->input('password'));
     $user->save() && event(new Registered($user));
     app('auth.driver')->login($user);
     return $request->expectsJson() ? response()->json($user)->setStatusCode(IlluminateResponse::HTTP_CREATED) : redirect($this->redirectPath());
 }
 /**
  * Activate an account [Web only].
  *
  * @param \Illuminate\Http\Request $request
  * @param string|null              $token
  *
  * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
  * @throws \App\Exceptions\Common\ValidationException
  */
 public function activate(Request $request, $token = null)
 {
     $data = !is_null($token) ? ['token' => $token] : $request->all();
     $validator = app('validator')->make($data, ['token' => 'required|string']);
     if ($validator->fails()) {
         throw new ValidationException($validator);
     }
     $activation = UserActivation::whereCode($data['token'])->first();
     if (!$activation) {
         throw new TokenNotValidException();
     }
     /** @var \App\Models\User $user */
     $user = User::findOrFail($activation->user_id);
     $this->complete($user, $data['token']);
     return $request->expectsJson() ? response()->json(['message' => 'Activated']) : redirect($this->redirectPath())->with('message', 'Activation successful');
 }
Example #12
0
 /**
  * @param \Illuminate\Http\Request $request
  * @param array                    $errors
  *
  * @return \Symfony\Component\HttpFoundation\Response
  */
 protected function buildFailedValidationResponse(Request $request, array $errors)
 {
     if ($request->expectsJson()) {
         return new JsonResponse($errors, 422);
     }
     return redirect()->to($this->getRedirectUrl())->withInput($request->input())->withErrors($errors, $this->errorBag());
 }
Example #13
0
 /**
  * Log the user out of the application.
  *
  * @param \Illuminate\Http\Request $request
  *
  * @return \Illuminate\Http\RedirectResponse|\Illuminate\Http\JsonResponse
  */
 public function logout(Request $request)
 {
     $user = app('auth.driver')->user();
     app('auth.driver')->logout();
     $request->session()->flush();
     $request->session()->regenerate();
     app('events')->fire(new LoggedOut($user));
     if ($request->expectsJson()) {
         return response()->json([]);
     }
     return redirect('/');
 }
 /**
  * Reset the password through password-reset-token and email provided.
  *
  * @param \Illuminate\Http\Request $request
  *
  * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
  * @throws \App\Exceptions\Common\ValidationException
  */
 public function resetPassword(Request $request)
 {
     $validator = app('validator')->make($request->all(), ['token' => 'required|string', 'email' => 'required|email|max:255', 'password' => 'required|confirmed|min:' . app('config')->get('auth.passwords.users.min_length')]);
     if ($validator->fails()) {
         throw new ValidationException($validator);
     }
     $credentials = $request->only('email', 'password', 'password_confirmation', 'token');
     $passwordBroker = app('auth.password.broker');
     $response = $passwordBroker->reset($credentials, function (User $user, $password) {
         $user->password = app('hash')->make($password);
         $user->save();
         app('auth.driver')->login($user);
     });
     switch ($response) {
         case $passwordBroker::INVALID_USER:
             throw new ModelNotFoundException(trans($response));
             break;
         case $passwordBroker::INVALID_TOKEN:
             throw new TokenNotValidException(trans($response));
             break;
     }
     event(new ResetPassword(app('auth.driver')->user()));
     if ($request->expectsJson()) {
         return response()->json(['message' => trans('passwords.reset')]);
     }
     return redirect($this->redirectPath())->with('message', trans('passwords.reset'));
 }