init<TProfile> static method

Future<CkAuthService<TProfile>> init<TProfile>({
  1. required CkAuthConfig<TProfile> config,
  2. required CkAuthTokenManager tokenManager,
})

Completes auth module setup. CkTransport must already be initialized.

Implementation

static Future<CkAuthService<TProfile>> init<TProfile>({
  required CkAuthConfig<TProfile> config,
  required CkAuthTokenManager tokenManager,
}) async {
  final authState = CkAuthStateController();

  final profileExtractor = CkProfileExtractor<TProfile>(
    extractors: config.extractors,
  );

  final otpManager = CkOtpFlowManager(
    config: config.otpConfig,
    extractors: config.extractors,
    sendUrl: config.endpoints.sendOtp,
    verifyUrl: config.endpoints.verifyOtp,
    verifyForgetUrl: config.endpoints.verifyForgetOtp,
    sendMethod: config.endpoints.sendOtpMethod,
    verifyMethod: config.endpoints.verifyOtpMethod,
    verifyForgotMethod: config.endpoints.verifyForgotOtpMethod,
  );

  await otpManager.restoreTokens();

  final logoutHandler = CkLogoutHandler(
    tokenManager: tokenManager,
    profileExtractor: profileExtractor,
    otpManager: otpManager,
    stateController: authState,
    handlers: config.handlers,
    logoutUrl: config.endpoints.logout,
    logoutMethod: config.endpoints.logoutMethod,
  );

  final socialManager = CkSocialAuthManager<TProfile>(
    config: config.socialLoginConfig,
    tokenManager: tokenManager,
    profileExtractor: profileExtractor,
    stateController: authState,
    logoutHandler: logoutHandler,
    defaultExtractors: config.extractors,
  );

  final loadingController = CkAuthLoadingController();

  final service = CkAuthService<TProfile>._(
    tokenManager: tokenManager,
    authState: authState,
    profileExtractor: profileExtractor,
    otpManager: otpManager,
    logoutHandler: logoutHandler,
    socialManager: socialManager,
    config: config,
    loadingController: loadingController,
  );

  _instance = service;

  // Restore session on init
  await service.restoreSession();
  return service;
}