Skip to content

Commit

Permalink
fix: remove persist access token (#406)
Browse files Browse the repository at this point in the history
  • Loading branch information
wangsijie authored Sep 20, 2022
1 parent 952fed1 commit f2ba84f
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 43 deletions.
12 changes: 5 additions & 7 deletions packages/client/src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,7 @@ describe('LogtoClient', () => {
});
});

describe('getAccessToken when persistAccessToken is true', () => {
describe('getAccessToken', () => {
it('should load access token', async () => {
const logtoClient = createClient(
undefined,
Expand All @@ -415,8 +415,7 @@ describe('LogtoClient', () => {
expiresAt: Date.now() + 1000,
},
}),
}),
true
})
);

await expect(logtoClient.getAccessToken()).resolves.toEqual(accessToken);
Expand All @@ -434,8 +433,7 @@ describe('LogtoClient', () => {
expiresAt: Date.now() + 1000,
},
}),
}),
true
})
);

await expect(logtoClient.getAccessToken()).rejects.toThrow();
Expand All @@ -451,14 +449,14 @@ describe('LogtoClient', () => {
scope: 'read register manage',
expiresIn: 3600,
}));
const logtoClient = createClient(undefined, storage, true);
const logtoClient = createClient(undefined, storage);
await logtoClient.signIn(redirectUri);
const code = `code_value`;
const callbackUri = `${redirectUri}?code=${code}&state=${mockedState}&codeVerifier=${mockedCodeVerifier}`;
await logtoClient.handleSignInCallback(callbackUri);

await storage.removeItem('refreshToken');
const anotherClient = createClient(undefined, storage, true);
const anotherClient = createClient(undefined, storage);

await expect(anotherClient.getAccessToken()).resolves.not.toThrow();
});
Expand Down
8 changes: 1 addition & 7 deletions packages/client/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,7 @@ export default class LogtoClient {
};
this.adapter = adapter;

if (this.logtoConfig.persistAccessToken) {
void this.loadAccessTokenMap();
}
void this.loadAccessTokenMap();
}

async isAuthenticated() {
Expand Down Expand Up @@ -369,10 +367,6 @@ export default class LogtoClient {
}

private async saveAccessTokenMap() {
if (!this.logtoConfig.persistAccessToken) {
return;
}

const data: Record<string, AccessToken> = {};

for (const [key, accessToken] of this.accessTokenMap.entries()) {
Expand Down
8 changes: 2 additions & 6 deletions packages/client/src/mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,13 +104,9 @@ export const createAdapters = () => ({
generateState,
});

export const createClient = (
prompt?: Prompt,
storage = new MockedStorage(),
persistAccessToken?: boolean
) =>
export const createClient = (prompt?: Prompt, storage = new MockedStorage()) =>
new LogtoClient(
{ endpoint, appId, prompt, persistAccessToken },
{ endpoint, appId, prompt },
{
...createAdapters(),
storage,
Expand Down
1 change: 0 additions & 1 deletion packages/client/src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ export type LogtoConfig = {
scopes?: string[];
resources?: string[];
prompt?: Prompt;
persistAccessToken?: boolean;
};

export const AccessTokenSchema = type({
Expand Down
16 changes: 5 additions & 11 deletions packages/express/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,12 @@ const createNodeClient = (

const storage = new ExpressStorage(request);

return new NodeClient(
{
...config,
persistAccessToken: config.persistAccessToken ?? true,
return new NodeClient(config, {
storage,
navigate: (url) => {
response.redirect(url);
},
{
storage,
navigate: (url) => {
response.redirect(url);
},
}
);
});
};

export const handleAuthRoutes = (config: LogtoExpressConfig): Router => {
Expand Down
16 changes: 5 additions & 11 deletions packages/next/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,18 +107,12 @@ export default class LogtoClient {
private createNodeClient(request: IncomingMessage) {
this.storage = new NextStorage(request);

return new NodeClient(
{
...this.config,
persistAccessToken: this.config.persistAccessToken ?? true,
return new NodeClient(this.config, {
storage: this.storage,
navigate: (url) => {
this.navigateUrl = url;
},
{
storage: this.storage,
navigate: (url) => {
this.navigateUrl = url;
},
}
);
});
}

private get ironSessionConfigs() {
Expand Down

0 comments on commit f2ba84f

Please sign in to comment.