Skip to content

Commit

Permalink
update get start url
Browse files Browse the repository at this point in the history
  • Loading branch information
musordmt committed Jul 16, 2024
1 parent 6cff8be commit d3ca069
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 8 deletions.
18 changes: 16 additions & 2 deletions pkg/auth/components/login-panel.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
'use client'
import { useState, type PropsWithChildren } from 'react'
import { useState, useEffect, type PropsWithChildren } from 'react'
import { useRouter } from 'next/navigation'
import { observer } from 'mobx-react-lite'
import Link from 'next/link'
Expand Down Expand Up @@ -62,6 +62,7 @@ const LoginPanel: React.FC<PropsWithChildren & {
const router = useRouter()
const auth = useAuth()
const [isLoading, setIsLoading] = useState(false)
const [getStartUrl, setGetStartUrl] = useState('')

const succeed = async (loginMethod: AuthProvider | 'email' | null) => {

Expand Down Expand Up @@ -141,6 +142,19 @@ const LoginPanel: React.FC<PropsWithChildren & {
}
}

const generateUsernameFromEmail = (email: string) => {
const emailPrefix = email.split('@')[0]; // Get the part before the @ symbol
const randomSuffix = Math.random().toString(36).substring(2, 8); // Generate a random string
return `${emailPrefix}_${randomSuffix}`;
}

useEffect(() => {
if (auth.loggedIn) {
const username = auth.user?.email
if (username) setGetStartUrl('https://' + generateUsernameFromEmail(username) + '.' + getStartedUrl)
}
}, [auth])

return (
<ApplyTypography className={cn('w-full flex flex-col text-center !gap-3 LOGIN_OUTER', className)}>
{auth.loggedIn && !redirectUrl ? (
Expand All @@ -149,7 +163,7 @@ const LoginPanel: React.FC<PropsWithChildren & {
{auth.user && (<> {/* this means the hanzo user isn't loaded yet ...*/}
<p>You are signed in as {auth.user?.displayName ?? auth.user?.email}</p>
<div className='flex flex-col md:flex-row gap-3 items-center justify-center'>
{getStartedUrl && <Button variant='primary' onClick={() => router.push(getStartedUrl)} className='w-full'>GET STARTED</Button>}
<Button variant='primary' onClick={() => router.push(getStartUrl)} className='w-full'>GET STARTED</Button>
<Button onClick={logout} variant='outline' disabled={isLoading} className='w-full'>Sign Out</Button>
</div>
</>)}
Expand Down
18 changes: 16 additions & 2 deletions pkg/auth/components/signup-panel.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
'use client'
import { useState, type PropsWithChildren } from 'react'
import { useState, useEffect, type PropsWithChildren } from 'react'
import { useRouter } from 'next/navigation'
import { observer } from 'mobx-react-lite'
import Link from 'next/link'
Expand Down Expand Up @@ -62,6 +62,7 @@ const SignupPanel: React.FC<PropsWithChildren & {
const router = useRouter()
const auth = useAuth()
const [isLoading, setIsLoading] = useState(false)
const [getStartUrl, setGetStartUrl] = useState('')

const succeed = async (loginMethod: AuthProvider | 'email' | null) => {

Expand Down Expand Up @@ -142,6 +143,19 @@ const SignupPanel: React.FC<PropsWithChildren & {
}
}

const generateUsernameFromEmail = (email: string) => {
const emailPrefix = email.split('@')[0]; // Get the part before the @ symbol
const randomSuffix = Math.random().toString(36).substring(2, 8); // Generate a random string
return `${emailPrefix}_${randomSuffix}`;
}

useEffect(() => {
if (auth.loggedIn) {
const username = auth.user?.email
if (username) setGetStartUrl('https://' + generateUsernameFromEmail(username) + '.' + getStartedUrl)
}
}, [auth])

return (
<ApplyTypography className={cn('w-full flex flex-col text-center !gap-3', className)}>
{auth.loggedIn && !redirectUrl ? (
Expand All @@ -150,7 +164,7 @@ const SignupPanel: React.FC<PropsWithChildren & {
{auth.user && (<> {/* this means the hanzo user isn't loaded yet ...*/}
<p>You are signed in as {auth.user?.displayName ?? auth.user?.email}</p>
<div className='flex flex-col md:flex-row gap-3 items-center justify-center'>
{getStartedUrl && <Button variant='primary' onClick={() => {router.push(getStartedUrl)}} className='w-full'>GET STARTED</Button>}
<Button variant='primary' onClick={() => {router.push(getStartUrl)}} className='w-full'>GET STARTED</Button>
<Button onClick={logout} variant='outline' disabled={isLoading} className='w-full'>Sign Out</Button>
</div>
</>)}
Expand Down
2 changes: 1 addition & 1 deletion pkg/auth/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@hanzo/auth",
"version": "2.4.17",
"version": "2.4.18",
"description": "Library with Firebase authentication.",
"publishConfig": {
"registry": "https://registry.npmjs.org/",
Expand Down
4 changes: 2 additions & 2 deletions pkg/auth/server/firebase-support.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ const firebaseApp =
'firebase-admin-app'
)

const USER_INFO_COLLECTION = 'HZ_USER_INFO'
const USER_INFO_COLLECTION = 'USER_INFO'

const auth = getAuth(firebaseApp)
const db = getFirestore(firebaseApp, 'lux-accounts')
const db = getFirestore(firebaseApp, 'accounts')

async function isUserAuthenticated(session: string | undefined = undefined) {
const _session = session ?? (await getSession())
Expand Down
2 changes: 1 addition & 1 deletion pkg/auth/service/impl/firebase-support.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export const firebaseConfig = {
const firebaseApp = getApps().length === 0 ? initializeApp(firebaseConfig) : getApps()[0]
export const auth = getAuth(firebaseApp)
// :aa TODO should be in module conf in host app
export const db = getFirestore(firebaseApp, 'lux-accounts')
export const db = getFirestore(firebaseApp, 'accounts')

export async function loginWithProvider(provider: string): Promise<{ success: boolean, user: User | null }> {
const authProvider = (() => {
Expand Down

0 comments on commit d3ca069

Please sign in to comment.