Skip to content

Commit

Permalink
Refactor register
Browse files Browse the repository at this point in the history
  • Loading branch information
joepio committed Dec 27, 2022
1 parent 9955f94 commit 5711e37
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 65 deletions.
23 changes: 18 additions & 5 deletions data-browser/src/components/RegisterSignIn.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,13 @@ import {
import React, { FormEvent, useCallback, useEffect, useState } from 'react';
import { useSettings } from '../helpers/AppSettings';
import { Button } from './Button';
import { Agent, nameRegex, useRegister, useServerURL } from '@tomic/react';
import {
Agent,
nameRegex,
register,
useServerURL,
useStore,
} from '@tomic/react';
import Field from './forms/Field';
import { InputWrapper, InputStyled } from './forms/InputStyles';
import { Row } from './Row';
Expand All @@ -30,7 +36,7 @@ export function RegisterSignIn({
}: React.PropsWithChildren<RegisterSignInProps>): JSX.Element {
const { dialogProps, show } = useDialog();
const { agent } = useSettings();
const [register, setRegister] = useState(true);
const [isRegister, setRegister] = useState(true);

if (agent) {
return <>{children}</>;
Expand All @@ -56,7 +62,9 @@ export function RegisterSignIn({
Sign In
</Button>
</Row>
<Dialog {...dialogProps}>{register ? <Register /> : <SignIn />}</Dialog>
<Dialog {...dialogProps}>
{isRegister ? <Register /> : <SignIn />}
</Dialog>
</>
);
}
Expand All @@ -69,8 +77,9 @@ function Register() {
const [newAgent, setNewAgent] = useState<Agent | undefined>(undefined);
const [serverUrlStr] = useServerURL();
const [nameErr, setErr] = useState<Error | undefined>(undefined);
const register = useRegister();
const doRegister = useCallback(register, []);
const { setAgent } = useSettings();
const store = useStore();

const serverUrl = new URL(serverUrlStr);
serverUrl.host = `${name}.${serverUrl.host}`;
Expand All @@ -95,7 +104,11 @@ function Register() {
}

try {
const { driveURL: newDriveURL, agent } = await register(name, email);
const { driveURL: newDriveURL, agent } = await doRegister(
store,
name,
email,
);
setDriveURL(newDriveURL);
setSecret(agent.buildSecret());
setNewAgent(agent);
Expand Down
48 changes: 48 additions & 0 deletions lib/src/authentication.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import {
Agent,
generateKeyPair,
getTimestampNow,
HeadersObject,
properties,
signToBase64,
Store,
} from './index.js';
Expand Down Expand Up @@ -111,3 +113,49 @@ export const checkAuthenticationCookie = (): boolean => {

return matches.length > 0;
};

export interface RegisterResult {
agent: Agent;
driveURL: string;
}

/** Only lowercase chars, numbers and a hyphen */
export const nameRegex = '^[a-z0-9_-]+';

/** Creates a new Agent + Drive using a shortname and email. Uses the serverURL from the Store. */
export const register = async (
store: Store,
name: string,
email: string,
): Promise<RegisterResult> => {
const keypair = await generateKeyPair();
const agent = new Agent(keypair.privateKey);
const publicKey = await agent.getPublicKey();
const url = new URL('/register', store.getServerUrl());
url.searchParams.set('name', name);
url.searchParams.set('public-key', publicKey);
url.searchParams.set('email', email);
const resource = await store.getResourceAsync(url.toString());
const driveURL = resource.get(properties.redirect.destination) as string;
const agentSubject = resource.get(
properties.redirect.redirectAgent,
) as string;

if (resource.error) {
throw resource.error;
}

if (!driveURL) {
throw new Error('No redirect destination');
}

if (!agentSubject) {
throw new Error('No agent returned');
}

agent.subject = agentSubject;

store.setAgent(agent);

return { driveURL, agent };
};
1 change: 0 additions & 1 deletion react/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,4 @@ export * from './useImporter.js';
export * from './useLocalStorage.js';
export * from './useMarkdown.js';
export * from './useServerSearch.js';
export * from './useRegister.js';
export * from '@tomic/lib';
59 changes: 0 additions & 59 deletions react/src/useRegister.ts

This file was deleted.

0 comments on commit 5711e37

Please sign in to comment.