class MyAuthClient extends AuthClient {
newParam?: string;
constructor(options: { newParam?: string } & AuthClientOptions) {
super(options);
this.newParam = options.newParam;
}
// setter for newParam
setNewParam(newParam: string) {
this.newParam = newParam;
}
/**
* Add new param to authorize request.
*/
_buildAuthorizeUrl(
params: { newParam?: string } & AuthorizeParams
): string {
params.newParam = this.newParam;
const authUrl = `${this.baseURL}/oauth/authorize?${qs.stringify(params, {
skipNull: true,
skipEmptyString: true,
})}`;
this._debugLog(`Built authorize URL: ${authUrl}`);
return authUrl;
}
/**
* Builds a token cache key.
*/
_buildTokenCacheKey(scope?: string): DefaultTokenCacheKey {
scope = scope || this.scope;
if (this.useRefreshTokens && scope.indexOf('OFFLINE_ACCESS') === -1) {
scope = `${scope} OFFLINE_ACCESS`;
}
const key = new DefaultTokenCacheKey({
accountId: this.accountId,
clientId: this.clientId,
scope: scope || this.scope,
additionalArguments: [this.newParam],
});
this._debugLog(
`Building token cache key with newParam: ${key.stringify()}`
);
return key;
}
/**
* Saves the auth token to the token cache.
*/
_saveAuthToken(
result: RequestAccessTokenResult,
clientId?: string,
scope?: string
): void {
const args = {
accountId: this.accountId,
accessToken: result.accessToken,
clientId: clientId || this.clientId,
scope: scope ?? this.scope,
session: result.session,
additionalKeyArguments: [this.newParam],
};
this._debugLog(
`Saving auth token with newParam: ${JSON.stringify(args)}`
);
this.tokenCache.save(args);
}
}
const AuthProviderInner: FC<AuthProviderInnerProps> = props => {
// ...truncated for example
const { state } = useContext(MyContext);
const myParamValue = state?.someValue;
const initializeClient = useCallback(
(options: AuthClientOptions) => {
const client = new MyAuthClient({ ...options });
setNewParam(client);
if (myParamValue) {
client.setNewParam(myParamValue);
}
return client;
},
[myParamValue]
);
return (
<AuthProvider
initializeClient={initializeClient}
>
<AuthSessionManager>{children}</AuthSessionManager>
</AuthProvider>
);
};