Skip to content

Commit

Permalink
feat(next): sign out
Browse files Browse the repository at this point in the history
  • Loading branch information
wangsijie committed Jul 20, 2022
1 parent 60eb143 commit a8a7f71
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 2 deletions.
3 changes: 3 additions & 0 deletions packages/next-sample/pages/api/sign-out.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { logtoClient } from '../../libraries/logto';

export default logtoClient.handleSignOut();
3 changes: 2 additions & 1 deletion packages/next-sample/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
"extends": "@silverhand/ts-config-react/tsconfig.base",
"compilerOptions": {
"jsx": "preserve",
"incremental": true
"incremental": true,
"allowJs": true // added by next cli
},
"include": [
"next-env.d.ts",
Expand Down
22 changes: 22 additions & 0 deletions packages/next/src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const handleSignInCallback = jest.fn();
const getIdTokenClaims = jest.fn(() => ({
sub: 'user_id',
}));
const signOut = jest.fn();

jest.mock('./storage', () =>
jest.fn(() => ({
Expand All @@ -48,6 +49,10 @@ jest.mock('@logto/node', () =>
handleSignInCallback,
isAuthenticated: true,
getIdTokenClaims,
signOut: () => {
navigate(configs.baseUrl);
signOut();
},
}))
);

Expand Down Expand Up @@ -109,4 +114,21 @@ describe('Next', () => {
expect(getIdTokenClaims).toHaveBeenCalled();
});
});

describe('handleSignOut', () => {
it('should redirect to Logto sign out url', async () => {
const client = new LogtoClient(configs);
await testApiHandler({
handler: client.handleSignOut(),
url: '/api/sign-out',
test: async ({ fetch }) => {
const response = await fetch({ method: 'GET', redirect: 'manual' });
const headers = response.headers as Map<string, string>;
expect(headers.get('location')).toEqual(`${configs.baseUrl}/`);
},
});
expect(save).toHaveBeenCalled();
expect(signOut).toHaveBeenCalled();
});
});
});
15 changes: 14 additions & 1 deletion packages/next/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export default class LogtoClient {
private storage?: NextStorage;
constructor(private readonly config: LogtoNextConfig) {}

handleSignIn = (redirectUri = `${this.config.baseUrl}/api/sign-in`): NextApiHandler =>
handleSignIn = (redirectUri = `${this.config.baseUrl}/api/sign-in-callback`): NextApiHandler =>
this.withIronSession(async (request, response) => {
const nodeClient = this.createNodeClient(request);
await nodeClient.signIn(redirectUri);
Expand All @@ -34,6 +34,19 @@ export default class LogtoClient {
}
});

handleSignOut = (redirectUri = this.config.baseUrl): NextApiHandler =>
this.withIronSession(async (request, response) => {
const nodeClient = this.createNodeClient(request);
await nodeClient.signOut(redirectUri);

request.session.destroy();
await this.storage?.save();

if (this.navigateUrl) {
response.redirect(this.navigateUrl);
}
});

handleUser = () =>
this.withLogtoApiRoute((request, response) => {
response.json(request.user);
Expand Down

0 comments on commit a8a7f71

Please sign in to comment.