Skip to content

Commit

Permalink
MOBILE-4469 login: Move login constants to new file
Browse files Browse the repository at this point in the history
  • Loading branch information
dpalou committed Nov 22, 2023
1 parent 0c50ebd commit 006f901
Show file tree
Hide file tree
Showing 10 changed files with 67 additions and 49 deletions.
6 changes: 3 additions & 3 deletions src/core/features/login/components/site-help/site-help.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { AfterViewInit, Component, ElementRef, HostBinding, OnDestroy } from '@a

import { CoreUtils } from '@services/utils/utils';
import { ModalController, Translate } from '@singletons';
import { CoreLoginHelperProvider, GET_STARTED_URL } from '@features/login/services/login-helper';
import { FAQ_QRCODE_IMAGE_HTML, FAQ_URL_IMAGE_HTML, GET_STARTED_URL } from '@features/login/constants';
import { CoreDomUtils } from '@services/utils/dom';
import { CoreCancellablePromise } from '@classes/cancellable-promise';

Expand All @@ -39,8 +39,8 @@ export class CoreLoginSiteHelpComponent implements AfterViewInit, OnDestroy {
constructor(protected el: ElementRef<HTMLElement>) {
const getStartedTitle = Translate.instant('core.login.faqsetupsitelinktitle');
const canScanQR = CoreUtils.canScanQR();
const urlImageHtml = CoreLoginHelperProvider.FAQ_URL_IMAGE_HTML;
const qrCodeImageHtml = CoreLoginHelperProvider.FAQ_QRCODE_IMAGE_HTML;
const urlImageHtml = FAQ_URL_IMAGE_HTML;
const qrCodeImageHtml = FAQ_QRCODE_IMAGE_HTML;
const setupLinkHtml = `<a href="${GET_STARTED_URL}" title="${getStartedTitle}">${GET_STARTED_URL}</a>`;
const questions: Array<QuestionDefinition | false> = [
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { Component } from '@angular/core';

import { CoreConfig } from '@services/config';
import { CoreUtils } from '@services/utils/utils';
import { CoreLoginHelperProvider, GET_STARTED_URL } from '@features/login/services/login-helper';
import { GET_STARTED_URL, ONBOARDING_DONE } from '@features/login/constants';
import { ModalController } from '@singletons';

/**
Expand Down Expand Up @@ -88,7 +88,7 @@ export class CoreLoginSiteOnboardingComponent {
* Saves the onboarding has finished.
*/
protected saveOnboardingDone(): void {
CoreConfig.set(CoreLoginHelperProvider.ONBOARDING_DONE, 1);
CoreConfig.set(ONBOARDING_DONE, 1);
}

}
23 changes: 23 additions & 0 deletions src/core/features/login/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// (C) Copyright 2015 Moodle Pty Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

export const GET_STARTED_URL = 'https://moodle.com';
export const ONBOARDING_DONE = 'onboarding_done';
export const FAQ_QRCODE_INFO_DONE = 'qrcode_info_done';
export const FAQ_URL_IMAGE_HTML = '<img src="assets/img/login/faq_url.png" role="presentation" alt="">';
export const FAQ_QRCODE_IMAGE_HTML = '<img src="assets/img/login/faq_qrcode.png" role="presentation" alt="">';
export const EMAIL_SIGNUP_FEATURE_NAME = 'CoreLoginEmailSignup';
export const FORGOTTEN_PASSWORD_FEATURE_NAME = 'NoDelegate_ForgottenPassword';
export const IDENTITY_PROVIDERS_FEATURE_NAME = 'NoDelegate_IdentityProviders';
export const IDENTITY_PROVIDER_FEATURE_NAME_PREFIX = 'NoDelegate_IdentityProvider_';
8 changes: 4 additions & 4 deletions src/core/features/login/pages/credentials/credentials.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import { CoreApp } from '@services/app';
import { CoreNetwork } from '@services/network';
import { CoreSiteCheckResponse, CoreSites } from '@services/sites';
import { CoreDomUtils } from '@services/utils/dom';
import { CoreLoginHelper, CoreLoginHelperProvider } from '@features/login/services/login-helper';
import { CoreLoginHelper } from '@features/login/services/login-helper';
import { Translate } from '@singletons';
import { CoreSitePublicConfigResponse, CoreUnauthenticatedSite } from '@classes/sites/unauthenticated-site';
import { CoreEvents } from '@singletons/events';
Expand All @@ -33,6 +33,7 @@ import { CoreUserGuestSupportConfig } from '@features/user/classes/support/guest
import { SafeHtml } from '@angular/platform-browser';
import { CorePlatform } from '@services/platform';
import { CoreSitesFactory } from '@services/sites-factory';
import { EMAIL_SIGNUP_FEATURE_NAME, FORGOTTEN_PASSWORD_FEATURE_NAME } from '@features/login/constants';

/**
* Page to enter the user credentials.
Expand Down Expand Up @@ -199,9 +200,8 @@ export class CoreLoginCredentialsPage implements OnInit, OnDestroy {
this.showScanQR = await CoreLoginHelper.displayQRInCredentialsScreen(this.siteConfig.tool_mobile_qrcodetype);
}

this.canSignup = this.siteConfig.registerauth == 'email' &&
!this.site.isFeatureDisabled(CoreLoginHelperProvider.EMAIL_SIGNUP_FEATURE_NAME);
this.showForgottenPassword = !this.site.isFeatureDisabled(CoreLoginHelperProvider.FORGOTTEN_PASSWORD_FEATURE_NAME);
this.canSignup = this.siteConfig.registerauth == 'email' && !this.site.isFeatureDisabled(EMAIL_SIGNUP_FEATURE_NAME);
this.showForgottenPassword = !this.site.isFeatureDisabled(FORGOTTEN_PASSWORD_FEATURE_NAME);
this.exceededAttemptsHTML = CoreLoginHelper.buildExceededAttemptsHTML(
!!this.supportConfig?.canContactSupport(),
this.showForgottenPassword,
Expand Down
7 changes: 2 additions & 5 deletions src/core/features/login/pages/email-signup/email-signup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@ import {
AuthEmailSignupProfileFieldsCategory,
AuthEmailSignupSettings,
CoreLoginHelper,
CoreLoginHelperProvider,
} from '@features/login/services/login-helper';
import { CoreNavigator } from '@services/navigator';
import { CoreForms } from '@singletons/form';
import { CoreRecaptchaComponent } from '@components/recaptcha/recaptcha';
import { CorePath } from '@singletons/path';
import { CoreDom } from '@singletons/dom';
import { CoreSitesFactory } from '@services/sites-factory';
import { EMAIL_SIGNUP_FEATURE_NAME } from '@features/login/constants';

/**
* Page to signup using email.
Expand Down Expand Up @@ -238,10 +238,7 @@ export class CoreLoginEmailSignupPage implements OnInit {
* @returns True if success.
*/
protected async treatSiteConfig(): Promise<boolean> {
if (
this.siteConfig?.registerauth == 'email' &&
!this.site.isFeatureDisabled(CoreLoginHelperProvider.EMAIL_SIGNUP_FEATURE_NAME)
) {
if (this.siteConfig?.registerauth == 'email' && !this.site.isFeatureDisabled(EMAIL_SIGNUP_FEATURE_NAME)) {
this.siteName = await this.site.getSiteName();

this.authInstructions = this.siteConfig.authinstructions;
Expand Down
5 changes: 3 additions & 2 deletions src/core/features/login/pages/reconnect/reconnect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import { CoreNetwork } from '@services/network';
import { CoreSiteBasicInfo, CoreSites, CoreSitesReadingStrategy } from '@services/sites';
import { CoreDomUtils } from '@services/utils/dom';
import { CoreUtils } from '@services/utils/utils';
import { CoreLoginHelper, CoreLoginHelperProvider } from '@features/login/services/login-helper';
import { CoreLoginHelper } from '@features/login/services/login-helper';
import { CoreSite } from '@classes/sites/site';
import { CoreEvents } from '@singletons/events';
import { CoreError } from '@classes/errors/error';
Expand All @@ -32,6 +32,7 @@ import { CoreUserAuthenticatedSupportConfig } from '@features/user/classes/suppo
import { Translate } from '@singletons';
import { SafeHtml } from '@angular/platform-browser';
import { CoreSitePublicConfigResponse } from '@classes/sites/unauthenticated-site';
import { FORGOTTEN_PASSWORD_FEATURE_NAME } from '@features/login/constants';

/**
* Page to enter the user password to reconnect to a site.
Expand Down Expand Up @@ -171,7 +172,7 @@ export class CoreLoginReconnectPage implements OnInit, OnDestroy {
return;
}

this.showForgottenPassword = !this.site.isFeatureDisabled(CoreLoginHelperProvider.FORGOTTEN_PASSWORD_FEATURE_NAME);
this.showForgottenPassword = !this.site.isFeatureDisabled(FORGOTTEN_PASSWORD_FEATURE_NAME);
this.exceededAttemptsHTML = CoreLoginHelper.buildExceededAttemptsHTML(
!!this.supportConfig?.canContactSupport(),
this.showForgottenPassword,
Expand Down
6 changes: 3 additions & 3 deletions src/core/features/login/pages/site/site.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import { CoreUtils } from '@services/utils/utils';
import { CoreDomUtils } from '@services/utils/dom';
import {
CoreLoginHelper,
CoreLoginHelperProvider,
CoreLoginSiteFinderSettings,
CoreLoginSiteSelectorListMethod,
} from '@features/login/services/login-helper';
Expand All @@ -48,6 +47,7 @@ import { CoreLoginError } from '@classes/errors/loginerror';
import { CorePlatform } from '@services/platform';
import { CoreReferrer } from '@services/referrer';
import { CoreSitesFactory } from '@services/sites-factory';
import { ONBOARDING_DONE } from '@features/login/constants';

/**
* Site (url) chooser when adding a new site.
Expand Down Expand Up @@ -110,7 +110,7 @@ export class CoreLoginSitePage implements OnInit {

if (showOnboarding) {
// Don't display onboarding in this case, and don't display it again later.
CoreConfig.set(CoreLoginHelperProvider.ONBOARDING_DONE, 1);
CoreConfig.set(ONBOARDING_DONE, 1);
}
} else if (showOnboarding) {
this.initOnboarding();
Expand Down Expand Up @@ -191,7 +191,7 @@ export class CoreLoginSitePage implements OnInit {
* @returns Promise resolved when done.
*/
protected async initOnboarding(): Promise<void> {
const onboardingDone = await CoreConfig.get(CoreLoginHelperProvider.ONBOARDING_DONE, false);
const onboardingDone = await CoreConfig.get(ONBOARDING_DONE, false);

if (!onboardingDone) {
// Check onboarding.
Expand Down
47 changes: 22 additions & 25 deletions src/core/features/login/services/login-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,25 +48,22 @@ import {
CoreUnauthenticatedSite,
TypeOfLogin,
} from '@classes/sites/unauthenticated-site';

const PASSWORD_RESETS_CONFIG_KEY = 'password-resets';

export const GET_STARTED_URL = 'https://moodle.com';
import {
EMAIL_SIGNUP_FEATURE_NAME,
FAQ_QRCODE_IMAGE_HTML,
FAQ_QRCODE_INFO_DONE,
FORGOTTEN_PASSWORD_FEATURE_NAME,
IDENTITY_PROVIDERS_FEATURE_NAME,
IDENTITY_PROVIDER_FEATURE_NAME_PREFIX,
} from '../constants';

/**
* Helper provider that provides some common features regarding authentication.
*/
@Injectable({ providedIn: 'root' })
export class CoreLoginHelperProvider {

static readonly ONBOARDING_DONE = 'onboarding_done';
static readonly FAQ_QRCODE_INFO_DONE = 'qrcode_info_done';
static readonly FAQ_URL_IMAGE_HTML = '<img src="assets/img/login/faq_url.png" role="presentation" alt="">';
static readonly FAQ_QRCODE_IMAGE_HTML = '<img src="assets/img/login/faq_qrcode.png" role="presentation" alt="">';
static readonly EMAIL_SIGNUP_FEATURE_NAME = 'CoreLoginEmailSignup';
static readonly FORGOTTEN_PASSWORD_FEATURE_NAME = 'NoDelegate_ForgottenPassword';
static readonly IDENTITY_PROVIDERS_FEATURE_NAME = 'NoDelegate_IdentityProviders';
static readonly IDENTITY_PROVIDER_FEATURE_NAME_PREFIX = 'NoDelegate_IdentityProvider_';
protected static readonly PASSWORD_RESETS_CONFIG_KEY = 'password-resets';

protected logger: CoreLogger;
protected sessionExpiredCheckingSite: Record<string, boolean> = {};
Expand Down Expand Up @@ -418,7 +415,7 @@ export class CoreLoginHelperProvider {
return [];
}
// eslint-disable-next-line deprecation/deprecation
if (this.isFeatureDisabled(CoreLoginHelperProvider.IDENTITY_PROVIDERS_FEATURE_NAME, siteConfig)) {
if (this.isFeatureDisabled(IDENTITY_PROVIDERS_FEATURE_NAME, siteConfig)) {
// Identity providers are disabled, return an empty list.
return [];
}
Expand All @@ -435,7 +432,7 @@ export class CoreLoginHelperProvider {
provider.url &&
(provider.url.indexOf(httpsUrl) != -1 || provider.url.indexOf(httpUrl) != -1) &&
!this.isFeatureDisabled( // eslint-disable-line deprecation/deprecation
CoreLoginHelperProvider.IDENTITY_PROVIDER_FEATURE_NAME_PREFIX + urlParams.id,
IDENTITY_PROVIDER_FEATURE_NAME_PREFIX + urlParams.id,
siteConfig,
)
) {
Expand All @@ -459,7 +456,7 @@ export class CoreLoginHelperProvider {
return [];
}

if (site.isFeatureDisabled(CoreLoginHelperProvider.IDENTITY_PROVIDERS_FEATURE_NAME)) {
if (site.isFeatureDisabled(IDENTITY_PROVIDERS_FEATURE_NAME)) {
// Identity providers are disabled, return an empty list.
return [];
}
Expand All @@ -473,7 +470,7 @@ export class CoreLoginHelperProvider {
const urlParams = CoreUrlUtils.extractUrlParams(provider.url);

if (provider.url && (provider.url.indexOf(httpsUrl) != -1 || provider.url.indexOf(httpUrl) != -1) &&
!site.isFeatureDisabled(CoreLoginHelperProvider.IDENTITY_PROVIDER_FEATURE_NAME_PREFIX + urlParams.id)) {
!site.isFeatureDisabled(IDENTITY_PROVIDER_FEATURE_NAME_PREFIX + urlParams.id)) {
validProviders.push(provider);
}
});
Expand Down Expand Up @@ -571,7 +568,7 @@ export class CoreLoginHelperProvider {
*/
isEmailSignupDisabled(config?: CoreSitePublicConfigResponse): boolean {
// eslint-disable-next-line deprecation/deprecation
return this.isFeatureDisabled(CoreLoginHelperProvider.EMAIL_SIGNUP_FEATURE_NAME, config);
return this.isFeatureDisabled(EMAIL_SIGNUP_FEATURE_NAME, config);
}

/**
Expand Down Expand Up @@ -617,7 +614,7 @@ export class CoreLoginHelperProvider {
*/
isForgottenPasswordDisabled(config?: CoreSitePublicConfigResponse): boolean {
// eslint-disable-next-line deprecation/deprecation
return this.isFeatureDisabled(CoreLoginHelperProvider.FORGOTTEN_PASSWORD_FEATURE_NAME, config);
return this.isFeatureDisabled(FORGOTTEN_PASSWORD_FEATURE_NAME, config);
}

/**
Expand Down Expand Up @@ -1294,14 +1291,14 @@ export class CoreLoginHelperProvider {
* @returns Promise resolved if the user accepts to scan QR.
*/
async showScanQRInstructions(): Promise<void> {
const dontShowWarning = await CoreConfig.get(CoreLoginHelperProvider.FAQ_QRCODE_INFO_DONE, 0);
const dontShowWarning = await CoreConfig.get(FAQ_QRCODE_INFO_DONE, 0);
if (dontShowWarning) {
return;
}

const message = Translate.instant(
'core.login.faqwhereisqrcodeanswer',
{ $image: '<div class="text-center">'+ CoreLoginHelperProvider.FAQ_QRCODE_IMAGE_HTML + '</div>' },
{ $image: '<div class="text-center">'+ FAQ_QRCODE_IMAGE_HTML + '</div>' },
);
const header = Translate.instant('core.login.faqwhereisqrcode');

Expand All @@ -1315,7 +1312,7 @@ export class CoreLoginHelperProvider {
);

if (dontShowAgain) {
CoreConfig.set(CoreLoginHelperProvider.FAQ_QRCODE_INFO_DONE, 1);
CoreConfig.set(FAQ_QRCODE_INFO_DONE, 1);
}
} catch {
// User canceled.
Expand Down Expand Up @@ -1484,7 +1481,7 @@ export class CoreLoginHelperProvider {

passwordResets[siteUrl] = Date.now();

await CoreConfig.set(PASSWORD_RESETS_CONFIG_KEY, JSON.stringify(passwordResets));
await CoreConfig.set(CoreLoginHelperProvider.PASSWORD_RESETS_CONFIG_KEY, JSON.stringify(passwordResets));
}

/**
Expand Down Expand Up @@ -1516,9 +1513,9 @@ export class CoreLoginHelperProvider {
}

if (Object.values(passwordResets).length === 0) {
await CoreConfig.delete(PASSWORD_RESETS_CONFIG_KEY);
await CoreConfig.delete(CoreLoginHelperProvider.PASSWORD_RESETS_CONFIG_KEY);
} else {
await CoreConfig.set(PASSWORD_RESETS_CONFIG_KEY, JSON.stringify(passwordResets));
await CoreConfig.set(CoreLoginHelperProvider.PASSWORD_RESETS_CONFIG_KEY, JSON.stringify(passwordResets));
}
}

Expand Down Expand Up @@ -1562,7 +1559,7 @@ export class CoreLoginHelperProvider {
* @returns Password resets.
*/
protected async getPasswordResets(): Promise<Record<string, number>> {
const passwordResetsJson = await CoreConfig.get(PASSWORD_RESETS_CONFIG_KEY, '{}');
const passwordResetsJson = await CoreConfig.get(CoreLoginHelperProvider.PASSWORD_RESETS_CONFIG_KEY, '{}');

return CoreTextUtils.parseJSON<Record<string, number>>(passwordResetsJson, {});
}
Expand Down
6 changes: 3 additions & 3 deletions src/core/features/settings/pages/dev/dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

import { CoreConstants } from '@/core/constants';
import { Component, OnInit } from '@angular/core';
import { CoreLoginHelperProvider } from '@features/login/services/login-helper';
import { FAQ_QRCODE_INFO_DONE, ONBOARDING_DONE } from '@features/login/constants';
import { CoreSettingsHelper } from '@features/settings/services/settings-helper';
import { CoreSitePlugins } from '@features/siteplugins/services/siteplugins';
import { CoreUserTours } from '@features/usertours/services/user-tours';
Expand Down Expand Up @@ -174,8 +174,8 @@ export class CoreSettingsDevPage implements OnInit {
async resetUserTours(): Promise<void> {
await CoreUserTours.resetTours();

await CoreConfig.delete(CoreLoginHelperProvider.ONBOARDING_DONE);
await CoreConfig.delete(CoreLoginHelperProvider.FAQ_QRCODE_INFO_DONE);
await CoreConfig.delete(ONBOARDING_DONE);
await CoreConfig.delete(FAQ_QRCODE_INFO_DONE);

CoreDomUtils.showToast('User tours have been reseted');
}
Expand Down
4 changes: 2 additions & 2 deletions src/testing/services/behat-runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import { TestingBehatDomUtils } from './behat-dom';
import { TestingBehatBlocking } from './behat-blocking';
import { CoreCustomURLSchemes, CoreCustomURLSchemesProvider } from '@services/urlschemes';
import { CoreLoginHelperProvider } from '@features/login/services/login-helper';
import { ONBOARDING_DONE } from '@features/login/constants';
import { CoreConfig } from '@services/config';
import { EnvironmentConfig } from '@/types/config';
import { LocalNotifications, makeSingleton, NgZone } from '@singletons';
Expand Down Expand Up @@ -77,7 +77,7 @@ export class TestingBehatRuntimeService {
TestingBehatBlocking.init();

if (options.skipOnBoarding) {
CoreConfig.set(CoreLoginHelperProvider.ONBOARDING_DONE, 1);
CoreConfig.set(ONBOARDING_DONE, 1);
}

if (options.configOverrides) {
Expand Down

0 comments on commit 006f901

Please sign in to comment.