Skip to content

Commit

Permalink
Improves method order
Browse files Browse the repository at this point in the history
  • Loading branch information
AlbertoElias committed Oct 16, 2024
1 parent fc8233f commit 357bf4d
Showing 1 changed file with 29 additions and 29 deletions.
58 changes: 29 additions & 29 deletions packages/server/src/auth/CrossmintAuth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,38 @@ export class CrossmintAuth {
return new CrossmintAuth(crossmint);
}

public getJWKSUri() {
return `${this.apiClient.baseUrl}/.well-known/jwks.json`;
public async getSession(options: GenericRequest | AuthMaterialBasic): Promise<AuthSession> {
const { jwt, refreshToken } = "refreshToken" in options ? options : getAuthCookies(options);

if (!refreshToken) {
throw new CrossmintAuthenticationError("Refresh token not found");
}

try {
return await this.validateOrRefreshSession(jwt, refreshToken);
} catch (error) {
console.error("Failed to get session", error);
throw new CrossmintAuthenticationError("Failed to get session");
}
}

async getUser(externalUserId: string) {
const result = await this.apiClient.get(`api/${CROSSMINT_API_VERSION}/sdk/auth/user/${externalUserId}`, {
headers: {
"Content-Type": "application/json",
},
});

const user = await result.json();
return user;
}

public verifyCrossmintJwtToken(token: string) {
return verifyCrossmintJwtToken(token, this.getJWKSUri());
return verifyCrossmintJwtToken(token, this.getJwksUri());
}

public getJwksUri() {
return `${this.apiClient.baseUrl}/.well-known/jwks.json`;
}

private async refreshAuthMaterial(refreshToken: string): Promise<AuthMaterial> {
Expand All @@ -53,21 +79,6 @@ export class CrossmintAuth {
};
}

public async getSession(options: GenericRequest | AuthMaterialBasic): Promise<AuthSession> {
const { jwt, refreshToken } = "refreshToken" in options ? options : getAuthCookies(options);

if (!refreshToken) {
throw new CrossmintAuthenticationError("Refresh token not found");
}

try {
return await this.validateOrRefreshSession(jwt, refreshToken);
} catch (error) {
console.error("Failed to get session", error);
throw new CrossmintAuthenticationError("Failed to get session");
}
}

private async validateOrRefreshSession(jwt: string | undefined, refreshToken: string): Promise<AuthSession> {
if (jwt) {
try {
Expand All @@ -87,15 +98,4 @@ export class CrossmintAuth {
userId: refreshedAuthMaterial.user.id,
};
}

async getUser(externalUserId: string) {
const result = await this.apiClient.get(`api/${CROSSMINT_API_VERSION}/sdk/auth/user/${externalUserId}`, {
headers: {
"Content-Type": "application/json",
},
});

const user = await result.json();
return user;
}
}

0 comments on commit 357bf4d

Please sign in to comment.