Skip to content
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: 1 addition & 2 deletions packages/atlas-service/src/atlas-auth-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import type { AtlasUserInfo } from './util';
export type ArgsWithSignal<T = Record<string, unknown>> = T & {
signal?: AbortSignal;
};
export type SignInPrompt = 'none';

type AtlasAuthServiceEvents = {
'signed-in': [];
Expand All @@ -19,7 +18,7 @@ type AtlasAuthEventListener<T extends AtlasAuthEventNames> = (

export abstract class AtlasAuthService extends EventEmitter {
abstract signIn(
opts?: ArgsWithSignal<{ promptType?: SignInPrompt }>
opts?: ArgsWithSignal<{ mainProcessSignIn?: boolean }>
): Promise<AtlasUserInfo>;
abstract signOut(): Promise<void>;
abstract isAuthenticated(opts?: ArgsWithSignal): Promise<boolean>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,10 @@ describe('CompassAtlasAuthService', function () {
const atlasAuthService = getAtlasAuthService({
signIn: signInStub,
});
await atlasAuthService.signIn({ signal: c.signal });
await atlasAuthService.signIn({
mainProcessSignIn: true,
signal: c.signal,
});
expect(signInStub.calledOnce).to.be.true;
expect(signInStub.firstCall.firstArg).to.deep.equal({ signal: c.signal });
});
Expand Down
17 changes: 8 additions & 9 deletions packages/atlas-service/src/compass-atlas-auth-service.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { ipcRenderer } from 'hadron-ipc';
import type { CompassAuthService as AtlasServiceMain } from './main';
import { signInWithoutPrompt } from './store/atlas-signin-reducer';
import { performSignInAttempt } from './store/atlas-signin-reducer';
import { getStore } from './store/atlas-signin-store';
import { AtlasAuthService } from './atlas-auth-service';
import type { ArgsWithSignal, SignInPrompt } from './atlas-auth-service';
import type { ArgsWithSignal } from './atlas-auth-service';

export class CompassAtlasAuthService extends AtlasAuthService {
private _ipc = ipcRenderer?.createInvoke<
Expand Down Expand Up @@ -37,15 +37,14 @@ export class CompassAtlasAuthService extends AtlasAuthService {
return this.ipc.signOut();
}
signIn({
promptType,
mainProcessSignIn,
signal,
}: ArgsWithSignal<{ promptType?: SignInPrompt }> = {}) {
switch (promptType) {
case 'none':
return getStore().dispatch(signInWithoutPrompt({ signal }));
default:
return this.ipc.signIn({ signal });
}: ArgsWithSignal<{ mainProcessSignIn?: boolean }> = {}) {
if (mainProcessSignIn) {
return this.ipc.signIn({ signal });
}

return getStore().dispatch(performSignInAttempt({ signal }));
}
getUserInfo(opts?: ArgsWithSignal) {
return this.ipc.getUserInfo(opts);
Expand Down
14 changes: 7 additions & 7 deletions packages/atlas-service/src/store/atlas-signin-reducer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
cancelSignIn,
attemptId,
AttemptStateMap,
signInWithoutPrompt,
performSignInAttempt,
} from './atlas-signin-reducer';
import { expect } from 'chai';
import { configureStore } from './atlas-signin-store';
Expand Down Expand Up @@ -174,7 +174,7 @@ describe('atlasSignInReducer', function () {
atlasAuthService: mockAtlasService as any,
});

void store.dispatch(signInWithoutPrompt()).catch(() => {});
void store.dispatch(performSignInAttempt()).catch(() => {});

await Promise.all([
store.dispatch(signIn()),
Expand All @@ -184,7 +184,7 @@ describe('atlasSignInReducer', function () {
});
});

describe('signInWithoutPrompt', function () {
describe('performSignInAttempt', function () {
it('should resolve when sign in flow finishes', async function () {
const mockAtlasService = {
isAuthenticated: sandbox.stub().resolves(false),
Expand All @@ -195,7 +195,7 @@ describe('atlasSignInReducer', function () {
const store = configureStore({
atlasAuthService: mockAtlasService as any,
});
await store.dispatch(signInWithoutPrompt());
await store.dispatch(performSignInAttempt());
expect(store.getState()).to.have.property('state', 'success');
});

Expand All @@ -210,8 +210,8 @@ describe('atlasSignInReducer', function () {
atlasAuthService: mockAtlasService as any,
});
try {
await store.dispatch(signInWithoutPrompt());
expect.fail('Expected signInWithoutPrompt action to throw');
await store.dispatch(performSignInAttempt());
expect.fail('Expected performSignInAttempt action to throw');
} catch (err) {
expect(err).to.have.property('message', 'Sign in failed');
}
Expand All @@ -237,7 +237,7 @@ describe('atlasSignInReducer', function () {
});
const c = new AbortController();
const signInPromise = store.dispatch(
signInWithoutPrompt({ signal: c.signal })
performSignInAttempt({ signal: c.signal })
);
c.abort(new Error('Aborted from outside'));
try {
Expand Down
7 changes: 5 additions & 2 deletions packages/atlas-service/src/store/atlas-signin-reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ const startAttempt = (fn: () => void): AtlasSignInThunkAction<AttemptState> => {
};
};

export const signInWithoutPrompt = ({
export const performSignInAttempt = ({
signal,
}: { signal?: AbortSignal } = {}): AtlasSignInThunkAction<
Promise<AtlasUserInfo>
Expand Down Expand Up @@ -348,7 +348,10 @@ export const signIn = (): AtlasSignInThunkAction<Promise<void>> => {
if (await atlasAuthService.isAuthenticated({ signal })) {
userInfo = await atlasAuthService.getUserInfo({ signal });
} else {
userInfo = await atlasAuthService.signIn({ signal });
userInfo = await atlasAuthService.signIn({
mainProcessSignIn: true,
signal,
});
}
openToast('atlas-sign-in-success', {
variant: 'success',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,6 @@ export const signIn = (): GenAIAtlasSignInThunkAction<Promise<void>> => {

await atlasAuthService.signIn({
signal,
promptType: 'none',
});
dispatch(atlasServiceSignedIn());
resolve();
Expand Down
1 change: 0 additions & 1 deletion packages/compass-settings/src/stores/atlas-login.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,6 @@ export const signIn = (): SettingsThunkAction<Promise<void>> => {
});
const userInfo = await atlasAuthService.signIn({
signal,
promptType: 'none',
});
dispatch({ type: AtlasLoginSettingsActionTypes.SignInSuccess, userInfo });
} catch (err) {
Expand Down
Loading