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: 2 additions & 1 deletion config/tslint.base.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@
"array-type": [true, "array-simple"],
"arrow-return-shorthand": [true, "multiline"],
"callable-types": [true],
"comment-format": [true, "check-uppercase", {"ignore-words": ["TODO"]}],
"comment-format": [true, "check-space", {"ignore-words": ["TODO"]}],
"interface-name": [false],
"interface-over-type-literal": true,
"member-access": [true],
"max-classes-per-file": [false],
"prefer-object-spread": [true],
"no-angle-bracket-type-assertion": true,
"no-boolean-literal-compare": true,
"no-empty": [false],
"no-implicit-dependencies": [true, "dev", "optional"],
"no-inferrable-types": [true],
"no-internal-module": [true],
Expand Down
12 changes: 10 additions & 2 deletions packages/browser/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,14 @@
{
"name": "Jason Flax",
"email": "jason.flax@mongodb.com"
},
{
"name": "Doug Kaminsky",
"email": "douglas.kaminsky@mongodb.com"
},
{
"name": "Tyler Kaye",
"email": "tyler.kaye@mongodb.com"
}
],
"license": "Apache-2.0",
Expand All @@ -58,8 +66,8 @@
"build-browser": "rollup -c && npm run add-version-browser",
"prepare": "npm run build",
"watch": "tsc -w -p tsconfig.esm.json",
"lint": "tslint src/**/*.ts __tests__/**/*.ts",
"lint-fix": "tslint --fix src/**/*.ts __tests__/**/*.ts",
"lint": "tslint 'src/**/*.ts' '__tests__/**/*.ts'",
"lint-fix": "tslint --fix 'src/**/*.ts' '__tests__/**/*.ts'",
"lint-check": "tslint-config-prettier-check ./tslint.json",
"prettier": "prettier --write \"{,!(node_modules),!(dist)/**/}*.ts\"",
"add-version-esm": "sed -i -e \"s/@VERSION@/$npm_package_version/g\" dist/esm/core/internal/common/Version.js",
Expand Down
8 changes: 5 additions & 3 deletions packages/browser/core/src/core/auth/StitchAuth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ export default interface StitchAuth {
* - [[removeUser]]
* - [[removeUserWithId]]
*/
listUsers(): Array<StitchUser>;
listUsers(): StitchUser[];

/**
* Switches the active user to the user with the specified id. The user must
Expand All @@ -89,7 +89,8 @@ export default interface StitchAuth {
*/
switchToUserWithId(userId: string): StitchUser

/** @hidden
/**
* @hidden
* Retrieves the authentication provider client for the authentication
* provider associated with the specified factory.
*
Expand All @@ -99,7 +100,8 @@ export default interface StitchAuth {
factory: AuthProviderClientFactory<ClientT>
): ClientT;

/** @hidden
/**
* @hidden
* Retrieves the authentication provider client for the authentication
* provider associated with the specified factory and auth provider name.
*
Expand Down
4 changes: 2 additions & 2 deletions packages/browser/core/src/core/auth/StitchUser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
import {
CoreStitchUser,
StitchCredential,
StitchUserProfile,
StitchUserIdentity } from "mongodb-stitch-core-sdk";
StitchUserIdentity,
StitchUserProfile } from "mongodb-stitch-core-sdk";

import StitchRedirectCredential from "./providers/StitchRedirectCredential";

Expand Down
76 changes: 40 additions & 36 deletions packages/browser/core/src/core/auth/internal/StitchAuthImpl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
import { detect } from "detect-browser";

import {
AuthInfo,
AuthEvent,
AuthEventKind,
AuthInfo,
CoreStitchAuth,
CoreStitchUser,
DeviceFields,
Expand Down Expand Up @@ -82,9 +82,9 @@ interface PartialWindow {
/** @hidden */
export default class StitchAuthImpl extends CoreStitchAuth<StitchUser>
implements StitchAuth {
public static injectedFetch?: any;
private readonly listeners: Set<StitchAuthListener> = new Set();
private readonly synchronousListeners: Set<StitchAuthListener> = new Set();
public static injectedFetch?: any;

/**
* Construct a new StitchAuth implementation
Expand Down Expand Up @@ -211,9 +211,7 @@ export default class StitchAuthImpl extends CoreStitchAuth<StitchUser>
providerName,
redirectFragment.asLink
)
).then(user => {
return user;
});
).then(user => user);
} catch (err) {
return Promise.reject(err);
}
Expand Down Expand Up @@ -273,12 +271,12 @@ export default class StitchAuthImpl extends CoreStitchAuth<StitchUser>
this.listeners.add(listener);

// Trigger the ListenerRegistered event in case some event happens and
// this caller would miss out on this event other wise.
// This caller would miss out on this event other wise.

// dispatch a legacy deprecated auth event
// Dispatch a legacy deprecated auth event
this.onAuthEvent(listener);

// dispatch a new style auth event
// Dispatch a new style auth event
this.dispatchAuthEvent({
kind: AuthEventKind.ListenerRegistered,
});
Expand All @@ -288,12 +286,12 @@ export default class StitchAuthImpl extends CoreStitchAuth<StitchUser>
this.listeners.add(listener);

// Trigger the ListenerRegistered event in case some event happens and
// this caller would miss out on this event other wise.
// This caller would miss out on this event other wise.

// dispatch a legacy deprecated auth event
// Dispatch a legacy deprecated auth event
this.onAuthEvent(listener);

// dispatch a new style auth event
// Dispatch a new style auth event
this.dispatchAuthEvent({
kind: AuthEventKind.ListenerRegistered,
});
Expand All @@ -308,10 +306,9 @@ export default class StitchAuthImpl extends CoreStitchAuth<StitchUser>
*/
public onAuthEvent(listener?: StitchAuthListener) {
if (listener) {
const auth = this;
const _ = new Promise(resolve => {
if (listener.onAuthEvent) {
listener.onAuthEvent(auth);
listener.onAuthEvent(this);
}
resolve(undefined);
});
Expand All @@ -322,15 +319,6 @@ export default class StitchAuthImpl extends CoreStitchAuth<StitchUser>
}
}

/**
* Utility function used to force the compiler to enforce an exhaustive
* switch statment in dispatchAuthEvent at compile-time.
* @see https://www.typescriptlang.org/docs/handbook/advanced-types.html
*/
private assertNever(x: never): never {
throw new Error("unexpected object: " + x);
}

/**
* Dispatch method for the new auth listener methods.
* @param event the discriminated union representing the auth event
Expand Down Expand Up @@ -397,13 +385,23 @@ export default class StitchAuthImpl extends CoreStitchAuth<StitchUser>
});
break;
default:
// compiler trick to force this switch to be exhaustive. if the above
// switch statement doesn't check all AuthEventKinds, event will not
// be of type never
/* Compiler trick to force this switch to be exhaustive. if the above
* switch statement doesn't check all AuthEventKinds, event will not
* be of type never
*/
return this.assertNever(event);
}
}

/**
* Utility function used to force the compiler to enforce an exhaustive
* switch statment in dispatchAuthEvent at compile-time.
* @see https://www.typescriptlang.org/docs/handbook/advanced-types.html
*/
private assertNever(x: never): never {
throw new Error("unexpected object: " + x);
}

/**
* Dispatches the provided block to all auth listeners, including the
* synchronous and asynchronous ones.
Expand All @@ -423,7 +421,12 @@ export default class StitchAuthImpl extends CoreStitchAuth<StitchUser>
}

private cleanupRedirect() {
// This should probably be undefined, but null works just fine and we dont want to test
// all browsers which may behave differently. Furthermore, the documentation on replaceState()
// uses null.
/* tslint:disable:no-null-keyword */
this.jsdomWindow.history.replaceState(null, "", this.pageRootUrl());
/* tslint:enable:no-null-keyword */

this.authStorage.remove(RedirectKeys.State);
this.authStorage.remove(RedirectKeys.ProviderName);
Expand All @@ -433,7 +436,7 @@ export default class StitchAuthImpl extends CoreStitchAuth<StitchUser>
private parseRedirect(): ParsedRedirectFragment | never {
if (typeof this.jsdomWindow === "undefined") {
// This means we're running in some environment other
// than a browser - so handling a redirect makes no sense here.
// Than a browser - so handling a redirect makes no sense here.
throw new StitchRedirectError("running in a non-browser environment");
}

Expand All @@ -458,7 +461,7 @@ export default class StitchAuthImpl extends CoreStitchAuth<StitchUser>
}

if (redirectFragment.lastError) {
// remove the fragment from the window history and reject
// Remove the fragment from the window history and reject
throw new StitchRedirectError(
`error handling redirect: ${redirectFragment.lastError}`
);
Expand Down Expand Up @@ -530,11 +533,11 @@ function unmarshallUserAuth(data): AuthInfo {
}

class ParsedRedirectFragment {
public stateValid: boolean = false;
public stateValid = false;
public authInfo?: AuthInfo;
public lastError?: string;
public clientAppIdValid: boolean = false;
public asLink: boolean = false;
public clientAppIdValid = false;
public asLink = false;

get isValid(): boolean {
return this.stateValid && this.clientAppIdValid;
Expand All @@ -546,14 +549,15 @@ function parseRedirectFragment(
ourState,
ourClientAppId
): ParsedRedirectFragment {
// After being redirected from oauth, the URL will look like:
// https://todo.examples.stitch.mongodb.com/#_stitch_state=...&_stitch_ua=...
// This function parses out stitch-specific tokens from the fragment and
// builds an object describing the result.
/*
* After being redirected from oauth, the URL will look like:
* https://todo.examples.stitch.mongodb.com/#_stitch_state=...&_stitch_ua=...
* This function parses out stitch-specific tokens from the fragment and
* builds an object describing the result.
*/
const vars = fragment.split("&");
const result: ParsedRedirectFragment = new ParsedRedirectFragment();
vars.forEach(kvp => {
// for (let i = 0; i < vars.length; ++i) {
const pairParts = kvp.split("=");
const pairKey = decodeURIComponent(pairParts[0]);

Expand All @@ -571,7 +575,7 @@ function parseRedirectFragment(
}
break;
case RedirectFragmentFields.StitchLink:
if (pairParts[1] == "ok") {
if (pairParts[1] === "ok") {
result.asLink = true;
}
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ export namespace UserApiKeyAuthProviderClient {
implements AuthProviderClientFactory<UserApiKeyAuthProviderClient> {
public getClient(
authRequestClient: StitchAuthRequestClient,
requestClient: StitchRequestClient, // this arg is ignored
requestClient: StitchRequestClient, // This arg is ignored
routes: StitchAuthRoutes
): UserApiKeyAuthProviderClient {
return new UserApiKeyAuthProviderClientImpl(authRequestClient, routes);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ export namespace UserPasswordAuthProviderClient {
> = new class
implements AuthProviderClientFactory<UserPasswordAuthProviderClient> {
public getClient(
authRequestClient: StitchAuthRequestClient, // this arg is ignored
authRequestClient: StitchAuthRequestClient, // This arg is ignored
requestClient: StitchRequestClient,
routes: StitchAuthRoutes
): UserPasswordAuthProviderClient {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,15 @@ import {
StitchAppClientInfo,
StitchAppRequestClient
} from "mongodb-stitch-core-sdk";

import NamedServiceClientFactory from "../../services/internal/NamedServiceClientFactory";
import ServiceClientFactory from "../../services/internal/ServiceClientFactory";
import StitchServiceClientImpl from "../../services/internal/StitchServiceClientImpl";
import StitchServiceClient from "../../services/StitchServiceClient";
import StitchAuthImpl from "../auth/internal/StitchAuthImpl";
import StitchBrowserAppRoutes from "../auth/internal/StitchBrowserAppRoutes";

import StitchAppClient from "../StitchAppClient";
import StitchServiceClient from "../../services/StitchServiceClient";
import StitchServiceClientImpl from "../../services/internal/StitchServiceClientImpl";


/** @hidden */
export default class StitchAppClientImpl implements StitchAppClient {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,6 @@ const stitchPrefixKey = "__stitch.client";
export default class LocalStorage implements Storage {
constructor(private readonly suiteName: string) {}

private getKey(forKey: string): string {
return `${stitchPrefixKey}.${this.suiteName}.${forKey}`;
}

public get(key: string): any {
return localStorage.getItem(this.getKey(key));
}
Expand All @@ -37,4 +33,8 @@ export default class LocalStorage implements Storage {
public remove(key: string) {
localStorage.removeItem(this.getKey(key));
}

private getKey(forKey: string): string {
return `${stitchPrefixKey}.${this.suiteName}.${forKey}`;
}
}
Loading