Function defineJWTAuth
- defineJWTAuth<Cred>(definition): JWTAuthDefinition<Cred>
Type Parameters
Cred extends AnyAppCredential
Parameters
definition: JWTAuthDefinitionInput<Cred>
Returns JWTAuthDefinition<Cred>
See
Example
import { AppCredential, defineJWTAuth } from "@rollout/framework";
export type MyAppCredential = AppCredential<{
accessToken: string;
refreshToken: string;
expiresAt: string;
}>;
export const auth = defineJWTAuth<MyAppCredential>({
async getCredentialFromJWT({ consumer, authData }) {
// validate/transform/parse authData if needed
const credentialData = authData as MyAppCredential["data"];
return {
// we assume there is only one credential per consumer,
// so we just use `consumerKey` as `credentialKey`.
credentialKey: consumer.consumerKey,
data: credentialData,
};
},
async refreshTokens({ credential }) {
if (!needsRefresh(new Date(credential.data.expiresAt))) {
// No refresh needed
return credential.data;
}
// Token needs refreshing
const newTokens = await refreshMyTokens({ credential });
return newTokens;
},
async getCredentialOption({ credential }) {
// Fetch user name and avatar
return { label, avatar };
},
});
Used to define a Connector JWT Token Auth