# テーマコンポーネントを使用してユーザーログインイベントをリッスンする方法はありますか

**URL:** https://meta.discourse.org/t/is-there-any-way-to-listen-user-login-event-using-theme-component/170976
**Category:** Development
**Created:** [2020 年 11 月 23 日午前 4:30 UTC](https://meta.discourse.org/t/is-there-any-way-to-listen-user-login-event-using-theme-component/170976 "2020-11-23T04:30:14Z")
**Posts on this page:** 1
**Showing post:** 6

<div class="post-metadata">

### Author: ![Johani](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/johani/32/176920_2.png) [@Johani](https://meta.discourse.org/u/Johani)
#### Post date: [2020 年 11 月 25 日午前 9:22 UTC](https://meta.discourse.org/t/is-there-any-way-to-listen-user-login-event-using-theme-component/170976/6 "2020-11-25T09:22:18Z")

</div>

> [@Saurabh\_Khandelwal](#):
>
> これらのプロパティの値を更新することはできますか？

残念ながら、それは不可能です。

これらのプロパティにはセッターが存在しません。仮に存在したとしても、変更は最初のウィンドウで一時的にのみ適用されます。ユーザーが 2 番目のタブに移動すると、データはデータベースに保存されている内容に基づいて表示されます。テーマはバックエンドにアクセスできず、フロントエンドの変更のみ可能です。

代わりに、リンクにハッシュを追加し、以下のようにチェックする方法があります。

```javascript
import { withPluginApi } from "discourse/lib/plugin-api";
import bootbox from "bootbox";

export default {
  name: "first-login-bootbox",
  initialize() {
    withPluginApi("0.8", api => {
      const user = api.getCurrentUser();
      if (!user) return;

      if (
        !user.read_first_notification &&
        !user.enforcedSecondFactor &&
        !window.location.hash
      ) {
        const text = `Lorem ipsum dolor sit amet <a href="http://localhost:3000/new-topic#some-hash" target="_blank">Link</a>, consectetur adipiscing elit, sed do eiusmod tempor`;
        bootbox.alert(text);
      }
    });
  }
};

```

投稿内の `/new-topic` へのリンクが単なる例なのか、それとも実際に実行したい操作なのかはわかりません。もしそれが望ましい結果である場合、別の問題が発生します。ハッシュ付きのページで bootbox が表示されなくても、ユーザーは以下のような状態を目にすることになります…

 ![image](https://global.discourse-cdn.com/meta/original/3X/7/6/765996f17503e72977475359b9bf15351b06d120.png)

…そして、コンポーザーは開きません。これは、ユーザーが最初のページビューで即座にトピックを入力し始めるのは非常に予想外であるため、当然の結果です。

ここで何を実現しようとしているのでしょうか？ユーザーに何かを伝えたいのでしょうか？

他のサイトで行われている一般的な方法は、ウェルカムメッセージを編集することですが、それが可能であれば代替案もあります。

以下に提案します。

1. 必要な情報をすべて含んだトピックを作成する
2. そのトピックを[公開する](https://meta.discourse.org/t/page-publishing/151971)
3. bootbox 内にそのトピックへのリンクを貼り付け、新しいタブで開く

これにより、ユーザーがリンクをクリックすると、以下のような画面（オーバーレイなし）が表示されます。

 ![image](https://global.discourse-cdn.com/meta/original/3X/6/1/612cb81bf16699ebe543c93fcc4c67e58a63f0fb.png)

そのページを見終えたら、最初のタブに戻り、bootbox を閉じて最初の通知を読み、その後サイトを利用を続行できます。

この方法であれば、ハッシュの追加やチェックすら不要です。以下にその例を示します。

```javascript
import { withPluginApi } from "discourse/lib/plugin-api";
import bootbox from "bootbox";

export default {
  name: "first-login-bootbox",
  initialize() {
    withPluginApi("0.8", api => {
      const user = api.getCurrentUser();
      if (!user) return;

      if (!user.read_first_notification && !user.enforcedSecondFactor) {
        const text = `Lorem ipsum dolor sit amet <a href="http://my.site.com/pub/bentley-flying-spur-s-production-milestone" target="_blank">Link</a>, consectetur adipiscing elit, sed do eiusmod tempor`;
        bootbox.alert(text);
      }
    });
  }
};

```

---

_[View the full topic](https://meta.discourse.org/t/is-there-any-way-to-listen-user-login-event-using-theme-component/170976)._
