/**
  * Sends a verification email to a user.
  *
  * @param UserInterface $user
  *
  * @return bool
  */
 public function sendVerificationEmail(UserInterface $user)
 {
     $params = ['user_id' => $user->id(), 'type' => UserLink::VERIFY_EMAIL];
     // delete previous verify links
     $this->app['db']->delete('UserLinks')->where($params)->execute();
     // create new verification link
     $link = new UserLink();
     $link->create($params);
     // email it
     return $user->sendEmail('verify-email', ['verify' => $link->link]);
 }
 /**
  * Persists this cookie to the database.
  *
  * @param UserInterface $user
  *
  * @throws \Exception when the model cannot be saved.
  *
  * @return PersistentSession
  */
 public function persist(UserInterface $user)
 {
     $session = new PersistentSession();
     $session->email = $this->email;
     $session->series = $this->hash($this->series);
     $session->token = $this->hash($this->token);
     $session->user_id = $user->id();
     $session->two_factor_verified = $user->isTwoFactorVerified();
     try {
         $session->save();
     } catch (\Exception $e) {
         throw new \Exception("Unable to save persistent session for user # {$user->id()}: " . $e->getMessage());
     }
     return $session;
 }
 public function signIn(UserInterface $user, Request $req, Response $res)
 {
     // nothing to do if the user ID is already signed in
     $currentUserId = $req->session(self::SESSION_USER_ID_KEY);
     $userId = $user->id();
     if ($currentUserId == $userId) {
         return true;
     }
     // we are going to kill the current session and start a new one
     $req->destroySession();
     if (session_status() == PHP_SESSION_ACTIVE) {
         // remove the currently active session, for signed in users
         if ($currentUserId > 0 && ($sid = session_id())) {
             // delete any active sessions for this session ID
             $this->deleteSession($sid);
         }
         // regenerate session id to prevent session hijacking
         session_regenerate_id(true);
         // hang on to the new session id
         $sid = session_id();
         // close the old and new sessions
         session_write_close();
         // re-open the new session
         session_id($sid);
         session_start();
         // record the active session, for signed in users
         if ($userId > 0) {
             // create an active session for this session ID
             $this->createSession($sid, $userId, $req);
         }
     }
     // set the user id
     $req->setSession([self::SESSION_USER_ID_KEY => $userId, self::SESSION_USER_AGENT_KEY => $req->agent()]);
     return true;
 }