From c4e62bd099450a9a6fc323a994d959471629dcbe Mon Sep 17 00:00:00 2001 From: Henry Dollman Date: Sun, 28 Jul 2024 11:09:48 -0400 Subject: [PATCH] only show GitHub button / dialog during onboarding --- hub/site/src/components/login/auth-form.tsx | 121 +++++++++++--------- 1 file changed, 64 insertions(+), 57 deletions(-) diff --git a/hub/site/src/components/login/auth-form.tsx b/hub/site/src/components/login/auth-form.tsx index 0935136..ade4bdb 100644 --- a/hub/site/src/components/login/auth-form.tsx +++ b/hub/site/src/components/login/auth-form.tsx @@ -13,7 +13,7 @@ import { DialogHeader, DialogTitle, } from '@/components/ui/dialog' -import { useState } from 'react' +import { useCallback, useState } from 'react' import { AuthMethodsList, OAuth2AuthConfig } from 'pocketbase' import { Link } from '../router' @@ -67,57 +67,60 @@ export function UserAuthForm({ const [isOauthLoading, setIsOauthLoading] = useState(false) const [errors, setErrors] = useState>({}) - async function handleSubmit(e: React.FormEvent) { - e.preventDefault() - setIsLoading(true) - try { - const formData = new FormData(e.target as HTMLFormElement) - const data = Object.fromEntries(formData) as Record - const Schema = isFirstRun ? RegisterSchema : LoginSchema - const result = v.safeParse(Schema, data) - if (!result.success) { - console.log(result) - let errors = {} - for (const issue of result.issues) { - // @ts-ignore - errors[issue.path[0].key] = issue.message - } - setErrors(errors) - return - } - const { email, password, passwordConfirm, username } = result.output - if (isFirstRun) { - // check that passwords match - if (password !== passwordConfirm) { - let msg = 'Passwords do not match' - setErrors({ passwordConfirm: msg }) + const handleSubmit = useCallback( + async (e: React.FormEvent) => { + e.preventDefault() + setIsLoading(true) + try { + const formData = new FormData(e.target as HTMLFormElement) + const data = Object.fromEntries(formData) as Record + const Schema = isFirstRun ? RegisterSchema : LoginSchema + const result = v.safeParse(Schema, data) + if (!result.success) { + console.log(result) + let errors = {} + for (const issue of result.issues) { + // @ts-ignore + errors[issue.path[0].key] = issue.message + } + setErrors(errors) return } - await pb.admins.create({ - email, - password, - passwordConfirm: password, - }) - await pb.admins.authWithPassword(email, password) - await pb.collection('users').create({ - username, - email, - password, - passwordConfirm: password, - role: 'admin', - verified: true, - }) - await pb.collection('users').authWithPassword(email, password) - } else { - await pb.collection('users').authWithPassword(email, password) + const { email, password, passwordConfirm, username } = result.output + if (isFirstRun) { + // check that passwords match + if (password !== passwordConfirm) { + let msg = 'Passwords do not match' + setErrors({ passwordConfirm: msg }) + return + } + await pb.admins.create({ + email, + password, + passwordConfirm: password, + }) + await pb.admins.authWithPassword(email, password) + await pb.collection('users').create({ + username, + email, + password, + passwordConfirm: password, + role: 'admin', + verified: true, + }) + await pb.collection('users').authWithPassword(email, password) + } else { + await pb.collection('users').authWithPassword(email, password) + } + $authenticated.set(true) + } catch (e) { + showLoginFaliedToast() + } finally { + setIsLoading(false) } - $authenticated.set(true) - } catch (e) { - showLoginFaliedToast() - } finally { - setIsLoading(false) - } - } + }, + [isFirstRun] + ) if (!authMethods) { return null @@ -225,14 +228,17 @@ export function UserAuthForm({ -
-
- + {(isFirstRun || authMethods.authProviders.length > 0) && ( + // only show 'continue with' during onboarding or if we have auth providers +
+
+ +
+
+ Or continue with +
-
- Or continue with -
-
+ )} )} @@ -297,7 +303,8 @@ export function UserAuthForm({
)} - {!authMethods.authProviders.length && ( + {!authMethods.authProviders.length && isFirstRun && ( + // only show GitHub button / dialog during onboarding