Skip to content

Commit

Permalink
Edit readme and sample app
Browse files Browse the repository at this point in the history
  • Loading branch information
AnthonyRonning committed Dec 3, 2024
1 parent 2179dad commit 8537291
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 5 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,10 @@ The `useOpenSecret` hook provides access to the OpenSecret API. It returns an ob

#### Authentication Methods
- `signIn(email: string, password: string): Promise<void>`: Signs in a user with the provided email and password.
- `signUp(name: string, email: string, password: string, inviteCode: string): Promise<void>`: Signs up a new user with the provided name, email, password, and invite code.
- `signUp(email: string, password: string, inviteCode: string, name?: string): Promise<void>`: Signs up a new user with the provided email, password, invite code, and optional name.
- `signInGuest(id: string, password: string): Promise<void>`: Signs in a guest user with their ID and password.
- `signUpGuest(password: string, inviteCode: string): Promise<void>`: Creates a new guest account with just a password and invite code.
- `convertGuestToUserAccount(email: string, password: string): Promise<void>`: Converts a guest account to a regular account with email authentication.
- `signOut(): Promise<void>`: Signs out the current user.

#### Key-Value Storage Methods
Expand Down
Binary file modified bun.lockb
Binary file not shown.
107 changes: 107 additions & 0 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,60 @@ function App() {
}
};

const handleGuestSignup = async (e: React.FormEvent<HTMLFormElement>) => {
e.preventDefault();
const formData = new FormData(e.currentTarget);
const password = formData.get("password") as string;
const inviteCode = formData.get("inviteCode") as string;

console.log("Guest Signup request:", { password, inviteCode });

try {
const response = await os.signUpGuest(password, inviteCode);
console.log("Guest Signup response:", response);
alert("Guest Signup successful! Your ID is: " + response.id);
} catch (error) {
console.error("Guest Signup error:", error);
alert("Guest Signup failed: " + (error as Error).message);
}
};

const handleGuestLogin = async (e: React.FormEvent<HTMLFormElement>) => {
e.preventDefault();
const formData = new FormData(e.currentTarget);
const id = formData.get("id") as string;
const password = formData.get("password") as string;

console.log("Guest Login request:", { id, password });

try {
const response = await os.signInGuest(id, password);
console.log("Guest Login response:", response);
alert("Guest Login successful!");
} catch (error) {
console.error("Guest Login error:", error);
alert("Guest Login failed: " + (error as Error).message);
}
};

const handleGuestConversion = async (e: React.FormEvent<HTMLFormElement>) => {
e.preventDefault();
const formData = new FormData(e.currentTarget);
const email = formData.get("email") as string;
const password = formData.get("password") as string;

console.log("Guest Conversion request:", { email, password });

try {
await os.convertGuestToUserAccount(email, password);
console.log("Guest Conversion successful");
alert("Account successfully converted to email login!");
} catch (error) {
console.error("Guest Conversion error:", error);
alert("Guest Conversion failed: " + (error as Error).message);
}
};

const handleLogin = async (e: React.FormEvent<HTMLFormElement>) => {
e.preventDefault();
const formData = new FormData(e.currentTarget);
Expand Down Expand Up @@ -252,6 +306,59 @@ function App() {
</form>
</section>

<section>
<h2>Guest Sign Up</h2>
<p>Create a new guest account with just a password and invite code.</p>
<form onSubmit={handleGuestSignup} className="auth-form">
<input
type="password"
name="password"
placeholder="Password"
autoComplete="new-password"
minLength={8}
required
/>
<input type="text" name="inviteCode" placeholder="Invite Code" required />
<button type="submit">Sign Up as Guest</button>
</form>
</section>

<section>
<h2>Guest Login</h2>
<p>Sign in to your guest account with ID and password.</p>
<form onSubmit={handleGuestLogin} className="auth-form">
<input type="text" name="id" placeholder="Guest ID" required />
<input
type="password"
name="password"
placeholder="Password"
autoComplete="current-password"
minLength={8}
required
/>
<button type="submit">Login as Guest</button>
</form>
</section>

{(os.auth.user?.user.email === undefined || os.auth.user?.user.email === null) && (
<section>
<h2>Convert Guest Account</h2>
<p>Convert your guest account to a regular account with email.</p>
<form onSubmit={handleGuestConversion} className="auth-form">
<input type="email" name="email" placeholder="Email" autoComplete="email" required />
<input
type="password"
name="password"
placeholder="New Password"
autoComplete="new-password"
minLength={8}
required
/>
<button type="submit">Convert to Email Account</button>
</form>
</section>
)}

<section>
<h2>Get Data</h2>
<p>Retrieve string data by key.</p>
Expand Down
15 changes: 11 additions & 4 deletions src/lib/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { getAttestation } from "./getAttestation";
import { authenticate } from "./attestation";
import { parseAttestationForView, AWS_ROOT_CERT_DER, EXPECTED_ROOT_CERT_HASH, ParsedAttestationView } from "./attestationForView";
import type { AttestationDocument } from "./attestation";
import type { LoginResponse } from "./api";
import { PcrConfig } from "./pcr";

export type OpenSecretAuthState = {
Expand Down Expand Up @@ -66,7 +67,7 @@ export type OpenSecretContextType = {
* Creates a new guest account, which can be upgraded to a normal account later with email.
* @param password - User's chosen password, cannot be changed or recovered without adding email address.
* @param inviteCode - Invitation code for registration
* @returns A promise that resolves when account creation is complete
* @returns A promise that resolves to the login response containing the guest ID
* @throws {Error} If signup fails
*
* @description
Expand All @@ -75,7 +76,7 @@ export type OpenSecretContextType = {
* - Updates the auth state with new user information
* - Throws an error if account creation fails
*/
signUpGuest: (password: string, inviteCode: string) => Promise<void>;
signUpGuest: (password: string, inviteCode: string) => Promise<LoginResponse>;

/**
* Upgrades a guest account to a user account with email and password authentication.
Expand Down Expand Up @@ -281,7 +282,12 @@ export const OpenSecretContext = createContext<OpenSecretContextType>({
signIn: async () => { },
signUp: async () => { },
signInGuest: async () => { },
signUpGuest: async () => { },
signUpGuest: async (): Promise<LoginResponse> => ({
id: "",
email: undefined,
access_token: "",
refresh_token: ""
}),
convertGuestToUserAccount: async () => { },
signOut: async () => { },
get: api.fetchGet,
Expand Down Expand Up @@ -442,13 +448,14 @@ export function OpenSecretProvider({

async function signUpGuest(password: string, inviteCode: string) {
try {
const { access_token, refresh_token } = await api.fetchGuestSignUp(
const { access_token, refresh_token, id } = await api.fetchGuestSignUp(
password,
inviteCode,
);
window.localStorage.setItem("access_token", access_token);
window.localStorage.setItem("refresh_token", refresh_token);
await fetchUser();
return { access_token, refresh_token, id };
} catch (error) {
console.error(error);
throw error;
Expand Down

0 comments on commit 8537291

Please sign in to comment.