Skip to content

Commit db76111

Browse files
committed
feat(auth): refactor auth generation to wait for tokens and redirect to cli-log-in page
1 parent 2c0a584 commit db76111

File tree

1 file changed

+20
-8
lines changed

1 file changed

+20
-8
lines changed

src/commands/auth/login.ts

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { createInterface } from 'node:readline';
44
import { URL } from 'node:url';
55
import { Command } from '@oclif/core';
66
import { ensureUserSetup } from '../../api/user-setup.client.ts';
7-
import { OAUTH_CALLBACK_ERROR_CODES } from '../../config/constants.ts';
7+
import { EOL_LOG_IN_URL, OAUTH_CALLBACK_ERROR_CODES } from '../../config/constants.ts';
88
import { refreshIdentityFromStoredToken } from '../../service/analytics.svc.ts';
99
import { persistTokenResponse } from '../../service/auth.svc.ts';
1010
import { getClientId, getRealmUrl } from '../../service/auth-config.svc.ts';
@@ -40,8 +40,7 @@ export default class AuthLogin extends Command {
4040
`&code_challenge_method=S256` +
4141
`&state=${state}`;
4242

43-
const code = await this.startServerAndAwaitCode(authUrl, state);
44-
const token = await this.exchangeCodeForToken(code, codeVerifier);
43+
const token = await this.startServerAndAwaitToken(authUrl, state, codeVerifier);
4544

4645
try {
4746
await persistTokenResponse(token);
@@ -65,7 +64,11 @@ export default class AuthLogin extends Command {
6564
this.log('\nLogin completed successfully.');
6665
}
6766

68-
private startServerAndAwaitCode(authUrl: string, expectedState: string): Promise<string> {
67+
private startServerAndAwaitToken(
68+
authUrl: string,
69+
expectedState: string,
70+
codeVerifier: string,
71+
): Promise<TokenResponse> {
6972
return new Promise((resolve, reject) => {
7073
this.server = http.createServer((req, res) => {
7174
if (!req.url) {
@@ -138,10 +141,19 @@ export default class AuthLogin extends Command {
138141
}
139142

140143
if (code) {
141-
res.writeHead(200, { 'Content-Type': 'text/plain' });
142-
res.end('Login successful. You can close this window.');
143-
this.stopServer();
144-
resolve(code);
144+
this.exchangeCodeForToken(code, codeVerifier)
145+
.then(async (token) => {
146+
res
147+
.writeHead(302, {
148+
Location: `${EOL_LOG_IN_URL}`,
149+
})
150+
.end();
151+
resolve(token);
152+
})
153+
.catch((error) => reject(error))
154+
.finally(() => {
155+
this.stopServer();
156+
});
145157
} else {
146158
res.writeHead(400, { 'Content-Type': 'text/plain' });
147159
res.end('No authorization code returned. Please try again.');

0 commit comments

Comments
 (0)