Skip to content

Commit

Permalink
Make use of logging lib, move errors to utils/errors, remove yalc fro…
Browse files Browse the repository at this point in the history
…m packages
  • Loading branch information
incorbador committed Nov 28, 2023
1 parent 149716e commit 3732265
Show file tree
Hide file tree
Showing 18 changed files with 67 additions and 190 deletions.
26 changes: 24 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions packages/react-sdk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@
"react": ">=16.8"
},
"dependencies": {
"@corbado/web-core": "file:.yalc/@corbado/web-core",
"ts-results": "^3.3.0"
"@corbado/web-core": "^0.1.6"
},
"devDependencies": {
"@types/react": "^18.2.0"
Expand Down
9 changes: 0 additions & 9 deletions packages/react-sdk/yalc.lock

This file was deleted.

4 changes: 2 additions & 2 deletions packages/react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@
"url": "https://github.com/corbado/javascript/issues"
},
"dependencies": {
"@corbado/react-sdk": "file:.yalc/@corbado/react-sdk",
"@corbado/web-core": "file:.yalc/@corbado/web-core",
"@corbado/react-sdk": "^0.1.6",
"@corbado/web-core": "^0.1.6",
"i18next": "23.5.1",
"i18next-browser-languagedetector": "7.1.0",
"react-i18next": "13.2.2"
Expand Down
13 changes: 0 additions & 13 deletions packages/react/yalc.lock

This file was deleted.

5 changes: 4 additions & 1 deletion packages/web-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@
"dependencies": {
"@github/webauthn-json": "^2.1.1",
"axios": "^1.6.0",
"rxjs": "^7.8.1",
"loglevel": "^1.8.1",
"rxjs": "^7.8.1"
},
"devDependencies": {
"ts-results": "^3.3.0"
}
}
1 change: 1 addition & 0 deletions packages/web-core/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export * from './utils/constants';
export * from './utils/helpers/webAuthUtils';
export * from './utils/errors';
export * from './types';
export * from './services';
4 changes: 2 additions & 2 deletions packages/web-core/src/services/ApiService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ import type {
InitSignUpWithEmailOTPError,
LoginWithPasskeyError,
SignUpWithPasskeyError,
} from '../types';
import { CorbadoError, NonRecoverableError } from '../types';
} from '../utils';
import { CorbadoError, NonRecoverableError } from '../utils';

// TODO: does this work also without npm start? (e.g. vite js)
const packageVersion = '0';
Expand Down
23 changes: 14 additions & 9 deletions packages/web-core/src/services/AuthService.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,24 @@
import log from 'loglevel';
import { Subject } from 'rxjs';
import type { Result } from 'ts-results';
import { Ok } from 'ts-results';

import type { AuthenticationResponse } from '../internaltypes/auth';
import type { IUser, ShortSession, UserAuthMethodsInterface } from '../types';
import { AuthState, LoginHandler } from '../types';
import type {
AppendPasskeyError,
CompleteLoginWithEmailOTPError,
CompleteSignupWithEmailOTPError,
InitAutocompletedLoginWithPasskeyError,
InitLoginWithEmailOTPError,
InitSignUpWithEmailOTPError,
IUser,
LoginWithPasskeyError,
ShortSession,
SignUpWithPasskeyError,
UserAuthMethodsInterface,
} from '../types';
import { AuthState, LoginHandler } from '../types';
} from '../utils';
import type { ApiService } from './ApiService';
import type { AuthenticatorService } from './AuthenticatorService';
import type { SessionService } from './SessionService';
import type { WebAuthnService } from './WebAuthnService';

/**
* AuthService is a class that handles authentication related operations.
Expand All @@ -29,7 +28,7 @@ import type { SessionService } from './SessionService';
*/
export class AuthService {
#apiService: ApiService;
#authenticatorService: AuthenticatorService;
#authenticatorService: WebAuthnService;

// sessionService is used to store and manage (e.g. refresh) the user's session
#sessionService: SessionService;
Expand All @@ -45,13 +44,19 @@ export class AuthService {
/**
* The constructor initializes the AuthService with an instance of ApiService.
*/
constructor(apiService: ApiService, sessionService: SessionService, authenticatorService: AuthenticatorService) {
constructor(apiService: ApiService, sessionService: SessionService, authenticatorService: WebAuthnService) {
this.#apiService = apiService;
this.#authenticatorService = authenticatorService;
this.#sessionService = sessionService;
}

init() {
init(isDebug = false) {
if (isDebug) {
log.setLevel('debug');
} else {
log.setLevel('error');
}

this.#sessionService.init((shortSession: ShortSession | undefined) => {
const user = this.#sessionService.getUser();

Expand Down
12 changes: 6 additions & 6 deletions packages/web-core/src/services/SessionService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type { AxiosRequestConfig } from 'axios';
import type { IFullUser, IUser } from '../types';
import { ShortSession } from '../types';
import type { ApiService } from './ApiService';
import log from "loglevel";

const shortSessionKey = 'cbo_short_session';
const longSessionKey = 'cbo_long_session';
Expand Down Expand Up @@ -68,7 +69,6 @@ export class SessionService {
* @returns The username or null if it's not set.
*/
public getUser(): IUser | undefined {
console.log(this.#shortSession);
if (!this.#shortSession) {
return;
}
Expand Down Expand Up @@ -127,7 +127,7 @@ export class SessionService {

logout() {
// TODO: should we call backend to destroy the session here?
console.log('logging out user');
log.debug('logging out user');
this.clear();

if (this.#onShortSessionChange) {
Expand Down Expand Up @@ -204,7 +204,7 @@ export class SessionService {
}

// nothing to do for now
console.log('no refresh, token still valid');
log.debug('no refresh, token still valid');
return;
}

Expand All @@ -217,12 +217,12 @@ export class SessionService {
};
const response = await this.#apiService.sessionsApi.sessionRefresh({}, options);
if (response.status !== 200) {
console.error(`refresh error, status code: ${response.status}`);
log.warn(`refresh error, status code: ${response.status}`);
return;
}

if (!response.data.shortSession?.value) {
console.error('refresh error, missing short session');
log.warn('refresh error, missing short session');
return;
}

Expand All @@ -231,7 +231,7 @@ export class SessionService {
} catch (e) {
// if it's a network error, we should do a retry
// for all other errors, we should log out the user
console.log(e);
log.warn(e);
this.logout();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ import { create, get } from '@github/webauthn-json';
import type { Subject } from 'rxjs';
import type { Result } from 'ts-results';
import { Err, Ok } from 'ts-results';
import {CorbadoError, NonRecoverableError} from "../utils";

import { CorbadoError, NonRecoverableError } from '../types';

/**
* AuthenticatorService handles all interactions with webAuthn platform authenticators.
* Currently, this includes the creation of passkeys and the login with existing passkeys.
*/
export class AuthenticatorService {
export class WebAuthnService {
#globalErrors: Subject<NonRecoverableError | undefined>;

constructor(globalErrors: Subject<NonRecoverableError | undefined>) {
Expand Down
8 changes: 3 additions & 5 deletions packages/web-core/src/services/index.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import { Subject } from 'rxjs';

import type { IFlowHandlerConfig } from '../types';
import { NonRecoverableError } from '../types';
import { defaultTimeout } from '../utils';
import {defaultTimeout, NonRecoverableError} from '../utils';
import { ApiService } from './ApiService';
import { AuthenticatorService } from './AuthenticatorService';
import { AuthService } from './AuthService';
import type { FlowHandlerService } from './FlowHandlerService';
import { ProjectService } from './ProjectService';
import { SessionService } from './SessionService';
import { WebAuthnService } from './WebAuthnService';

export type { ProjectService } from './ProjectService';
export type { AuthService } from './AuthService';
Expand Down Expand Up @@ -41,7 +40,7 @@ export class CorbadoApp {
this.#projectId = projectId;
this.#apiService = new ApiService(this.#projectId, apiTimeout);
const sessionService = new SessionService(this.#apiService);
const authenticatorService = new AuthenticatorService(this.#globalErrors);
const authenticatorService = new WebAuthnService(this.#globalErrors);
this.#authService = new AuthService(this.#apiService, sessionService, authenticatorService);
this.#projectService = new ProjectService(this.#apiService);
}
Expand Down Expand Up @@ -72,7 +71,6 @@ export class CorbadoApp {
*/
public init() {
if (!this.#validateProjectId(this.#projectId)) {
console.log('invalid project id');
this.#globalErrors.next(NonRecoverableError.invalidConfig('Invalid project ID'));
}

Expand Down
1 change: 0 additions & 1 deletion packages/web-core/src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,3 @@ export * from './auth';
export * from './flowHandler';
export * from './session';
export * from './loginHandler';
export * from './errors';
2 changes: 1 addition & 1 deletion packages/web-core/src/types/loginHandler.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { Result } from 'ts-results';

import type { CompleteAutocompletedLoginWithPasskeyError } from './errors';
import type { CompleteAutocompletedLoginWithPasskeyError } from '../utils/errors/errors';

export class LoginHandler {
readonly #completionCallback: () => Promise<Result<void, CompleteAutocompletedLoginWithPasskeyError>>;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import type { ErrorRspAllOfError } from '../api';
import log from "loglevel";

import type { ErrorRspAllOfError } from '../../api';

export type SignUpWithPasskeyError =
| UserAlreadyExistsError
Expand Down Expand Up @@ -28,7 +30,7 @@ export class CorbadoError extends Error {
}

static fromApiResponse(errorResp: ErrorRspAllOfError): CorbadoError {
console.log('errorResp', errorResp);
log.debug('errorResp', errorResp);
switch (errorResp.type) {
case 'validation_error': {
if (!errorResp.validation?.length) {
Expand Down Expand Up @@ -77,7 +79,7 @@ export class CorbadoError extends Error {
}

static fromUnknownException(e: unknown): CorbadoError {
console.log('unknown exception', e);
log.debug('unknown exception', e);
return CorbadoError.unknown();
}

Expand Down
1 change: 1 addition & 0 deletions packages/web-core/src/utils/errors/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './errors';
1 change: 1 addition & 0 deletions packages/web-core/src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './helpers';
export * from './constants';
export * from './flows';
export * from './errors';
Loading

0 comments on commit 3732265

Please sign in to comment.