Skip to content

Commit

Permalink
feat(ngx-auth)!: add mobileScheme option to schematic
Browse files Browse the repository at this point in the history
BREAKING CHANGE: `schemeUri` setting is now `mobileScheme`
  • Loading branch information
Badisi committed Sep 1, 2022
1 parent badc29b commit 7d852a9
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 19 deletions.
2 changes: 1 addition & 1 deletion projects/auth-js/oidc/models/oidc-auth-settings.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ type UsefulSettings = 'scope' | 'loadUserInfo' | 'automaticSilentRenew';
export interface OIDCAuthSettings extends CoreAuthSettings, Pick<UserManagerSettings, UsefulSettings> {
authorityUrl: string;
clientId: string;
schemeUri?: string;
mobileScheme?: string;
retrieveUserSession?: boolean;
navigationType?: Navigation;
logLevel?: Log;
Expand Down
2 changes: 1 addition & 1 deletion projects/auth-js/oidc/oidc-auth-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ export class OIDCAuthManager extends AuthManager<OIDCAuthSettings> {
* Providers like Keycloak does not handle custom redirect urls like `demo-app://?oidc-callback=login`,
* because they lack a host name. To fix this, `demo-app://localhost/?oidc-callback=login` is used instead.
*/
const baseUrl = (isNativeMobile) ? `${userSettings.schemeUri}://localhost/` : AuthUtils.getBaseUrl();
const baseUrl = (isNativeMobile) ? `${userSettings.mobileScheme}://localhost/` : AuthUtils.getBaseUrl();

// Initialize settings
this.settings = merge({}, DEFAULT_SETTINGS, {
Expand Down
4 changes: 2 additions & 2 deletions projects/demo-app/web/common/settings/default-settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export const DEFAULT_AUTH_JS_SETTINGS = (production = false): UserSettings<OIDCA
librarySettings: {
authorityUrl: 'https://dev-fijd1e9x.us.auth0.com',
clientId: 'kRVVEnAWKMpxxpcodl0TqLXfIHgQvmmt',
schemeUri: 'demo-app',
mobileScheme: 'demo-app',
scope: 'openid profile email phone offline_access read:current_user',
internal: {
extraQueryParams: {
Expand All @@ -32,7 +32,7 @@ export const DEFAULT_AUTH_JS_SETTINGS = (production = false): UserSettings<OIDCA
librarySettings: {
authorityUrl: 'http://localhost:8080/auth/realms/demo',
clientId: 'demo-app',
schemeUri: 'demo-app',
mobileScheme: 'demo-app',
scope: 'openid profile email phone',
navigationType: Navigation.REDIRECT,
logLevel: Log.NONE,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ export const AUTH_JS_LIBRARY_SETTINGS_DEFINITION: LibrarySettingsDefinitionItem<
type: 'string',
required: true
}, {
name: 'schemeUri',
label: 'Scheme uri',
name: 'mobileScheme',
label: 'Custom mobile scheme name',
type: 'string'
}, {
name: 'scope',
Expand Down
26 changes: 15 additions & 11 deletions projects/ngx-auth/schematics/install/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,25 @@ import { JSONFile } from '@schematics/angular/utility/json-file';
import { dedent } from '../utils/dedent';
import { InstallOptions } from './install-options';

const getActualUserConfig = async (context: SchematicContext, options: InstallOptions): Promise<InstallOptions> => {
const sanitizeOptions = async (context: SchematicContext, userOptions: InstallOptions): Promise<Omit<InstallOptions, 'project'>> => {
const defaultOptions = await getSchematicSchemaDefaultOptions(context, 'install');

const config: InstallOptions = {
project: 'app-test',
authorityUrl: options.authorityUrl,
clientId: options.clientId
// `project` is omitted as it is only required by the schematic
const options: Omit<InstallOptions, 'project'> = {
authorityUrl: userOptions.authorityUrl,
clientId: userOptions.clientId
};
(['loginRequired', 'retrieveUserSession', 'loadUserInfo'] as (keyof InstallOptions)[]).forEach(name => {
if (options[name] !== defaultOptions[name]) {

// keeps only the user's options that are different from the defaults
const optionKeys: (keyof InstallOptions)[] = ['mobileScheme', 'loginRequired', 'retrieveUserSession', 'loadUserInfo'];
optionKeys.forEach(name => {
if (userOptions[name] && (userOptions[name] !== defaultOptions[name])) {
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-explicit-any
(config as any)[name] = options[name];
(options as any)[name] = userOptions[name];
}
});
return config;

return options;
};

export default (options: InstallOptions): Rule =>
Expand Down Expand Up @@ -62,7 +66,7 @@ export default (options: InstallOptions): Rule =>
const initContent = dedent`
/**
* Auth configuration
* @see https://badisi.github.io/auth-js/site/docs/configuration
* @see https://badisi.github.io/auth-js/site/documentation/configuration
*/
initAuth().then(authProvider => {
platformBrowserDynamic([
Expand Down Expand Up @@ -92,7 +96,7 @@ export default (options: InstallOptions): Rule =>
}

// Update the configuration
const config = await getActualUserConfig(context.schematicContext, options);
const config = await sanitizeOptions(context.schematicContext, options);
const configContent = JSON.stringify(config, null, 2).replace(/"([^"]+)":/g, '$1:').replace(/"/g, '\'');
rules.push(
replaceInFile(mainTsPath, /initAuth\((.*?)\)/sm, `initAuth(${configContent})`),
Expand Down
1 change: 1 addition & 0 deletions projects/ngx-auth/schematics/install/install-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ export class InstallOptions {
public project!: string;
public authorityUrl!: string;
public clientId!: string;
public mobileScheme?: string;
public loginRequired?: boolean;
public retrieveUserSession?: boolean;
public loadUserInfo?: boolean;
Expand Down
8 changes: 7 additions & 1 deletion projects/ngx-auth/schematics/install/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,20 @@
"x-prompt": "Authority url ?",
"description": "The authorization server DNS host name or IP address.",
"type": "string",
"minLength": 1
"minLength": 1,
"format": "uri"
},
"clientId": {
"x-prompt": "Client id ?",
"description": "The public client application's identifier assigned by the authorization server.",
"type": "string",
"minLength": 1
},
"mobileScheme": {
"x-prompt": "Custom scheme name ? (optional, only for mobile)",
"description": "The custom Scheme URI's identifier assigned to the native mobile application.",
"type": "string"
},
"loginRequired": {
"x-prompt": "Is authentication required at startup ?",
"description": "Whether an authentication should be required during the initialization phase.",
Expand Down
2 changes: 1 addition & 1 deletion projects/site/docs/documentation/configuration.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -101,5 +101,5 @@ automaticInjectToken: {

## Mobile

#### `schemeUri`
#### `mobileScheme`
> **type**: string<br/>

0 comments on commit 7d852a9

Please sign in to comment.