public function testItShould_notLoginUser_incorrectCredentials()
 {
     $email = $this->randomEmail();
     $password = $this->randomPassword();
     $this->userRepositoryMock->expects($this->once())->method('findByCredentials')->with($email, $password);
     $this->callApi('POST', '/login', ['email' => $email, 'password' => $password]);
     $this->assertResponseStatus(Response::HTTP_UNAUTHORIZED);
 }
 public function testItShould_notRegisterUser_invalidInput()
 {
     $referer = $this->randomUrl();
     $this->userRepositoryMock->expects($this->never())->method('create');
     $this->call('POST', '/register', $parameters = [], $cookies = [], $files = [], ['HTTP_REFERER' => $referer]);
     $this->assertNull(auth()->user());
     $this->assertRedirectedTo($referer);
     $this->assertSessionHasErrors();
 }
 /**
  * @param Request $request
  * @param UserRepository $userRepository
  * @return Response
  */
 public function login(Request $request, UserRepository $userRepository)
 {
     $this->validate($request, ['email' => 'required|email', 'password' => 'required']);
     $user = $userRepository->findByCredentials($request->email, $request->password);
     if (!$user instanceof User) {
         throw new HttpException(Response::HTTP_UNAUTHORIZED);
     }
     return $this->response($user->makeVisible('api_token'));
 }
 public function testItShould_registerUser()
 {
     $email = $this->randomEmail();
     $password = $this->randomPassword();
     $user = $this->createUser();
     $this->userRepositoryMock->expects($this->once())->method('create')->with(['email' => $email, 'password' => $password])->willReturn($user);
     $this->callApi('POST', '/register', ['email' => $email, 'password' => $password]);
     $this->assertResponseStatus(Response::HTTP_OK);
     $this->seeJson($user->makeVisible('api_token')->toArray());
 }
 public function testItShould_handleSocialiteUser_userDoesNotExist()
 {
     $driver = uniqid();
     $socialiteUser = $this->createSocialiteUser();
     $result = $this->repository->handleSocialiteUser($socialiteUser, $driver);
     $this->assertInstanceOf(User::class, $result);
     $this->assertEquals($driver, $result->auth_driver);
     $this->assertEquals($socialiteUser->email, $result->email);
 }
 /**
  * @param string $authDriver
  * @param UserRepository $userRepository
  * @return RedirectResponse
  */
 public function handleProviderCallback(string $authDriver, UserRepository $userRepository) : RedirectResponse
 {
     /** @var User $socialiteUser */
     $socialiteUser = Socialite::driver($authDriver)->user();
     try {
         $user = $userRepository->handleSocialiteUser($socialiteUser, $authDriver);
     } catch (UserCreatedWithAnotherDriverException $e) {
         $message = sprintf("User with email '%s' exists, but was not created with %s.", $socialiteUser->email, $authDriver);
         if ($e->user->auth_driver) {
             $message .= sprintf(' Try to login with %s.', $e->user->auth_driver);
         } else {
             $message .= ' Try to login using email and password.';
         }
         return redirect('/login')->with('errors', new MessageBag([$message]));
     }
     Auth::login($user);
     return redirect('/home');
 }
 /**
  * @param Request $request
  * @param UserRepository $userRepository
  * @return Response
  */
 public function register(Request $request, UserRepository $userRepository)
 {
     $this->validate($request, ['email' => 'required|email|max:255|unique:users', 'password' => 'required|min:6']);
     $user = $userRepository->create($request->all());
     return $this->response($user->makeVisible('api_token'));
 }
 /**
  * Create a new user instance after a valid registration.
  *
  * @param  array $attributes
  * @return User
  */
 protected function create(array $attributes)
 {
     return $this->userRepository->create($attributes);
 }