Skip to content
This repository has been archived by the owner on Apr 9, 2024. It is now read-only.

Commit

Permalink
feat: expose customisable messages on useKeycloak
Browse files Browse the repository at this point in the history
  • Loading branch information
Creaous committed Mar 15, 2024
1 parent baa6d21 commit 5665a39
Showing 1 changed file with 21 additions and 18 deletions.
39 changes: 21 additions & 18 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,6 @@ import { createClient } from 'redis';

export type KeycloakPluginOptions = KeycloakPluginOptionsBase;

/**
* Represents the options for the Keycloak plugin.
*/
/**
* Represents the options for the Keycloak plugin.
*/
/**
* Represents the options for the Keycloak plugin.
*/
Expand Down Expand Up @@ -52,6 +46,16 @@ export interface KeycloakPluginOptionsBase {
*/
requireAuth?: boolean;

/**
* The messages to use for different authentication errors.
*/
messages?: {
invalidToken: string;
expiredToken: string;
invalidPermissions: string;
authRequired: string;
};

/**
* A function that retrieves the token for authentication.
* @param params - The parameters for token retrieval.
Expand Down Expand Up @@ -79,6 +83,13 @@ export function useKeycloak(options: KeycloakPluginOptions): Plugin {
cachePrefix = 'tokens',
allowedRoles = [],
requireAuth = false,
messages = {
invalidToken: 'The provided access token is invalid.',
expiredToken: 'An invalid or expired access token was provided.',
invalidPermissions:
'You do not have the necessary permissions to access this resource.',
authRequired: 'Authentication is required to access this resource.'
},
getToken = defaultGetToken
} = options;

Expand Down Expand Up @@ -111,9 +122,7 @@ export function useKeycloak(options: KeycloakPluginOptions): Plugin {
);
} catch (ex) {
// If the token is invalid, throw an unauthorized error
throw unauthorizedError(
'The provided access token is invalid.'
);
throw unauthorizedError(messages.invalidToken);
}
}

Expand All @@ -122,9 +131,7 @@ export function useKeycloak(options: KeycloakPluginOptions): Plugin {

// If the token is not found in the cache, throw an unauthorized error
if (!ct) {
throw unauthorizedError(
`An invalid or expired access token was provided.`
);
throw unauthorizedError(messages.expiredToken);
}

// Check if the token has the necessary roles
Expand All @@ -135,9 +142,7 @@ export function useKeycloak(options: KeycloakPluginOptions): Plugin {
allowedRoles.includes(role)
)
) {
throw unauthorizedError(
`You do not have the necessary permissions to access this resource.`
);
throw unauthorizedError(messages.invalidPermissions);
}
}

Expand All @@ -146,9 +151,7 @@ export function useKeycloak(options: KeycloakPluginOptions): Plugin {
} else {
// If authentication is required, throw an unauthorized error
if (requireAuth) {
throw unauthorizedError(
'Authentication is required to access this resource.'
);
throw unauthorizedError(messages.authRequired);
}
}
},
Expand Down

0 comments on commit 5665a39

Please sign in to comment.