/** * 生成一个新的 Token * * @param UserModel $user * @return string */ public static function newToken($user) { $token = null; do { $token = sha1(rand(0, PHP_INT_MAX)); } while (self::byToken($token)->data()); self::q()->insert(["user_id" => $user->id(), "token" => $token]); return $token; }
public function login() { try { list($username, $passwd) = $this->post(["username", "passwd"]); if (Validator::test(Validator::Email, $username)) { $user = $this->model->byEmail($username); } else { $user = $this->model->byUsername($username); } if (!$user->data()) { throw new handlerException("userNotExists"); } if (!$user->checkPasswd($passwd)) { throw new handlerException("invalidPasswd"); } $auth = Application::$auth; $auth->authenticated($user->id()); $auth->cookieRemember(); } catch (handlerException $e) { $this->render("login", ["error" => $e->getMessage()]); } }