0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

HarmonyOS開発秘籍:エレガントなログインステータス管理システムの構築

Posted at

一、前言

HarmonyOSの開発プロセスにおいて、アプリケーションの規模が大きくなるに従って、ログインステータスの管理はシステム設計における1つの課題となってきます。明確で効率的なログインステータス管理システムは、開発プロセスを簡素化するだけでなく、ユーザー エクスペリエンスを向上させることができます。本稿では、複雑なシステムにおけるログインステータスの制御を簡単にこなせるような、エレガントなログインステータス管理設計方案を共有します。

二、認証イベントと認証コードの設計

認証イベントは、アプリケーション全体のイベントのコアであり、ログインやログアウトの操作をトリガーし、プロジェクト全体でブロードキャストすることができます。3種類の基本的な認証イベントを定義しました:アプリケーションの起動、ログイン、ログアウト。

認証イベントのコード例:

export class AuthenticationEvent {
  static readonly AppStart = "$AppStart$";
  static readonly LogIn = "$LogIn$";
  static readonly LogOut = "$LogOut$";
}

三、認証ステータス

ユーザーの認証ステータスは、システムがユーザーがログインしているかどうかを判断するための鍵です。2種類のステータスを定義しました:認証成功と未認証。

認証ステータスのコード例:


export const AUTHENTICATION_STATE:string = "$AuthenticationState$"

// 認証ステータス 
export class AuthenticationState {}

// - authenticated - 認証成功
export class AuthenticationAuthenticated extends AuthenticationState {}

// - unauthenticated - 未認証
export class AuthenticationUnauthenticated extends AuthenticationState {}

四、認証インターフェースのテンプレート

認証インターフェースは、ログインステータス管理の具体的な実装であり、トークンのチェック、保存、削除、認証成功時と未認証時のページ遷移を含んでいます。

認証インターフェースのコード例:

const TOKEN = "token";

export abstract class IUserAuthentication {
  libPreferencesSync: LibPreferencesSync;

  hasToken(): boolean {
    return this.libPreferencesSync.getValue(TOKEN).toString().length > 0;
  }

  saveToken(token: string) {
    this.libPreferencesSync.saveKeyValue(TOKEN, token);
  }

  deleteToken() {
    this.libPreferencesSync.deleteData(TOKEN);
  }

  abstract authPage(): void;
  abstract unAuthPage(): void;
}

五、ログインステータスを管理するページ

イベントハブ(eventHub)を使用して、アプリケーション全体でグローバルステータス通知を行うことができます。ログインステータスマネージャーは、ユーザーが送信したイベントに応じて認証ステータスを変更します。

ログインステータスマネージャーのコード例:


export class Authentication{
  // 私有静的変数で、シングルトンオブジェクトを保存する
  private static _instance :Authentication;

  // 私有コンストラクターで、外部からの直接インスタンス化を防ぐ
  private constructor() {
    // コンストラクターの内容
  }

  // 静的メソッドで、シングルトンオブジェクトを取得する
  public static getInstance() {
    if (!Authentication._instance) {
      Authentication._instance = new Authentication();
    }
    return Authentication._instance;
  }

  startApp(){
    Application.getInstance().applicationContext.eventHub.emit(AuthenticationEvent.AppStart)
  }

  logIn(token:string){
    Application.getInstance().applicationContext.eventHub.emit(AuthenticationEvent.LogIn,token)
  }

  logOut(){
    Application.getInstance().applicationContext.eventHub.emit(AuthenticationEvent.LogOut)
  }

  init(iuserAuthentciation : IUserAuthentication){
    Application.getInstance().applicationContext.eventHub.on(AuthenticationEvent.AppStart,()=>{
      if(iuserAuthentciation.hasToken()){
        AppStorage.setOrCreate(AUTHENTICATION_STATE,new AuthenticationAuthenticated())
        iuserAuthentciation.authPage();
      }else{
        AppStorage.setOrCreate(AUTHENTICATION_STATE,new AuthenticationUnauthenticated())
        iuserAuthentciation.unAuthPage();
      }
    })
    Application.getInstance().applicationContext.eventHub.on(AuthenticationEvent.LogIn,(token:string)=>{
      iuserAuthentciation.saveToken(token)
      AppStorage.set(AUTHENTICATION_STATE,new AuthenticationAuthenticated())
      iuserAuthentciation.authPage();
    })
    Application.getInstance().applicationContext.eventHub.on(AuthenticationEvent.LogOut,()=>{
      iuserAuthentciation.deleteToken()
      AppStorage.set(AUTHENTICATION_STATE,new AuthenticationUnauthenticated())
      iuserAuthentciation.unAuthPage();
    })
  }
}

六、プロジェクトでの使用

プロジェクトでは、ログインステータスマネージャーを初期化し、ページ内で認証ステータスに応じてページのスタイルや動作を変更します。

プロジェクト使用コード例:

class Auth extends IUserAuthentication {
  // ... 具体的なログインと未ログインページ遷移のロジックを実装する
}

// 初期化
Authentication.getInstance().init(new Auth());

// ページ内で使用する
aboutToAppear(): void {
  this.authentication();
}

authentication() {
  if (this.authetication instanceof AuthenticationAuthenticated) {
    // ユーザーデータを取得する
  } else if (this.authetication instanceof AuthenticationUnauthenticated) {
    // ユーザーデータを削除する
  }
}

七、おわりに

本稿の共有を通じて、HarmonyOSでエレガントなログインステータス管理システムを設計し、実装する方法を学びました。認証イベントの設計から認証ステータスの管理、具体的な認証インターフェースの実装まで、開発プロセスを簡素化し、システムの堅牢性和保守性を向上させるために、それぞれのステップが重要です。本稿がHarmonyOSの開発者にとって実用的な参考資料とインスピレーションを提供できることを願っています。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?