Skip to content

Commit

Permalink
fix link problem.
Browse files Browse the repository at this point in the history
  • Loading branch information
nodeloc committed Jan 28, 2024
1 parent 584d662 commit b2e140e
Showing 1 changed file with 29 additions and 6 deletions.
35 changes: 29 additions & 6 deletions src/Controllers/TelegramAuthController.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@
use Flarum\Forum\Auth\ResponseFactory;
use Flarum\Http\UrlGenerator;
use Flarum\Settings\SettingsRepositoryInterface;
use Laminas\Diactoros\Stream;
use Psr\Http\Message\ServerRequestInterface as Request;
use Exception;
use Psr\Http\Server\RequestHandlerInterface;
use Psr\Http\Message\ResponseInterface;
use Laminas\Diactoros\Response\HtmlResponse;
use Flarum\User\LoginProvider;
use Flarum\Locale\Translator;

class TelegramAuthController implements RequestHandlerInterface
{
Expand Down Expand Up @@ -45,14 +47,11 @@ public function handle(Request $request): ResponseInterface
$identifier = $auth['id'] ?? null;

if ($this->checkTelegramId($provider, $identifier)) {
$content = '<div style="text-align:center;font-family:Arial;">You can\'t link this telegram account to this user.</div>';
return new HtmlResponse($content);
$this->processContinue(false);
}

$user->loginProviders()->create(compact('provider', 'identifier'));
$content = '<script>window.close();window.opener.document.location.reload(true);</script>';

return new HtmlResponse($content);
$this->processContinue(true);
}

$suggestions = [];
Expand All @@ -69,12 +68,36 @@ public function handle(Request $request): ResponseInterface
}
);
} catch (Exception $e) {
$this->processContinue(false);
// 在异常情况下返回错误响应
return new HtmlResponse('Error: ' . $e->getMessage(), 500);
}
}


public function processContinue(bool $isSuccess): HtmlResponse
{
$url = resolve(UrlGenerator::class)->to('forum');
$translator = resolve(Translator::class);
if(!$isSuccess){
$redirect = $url->base().'/settings';
$href = htmlentities($redirect);
$continue = htmlentities($translator->trans('clarkwinkelmann-auth-popup-failsafe.api.auth.continue'));
$newBody = new Stream('php://temp', 'wb+');
$info = "You can\'t link this telegram account to this user.";
$newBody->write("<style>body{text-align:center;padding:20px;padding-top:40vh}p{font-family:sans-serif;font-size:2em;color:#aaa}a{color:#333}</style><p>$info</p><p><a href=\"$href\">$continue</a></p>");
$newBody->rewind();
return new HtmlResponse($newBody);
}else {
$redirect = $url->base() . '/settings';
$href = htmlentities($redirect);
$info = htmlentities($translator->trans('clarkwinkelmann-auth-popup-failsafe.api.auth.info'));
$continue = htmlentities($translator->trans('clarkwinkelmann-auth-popup-failsafe.api.auth.continue'));
$newBody = new Stream('php://temp', 'wb+');
$newBody->write("<style>body{text-align:center;padding:20px;padding-top:40vh}p{font-family:sans-serif;font-size:2em;color:#aaa}a{color:#333}</style><p>$info</p><p><a href=\"$href\">$continue</a></p>");
$newBody->rewind();
return new HtmlResponse($newBody);
}
}
protected function checkTelegramId($provider, $identifier)
{
$provider = LoginProvider::where(compact('provider', 'identifier'))->first();
Expand Down

0 comments on commit b2e140e

Please sign in to comment.