Skip to content

Commit

Permalink
fix(nile-js): set token on Nile create
Browse files Browse the repository at this point in the history
  • Loading branch information
jrea committed Jan 26, 2023
1 parent 7bda8b1 commit f1da2e9
Show file tree
Hide file tree
Showing 9 changed files with 66 additions and 19 deletions.
5 changes: 4 additions & 1 deletion lib/nile/spec/api.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ info:
description: Making SaaS chill.
contact:
email: [email protected]
version: 0.1.0-e78fcc4
version: 0.1.0-6591b7d
servers:
- url: localhost:8080
tags:
Expand Down Expand Up @@ -2043,6 +2043,9 @@ components:
- oidc_token_unidentifiable
- oidc_redirect_urls_undefined
- oidc_verify_attempt_not_found
- oidc_verify_attempt_invalid
- oidc_verify_attempt_repeated
- oidc_verify_attempt_expired
message:
type: string
status_code:
Expand Down
25 changes: 24 additions & 1 deletion lib/nile/src/Nile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
UsersApi,
WorkspacesApi as WorkspaceApi,
MetricsApi,
AuthApi,
} from './client/src';
import {
Configuration,
Expand Down Expand Up @@ -41,6 +42,7 @@ export class NileApi {
events: EventsApi;
access: AccessApi;
metrics: MetricsApi;
auth: AuthApi;
constructor(configuration?: Configuration) {
this.config = configuration;

Expand Down Expand Up @@ -69,6 +71,19 @@ export class NileApi {
this.events = new EventsApi(this.entities);
this.access = new AccessApi(configuration);
this.metrics = new MetricsApi(configuration);
this.auth = new AuthApi(configuration);

// this needs to be last because setToken sets all classes
if (this.config?.tokenStorage === StorageOptions.LocalStorage) {
if (typeof window !== 'undefined') {
if (this.config?.tokenStorage === StorageOptions.LocalStorage) {
const token = localStorage.getItem('nileToken');
if (token) {
this.authToken = token;
}
}
}
}
}

/**
Expand Down Expand Up @@ -98,6 +113,7 @@ export class NileApi {
this.workspaces.workspace = workspace;
this.access.workspace = workspace;
this.metrics.workspace = workspace;
this.auth.workspace = workspace;
}
}

Expand Down Expand Up @@ -126,10 +142,13 @@ export class NileApi {
if (this.metrics.workspace) {
return this.metrics.workspace;
}
if (this.auth.workspace) {
return this.auth.workspace;
}
}

/**
* When a token is set, it is shared across all classes
* When a token is set, it is shared across all classes.
*/
set authToken(token: void | string) {
if (this.config?.tokenStorage === StorageOptions.LocalStorage) {
Expand All @@ -149,6 +168,7 @@ export class NileApi {
this.organizations.authToken = token;
this.access.authToken = token;
this.metrics.authToken = token;
this.auth.authToken = token;
}
}

Expand Down Expand Up @@ -186,6 +206,9 @@ export class NileApi {
if (this.metrics.authToken) {
return this.metrics.authToken;
}
if (this.auth.authToken) {
return this.auth.authToken;
}
}
/**
* Creates a NileApi instance and connects using the provided credentials.
Expand Down
8 changes: 8 additions & 0 deletions lib/nile/src/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ describe('index', () => {
'events',
'access',
'metrics',
'auth',
]);
keys.shift();
keys.forEach((k) => {
Expand Down Expand Up @@ -129,6 +130,13 @@ describe('index', () => {
'produceBatchOfMetrics',
]);
}
if (k === 'auth') {
expect(props).toEqual([
'constructor',
'managedOidcCallback',
'managedOidcCallback1',
]);
}
});
});

Expand Down
7 changes: 6 additions & 1 deletion packages/examples/pages/_app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,15 @@ import React from 'react';
import '../styles/globals.css';
import type { AppProps } from 'next/app';
import { NileProvider } from '@theniledev/react';
import { StorageOptions } from '@theniledev/js';

function MyApp({ Component, pageProps }: AppProps) {
return (
<NileProvider basePath="http://localhost:8080" workspace="1">
<NileProvider
basePath="http://localhost:8080"
workspace="toastr2"
tokenStorage={StorageOptions.LocalStorage}
>
<Component {...pageProps} />
</NileProvider>
);
Expand Down
3 changes: 2 additions & 1 deletion packages/examples/pages/users.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ const useOrgQuery = () => {
const org = orgs?.filter(
(org: Organization) => org.name === currentUserEmail
);
const [{ id }] = org ?? [{}];
const [{ id }] =
orgs && orgs?.length > 0 ? (org as Organization[]) : [{ id: '' }];
return id;
}, [currentUserEmail, orgs]);
return [userLoading || orgLoading, orgId];
Expand Down
9 changes: 5 additions & 4 deletions packages/react/src/lib/SimpleForm/CheckGroup/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,14 @@ type Props = {
attribute: Attribute;
display: DisplayProps;
options: Options;
helperText: string;
};
export default function CheckGroup(props: Props) {
const { options, attribute, display } = props;
const { options, attribute, display, helperText } = props;
const { watch, control } = useFormContext();
const currentVals = watch(attribute.name);
const checkProps: { color?: 'danger'; id?: string } = {};
if (display.helperText) {
if (helperText) {
checkProps.color = 'danger';
}
return (
Expand All @@ -36,7 +37,7 @@ export default function CheckGroup(props: Props) {
sx={{
borderRadius: 'var(--joy-radius-sm)',
p: 0.5,
border: display.helperText
border: helperText
? '1px solid var(--joy-palette-danger-outlinedBorder)'
: 'none',
}}
Expand Down Expand Up @@ -95,7 +96,7 @@ export default function CheckGroup(props: Props) {
sx={{ color: 'var(--joy-palette-danger-500)' }}
level="body2"
>
{display.helperText}
{helperText}
</Typography>
</Stack>
);
Expand Down
16 changes: 11 additions & 5 deletions packages/react/src/lib/SimpleForm/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,10 @@ export default function ConfigForm(props: {
error: Boolean(errors[attr.name]),
};
const options = attr.options ?? [];
let helperText = '';

if (attr.required) {
display.helperText = errors[attr.name] && 'This field is required';
helperText = errors[attr.name] ? 'This field is required' : '';
fieldConfig.required = true;
}

Expand All @@ -98,11 +99,13 @@ export default function ConfigForm(props: {
attribute={attr}
display={display}
options={options}
helperText={helperText}
/>
);
case AttributeType.Select:
return (
<FormControl
key={display.key}
sx={{
'--FormHelperText-color': 'var(--joy-palette-danger-500)',
}}
Expand Down Expand Up @@ -139,7 +142,7 @@ export default function ConfigForm(props: {
})}
</Select>
<FormHelperText id={`${attr.name}-helper-text`}>
{display.helperText}
{helperText}
</FormHelperText>
</Stack>
);
Expand All @@ -150,6 +153,7 @@ export default function ConfigForm(props: {
case AttributeType.Password:
return (
<FormControl
key={display.key}
sx={{
'--FormHelperText-color': 'var(--joy-palette-danger-500)',
}}
Expand All @@ -161,13 +165,14 @@ export default function ConfigForm(props: {
type={AttributeType.Password}
/>
<FormHelperText id={`${attr.name}-helper-text`}>
{display.helperText}
{helperText}
</FormHelperText>
</FormControl>
);
case AttributeType.Number:
return (
<FormControl
key={display.key}
sx={{
'--FormHelperText-color': 'var(--joy-palette-danger-500)',
}}
Expand All @@ -179,7 +184,7 @@ export default function ConfigForm(props: {
type={AttributeType.Number}
/>
<FormHelperText id={`${attr.name}-helper-text`}>
{display.helperText}
{helperText}
</FormHelperText>
</FormControl>
);
Expand All @@ -188,14 +193,15 @@ export default function ConfigForm(props: {
default:
return (
<FormControl
key={display.key}
sx={{
'--FormHelperText-color': 'var(--joy-palette-danger-500)',
}}
>
<FormLabel>{attr.label ?? attr.name}</FormLabel>
<Input {...display} {...register(attr.name, fieldConfig)} />
<FormHelperText id={`${attr.name}-helper-text`}>
{display.helperText}
{helperText}
</FormHelperText>
</FormControl>
);
Expand Down
1 change: 0 additions & 1 deletion packages/react/src/lib/SimpleForm/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ export type DisplayProps = {
key: string;
label: string;
placeholder: string;
helperText?: string;
error?: boolean;
color?: 'danger';
};
11 changes: 6 additions & 5 deletions packages/react/src/lib/hooks/useVerifyToken/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,11 @@ export const useVerifyToken = (): [boolean, null | string] => {
setError(null);
if (token) {
try {
await fetch(
`${nile.config?.basePath}/auth/oidc/verify?state=${token}`
);
setSuccess(true);
const res = await nile.auth.managedOidcCallback1();
if (res) {
nile.authToken = res.token;
setSuccess(true);
}
} catch (e) {
if (e instanceof Error) {
setError(e.message);
Expand All @@ -46,7 +47,7 @@ export const useVerifyToken = (): [boolean, null | string] => {
}
}
doRequest();
}, [nile.config?.basePath, token, url]);
}, [nile, token, url]);

return [success, error];
};

2 comments on commit f1da2e9

@vercel
Copy link

@vercel vercel bot commented on f1da2e9 Jan 26, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

nile-js – ./

nile-js-git-master-theniledev.vercel.app
nile-js.vercel.app
nile-js-theniledev.vercel.app

@vercel
Copy link

@vercel vercel bot commented on f1da2e9 Jan 26, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.