Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(next): sign out #358

Merged
merged 1 commit into from
Jul 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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' });
wangsijie marked this conversation as resolved.
Show resolved Hide resolved
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 =>
charIeszhao marked this conversation as resolved.
Show resolved Hide resolved
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);
wangsijie marked this conversation as resolved.
Show resolved Hide resolved
await nodeClient.signOut(redirectUri);

request.session.destroy();
await this.storage?.save();
wangsijie marked this conversation as resolved.
Show resolved Hide resolved

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

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