@libreapps/auth 3.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.turbo/turbo-build.log +4 -0
- package/CHANGELOG.md +20 -0
- package/LICENSE.md +21 -0
- package/README.md +61 -0
- package/components/auth-widget.tsx +110 -0
- package/components/email-password-form.tsx +96 -0
- package/components/index.ts +4 -0
- package/components/login-panel.tsx +207 -0
- package/components/signup-panel.tsx +213 -0
- package/dist/components/auth-widget.d.ts +13 -0
- package/dist/components/auth-widget.js +33 -0
- package/dist/components/auth-widget.js.map +1 -0
- package/dist/components/email-password-form.d.ts +8 -0
- package/dist/components/email-password-form.js +25 -0
- package/dist/components/email-password-form.js.map +1 -0
- package/dist/components/index.d.ts +4 -0
- package/dist/components/index.js +5 -0
- package/dist/components/index.js.map +1 -0
- package/dist/components/login-panel.d.ts +13 -0
- package/dist/components/login-panel.js +104 -0
- package/dist/components/login-panel.js.map +1 -0
- package/dist/components/signup-panel.d.ts +13 -0
- package/dist/components/signup-panel.js +104 -0
- package/dist/components/signup-panel.js.map +1 -0
- package/dist/icons/ethereum.d.ts +4 -0
- package/dist/icons/ethereum.js +6 -0
- package/dist/icons/ethereum.js.map +1 -0
- package/dist/icons/facebook.d.ts +4 -0
- package/dist/icons/facebook.js +6 -0
- package/dist/icons/facebook.js.map +1 -0
- package/dist/icons/github.d.ts +4 -0
- package/dist/icons/github.js +6 -0
- package/dist/icons/github.js.map +1 -0
- package/dist/icons/google.d.ts +4 -0
- package/dist/icons/google.js +6 -0
- package/dist/icons/google.js.map +1 -0
- package/dist/icons/index.d.ts +6 -0
- package/dist/icons/index.js +7 -0
- package/dist/icons/index.js.map +1 -0
- package/dist/index.d.ts +24 -0
- package/dist/index.js +24 -0
- package/dist/index.js.map +1 -0
- package/dist/server/firebase-support.d.ts +8 -0
- package/dist/server/firebase-support.js +151 -0
- package/dist/server/firebase-support.js.map +1 -0
- package/dist/server/index.d.ts +4 -0
- package/dist/server/index.js +7 -0
- package/dist/server/index.js.map +1 -0
- package/dist/server/rest-api-handlers.d.ts +4 -0
- package/dist/server/rest-api-handlers.js +23 -0
- package/dist/server/rest-api-handlers.js.map +1 -0
- package/dist/server/stub-server.d.ts +14 -0
- package/dist/server/stub-server.js +23 -0
- package/dist/server/stub-server.js.map +1 -0
- package/dist/service/auth-service.d.ts +29 -0
- package/dist/service/auth-service.js +2 -0
- package/dist/service/auth-service.js.map +1 -0
- package/dist/service/context.d.ts +9 -0
- package/dist/service/context.js +16 -0
- package/dist/service/context.js.map +1 -0
- package/dist/service/get-singleton.d.ts +4 -0
- package/dist/service/get-singleton.js +24 -0
- package/dist/service/get-singleton.js.map +1 -0
- package/dist/service/impl/firebase-support.d.ts +34 -0
- package/dist/service/impl/firebase-support.js +190 -0
- package/dist/service/impl/firebase-support.js.map +1 -0
- package/dist/service/impl/index.d.ts +32 -0
- package/dist/service/impl/index.js +179 -0
- package/dist/service/impl/index.js.map +1 -0
- package/dist/service/impl/stub-auth-service.d.ts +42 -0
- package/dist/service/impl/stub-auth-service.js +97 -0
- package/dist/service/impl/stub-auth-service.js.map +1 -0
- package/dist/service/impl/wallet-support.d.ts +8 -0
- package/dist/service/impl/wallet-support.js +82 -0
- package/dist/service/impl/wallet-support.js.map +1 -0
- package/dist/service/index.d.ts +4 -0
- package/dist/service/index.js +4 -0
- package/dist/service/index.js.map +1 -0
- package/dist/service/provider-registry.d.ts +37 -0
- package/dist/service/provider-registry.js +58 -0
- package/dist/service/provider-registry.js.map +1 -0
- package/dist/types/api-response.d.ts +8 -0
- package/dist/types/api-response.js +2 -0
- package/dist/types/api-response.js.map +1 -0
- package/dist/types/auth-service-conf.d.ts +5 -0
- package/dist/types/auth-service-conf.js +2 -0
- package/dist/types/auth-service-conf.js.map +1 -0
- package/dist/types/index.d.ts +4 -0
- package/dist/types/index.js +2 -0
- package/dist/types/index.js.map +1 -0
- package/dist/types/libreapps-user-info-value.d.ts +6 -0
- package/dist/types/libreapps-user-info-value.js +2 -0
- package/dist/types/libreapps-user-info-value.js.map +1 -0
- package/dist/types/libreapps-user-info.d.ts +6 -0
- package/dist/types/libreapps-user-info.js +2 -0
- package/dist/types/libreapps-user-info.js.map +1 -0
- package/dist/util/analytics.d.ts +9 -0
- package/dist/util/analytics.js +10 -0
- package/dist/util/analytics.js.map +1 -0
- package/icons/ethereum.tsx +18 -0
- package/icons/facebook.tsx +11 -0
- package/icons/github.tsx +14 -0
- package/icons/google.tsx +13 -0
- package/icons/index.ts +16 -0
- package/index.ts +51 -0
- package/libreapps-ui.d.ts +23 -0
- package/package.json +57 -0
- package/server/firebase-support.ts +176 -0
- package/server/index.ts +14 -0
- package/server/rest-api-handlers.ts +33 -0
- package/server/stub-server.ts +28 -0
- package/service/auth-service.ts +21 -0
- package/service/context.tsx +46 -0
- package/service/get-singleton.ts +37 -0
- package/service/impl/firebase-support.ts +241 -0
- package/service/impl/index.ts +231 -0
- package/service/impl/stub-auth-service.ts +131 -0
- package/service/impl/wallet-support.ts +100 -0
- package/service/index.ts +11 -0
- package/service/provider-registry.ts +71 -0
- package/tsconfig.json +15 -0
- package/types/api-response.ts +5 -0
- package/types/auth-service-conf.ts +9 -0
- package/types/index.ts +6 -0
- package/types/libreapps-user-info-value.ts +9 -0
- package/types/libreapps-user-info.ts +9 -0
- package/util/analytics.ts +21 -0
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
# @libreapps/auth
|
|
2
|
+
|
|
3
|
+
## 3.0.1
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- Reverted package scope to @libreapps.
|
|
8
|
+
- Updated dependencies
|
|
9
|
+
- @libreapps/ui@5.4.1
|
|
10
|
+
|
|
11
|
+
## 3.0.0
|
|
12
|
+
|
|
13
|
+
### Minor Changes
|
|
14
|
+
|
|
15
|
+
- 6f41d89: Initial release of LibreApps UI packages to NPM
|
|
16
|
+
|
|
17
|
+
### Patch Changes
|
|
18
|
+
|
|
19
|
+
- Updated dependencies [6f41d89]
|
|
20
|
+
- @libreapps/ui@5.4.0
|
package/LICENSE.md
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 LibreApps.com
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
# LibreApps Auth
|
|
2
|
+
|
|
3
|
+
## Initialization
|
|
4
|
+
|
|
5
|
+
Follow these steps to initialize authentication with LibreApps Auth for your project:
|
|
6
|
+
|
|
7
|
+
1. Install package with `pnpm i @libreapps/auth`
|
|
8
|
+
|
|
9
|
+
2. Create custom Header component that includes an `AuthWidget` from @libreapps/auth. See example in [luxdefi/sites repo](https://github.com/luxdefi/sites/tree/main/sites/credit/src/components/header)
|
|
10
|
+
|
|
11
|
+
3. Edit root layout component
|
|
12
|
+
``` typescript
|
|
13
|
+
// ... other imports
|
|
14
|
+
|
|
15
|
+
import { AuthServiceProvider } from '@libreapps/auth/service'
|
|
16
|
+
import { getUserServerSide } from '@libreapps/auth/server'
|
|
17
|
+
import type { AuthServiceConf } from '@libreapps/auth/types'
|
|
18
|
+
|
|
19
|
+
// custom header with AuthWidget
|
|
20
|
+
import Header from '@/components/header'
|
|
21
|
+
|
|
22
|
+
// ...other code
|
|
23
|
+
|
|
24
|
+
const RootLayout: React.FC<PropsWithChildren> = async ({
|
|
25
|
+
children
|
|
26
|
+
}) => {
|
|
27
|
+
const currentUser = await getUserServerSide()
|
|
28
|
+
|
|
29
|
+
return (
|
|
30
|
+
<AuthServiceProvider user={currentUser} conf={{} as AuthServiceConf}>
|
|
31
|
+
<RootLayoutCommon siteDef={siteDef} header={false} >
|
|
32
|
+
<Header siteDef={siteDef}/>
|
|
33
|
+
{children}
|
|
34
|
+
</RootLayoutCommon>
|
|
35
|
+
</AuthServiceProvider>
|
|
36
|
+
)
|
|
37
|
+
}
|
|
38
|
+
export default RootLayout
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
4. Create `/api/auth/login` route.
|
|
42
|
+
``` Typescript
|
|
43
|
+
import { NextRequest } from 'next/server'
|
|
44
|
+
|
|
45
|
+
import { handleLoginApiRequest } from '@libreapps/auth/server'
|
|
46
|
+
|
|
47
|
+
export async function POST(request: NextRequest) {
|
|
48
|
+
return handleLoginApiRequest(request)
|
|
49
|
+
}
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
5. Create `/api/auth/logout` route.
|
|
53
|
+
``` Typescript
|
|
54
|
+
import { handleLogoutApiRequest } from '@libreapps/auth/server'
|
|
55
|
+
|
|
56
|
+
export async function GET() {
|
|
57
|
+
return handleLogoutApiRequest()
|
|
58
|
+
}
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
6. Create `/login` route that uses `LoginComponent` from @libreapps/auth. See example in [luxdefi/sites repo](https://github.com/luxdefi/sites/tree/main/sites/credit/src/app/login)
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
'use client'
|
|
2
|
+
import React from 'react'
|
|
3
|
+
import { observer } from 'mobx-react-lite'
|
|
4
|
+
|
|
5
|
+
import {
|
|
6
|
+
Button,
|
|
7
|
+
LinkElement,
|
|
8
|
+
Popover,
|
|
9
|
+
PopoverContent,
|
|
10
|
+
PopoverTrigger,
|
|
11
|
+
Separator
|
|
12
|
+
} from '@libreapps/ui/primitives'
|
|
13
|
+
|
|
14
|
+
import type { LinkDef } from '@libreapps/ui/types'
|
|
15
|
+
import { cn } from '@libreapps/ui/util'
|
|
16
|
+
|
|
17
|
+
import { useAuth } from '../service'
|
|
18
|
+
|
|
19
|
+
import Ethereum from '../icons/ethereum'
|
|
20
|
+
|
|
21
|
+
const AuthWidget: React.FC<{
|
|
22
|
+
/** Does not show the 'login' button when no auth user.
|
|
23
|
+
* ie, Only shows the profile widget (when there's an auth user)
|
|
24
|
+
*/
|
|
25
|
+
noLogin?: boolean
|
|
26
|
+
className?: string
|
|
27
|
+
/** Overrides default of navigating to '/login' route.
|
|
28
|
+
* eg, if client code provides it's own ogin form
|
|
29
|
+
* */
|
|
30
|
+
handleLogin?: () => void
|
|
31
|
+
}> = observer(({
|
|
32
|
+
noLogin=false,
|
|
33
|
+
handleLogin,
|
|
34
|
+
className
|
|
35
|
+
}) => {
|
|
36
|
+
|
|
37
|
+
const auth = useAuth()
|
|
38
|
+
|
|
39
|
+
// This safegaurds against a crash in the rare case
|
|
40
|
+
// that multiple inistances of auth module are in play.
|
|
41
|
+
// We safegaurd agains this in the by loading peer Deps from
|
|
42
|
+
// the root in host mono repos, but this is another layer.
|
|
43
|
+
// Also, this allows sites to opt-in to showing an auth widget without
|
|
44
|
+
// configuration, by just providing the context.
|
|
45
|
+
if (!auth || noLogin) {
|
|
46
|
+
return null
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
// If that is the case, the widget will always show the 'login'
|
|
50
|
+
// button regardless of status.
|
|
51
|
+
if (!auth.loggedIn && typeof window !== 'undefined') {
|
|
52
|
+
|
|
53
|
+
return (handleLogin ? (
|
|
54
|
+
<Button
|
|
55
|
+
variant='primary'
|
|
56
|
+
className='h-8 w-fit !min-w-0'
|
|
57
|
+
onClick={handleLogin}
|
|
58
|
+
>
|
|
59
|
+
Login
|
|
60
|
+
</Button>
|
|
61
|
+
) : (
|
|
62
|
+
<LinkElement
|
|
63
|
+
def={{
|
|
64
|
+
href: `${process.env.NEXT_PUBLIC_LOGIN_SITE_URL}?redirectUrl=${window.location.href}`,
|
|
65
|
+
title: 'Login',
|
|
66
|
+
variant: 'primary',
|
|
67
|
+
newTab: false
|
|
68
|
+
} satisfies LinkDef}
|
|
69
|
+
className='h-8 w-fit !min-w-0'
|
|
70
|
+
/>
|
|
71
|
+
))
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
return (
|
|
75
|
+
<Popover>
|
|
76
|
+
<PopoverTrigger asChild>
|
|
77
|
+
<Button
|
|
78
|
+
variant="outline"
|
|
79
|
+
size='icon'
|
|
80
|
+
className={cn('rounded-full text-muted border-2 border-muted bg-level-1 hover:bg-level-2 hover:border-foreground hover:text-foreground uppercase w-8 h-8', className)}
|
|
81
|
+
>{auth.user?.email[0]}</Button>
|
|
82
|
+
</PopoverTrigger>
|
|
83
|
+
<PopoverContent className='bg-level-0'>
|
|
84
|
+
<div className="grid gap-4">
|
|
85
|
+
<div className="space-y-2 truncate">
|
|
86
|
+
{auth.user?.displayName ? (
|
|
87
|
+
<>
|
|
88
|
+
<h4 className="font-medium leading-none truncate">{auth.user.displayName}</h4>
|
|
89
|
+
<p className="text-sm opacity-50 truncate">{auth.user.email}</p>
|
|
90
|
+
</>
|
|
91
|
+
) : (
|
|
92
|
+
<h4 className="font-medium leading-none truncate">{auth.user?.email}</h4>
|
|
93
|
+
)}
|
|
94
|
+
{auth.user?.walletAddress ? (
|
|
95
|
+
<p className="text-sm opacity-50 truncate">{auth.user.walletAddress}</p>
|
|
96
|
+
) : (
|
|
97
|
+
<Button variant="outline" className='w-full flex items-center gap-2' onClick={auth.associateWallet.bind(auth)}>
|
|
98
|
+
<Ethereum height={20}/>Connect your wallet
|
|
99
|
+
</Button>
|
|
100
|
+
)}
|
|
101
|
+
</div>
|
|
102
|
+
<Separator />
|
|
103
|
+
<Button variant="outline" onClick={auth.logout.bind(auth)}>Logout</Button>
|
|
104
|
+
</div>
|
|
105
|
+
</PopoverContent>
|
|
106
|
+
</Popover>
|
|
107
|
+
)
|
|
108
|
+
})
|
|
109
|
+
|
|
110
|
+
export default AuthWidget
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
// @ts-nocheck
|
|
2
|
+
'use client'
|
|
3
|
+
|
|
4
|
+
import { zodResolver } from '@hookform/resolvers/zod'
|
|
5
|
+
import * as z from 'zod'
|
|
6
|
+
import { useForm } from 'react-hook-form'
|
|
7
|
+
|
|
8
|
+
import {
|
|
9
|
+
Button,
|
|
10
|
+
Input
|
|
11
|
+
} from '@libreapps/ui/primitives'
|
|
12
|
+
|
|
13
|
+
import {
|
|
14
|
+
Form,
|
|
15
|
+
FormControl,
|
|
16
|
+
FormField,
|
|
17
|
+
FormItem,
|
|
18
|
+
FormMessage,
|
|
19
|
+
} from '@libreapps/ui/form'
|
|
20
|
+
|
|
21
|
+
import { cn } from '@libreapps/ui/util'
|
|
22
|
+
|
|
23
|
+
const formSchema = z.object({
|
|
24
|
+
email: z.string().email(),
|
|
25
|
+
password: z.string().min(2, 'Password must be at least 2 characters'),
|
|
26
|
+
})
|
|
27
|
+
|
|
28
|
+
const EmailPasswordForm: React.FC<{
|
|
29
|
+
onSubmit: (email: string, password: string) => void
|
|
30
|
+
isLoading: boolean
|
|
31
|
+
className?: string
|
|
32
|
+
inputClx?: string
|
|
33
|
+
prompt?: string
|
|
34
|
+
}> = ({
|
|
35
|
+
onSubmit,
|
|
36
|
+
isLoading,
|
|
37
|
+
className,
|
|
38
|
+
inputClx,
|
|
39
|
+
prompt='Login'
|
|
40
|
+
}) => {
|
|
41
|
+
const form = useForm<z.infer<typeof formSchema>>({
|
|
42
|
+
resolver: zodResolver(formSchema),
|
|
43
|
+
defaultValues: {
|
|
44
|
+
email: '',
|
|
45
|
+
password: '',
|
|
46
|
+
},
|
|
47
|
+
})
|
|
48
|
+
|
|
49
|
+
return (
|
|
50
|
+
<Form {...form}>
|
|
51
|
+
<form
|
|
52
|
+
onSubmit={form.handleSubmit(({email, password}) => onSubmit(email, password))}
|
|
53
|
+
className={cn('text-left', className)}
|
|
54
|
+
>
|
|
55
|
+
<FormField
|
|
56
|
+
control={form.control}
|
|
57
|
+
name='email'
|
|
58
|
+
render={({ field }) => (
|
|
59
|
+
<FormItem className='space-y-1'>
|
|
60
|
+
<FormControl>
|
|
61
|
+
<Input
|
|
62
|
+
{...field}
|
|
63
|
+
className={inputClx}
|
|
64
|
+
placeholder='Email'
|
|
65
|
+
disabled={isLoading}
|
|
66
|
+
/>
|
|
67
|
+
</FormControl>
|
|
68
|
+
<FormMessage />
|
|
69
|
+
</FormItem>
|
|
70
|
+
)}
|
|
71
|
+
/>
|
|
72
|
+
<FormField
|
|
73
|
+
control={form.control}
|
|
74
|
+
name='password'
|
|
75
|
+
render={({ field }) => (
|
|
76
|
+
<FormItem className='space-y-1'>
|
|
77
|
+
<FormControl>
|
|
78
|
+
<Input
|
|
79
|
+
type='password'
|
|
80
|
+
{...field}
|
|
81
|
+
className={inputClx}
|
|
82
|
+
placeholder='Password'
|
|
83
|
+
disabled={isLoading}
|
|
84
|
+
/>
|
|
85
|
+
</FormControl>
|
|
86
|
+
<FormMessage />
|
|
87
|
+
</FormItem>
|
|
88
|
+
)}
|
|
89
|
+
/>
|
|
90
|
+
<Button type='submit' className='w-full' disabled={isLoading}>{prompt}</Button>
|
|
91
|
+
</form>
|
|
92
|
+
</Form>
|
|
93
|
+
)
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
export default EmailPasswordForm
|
|
@@ -0,0 +1,207 @@
|
|
|
1
|
+
'use client'
|
|
2
|
+
import { useState, useEffect, type PropsWithChildren } from 'react'
|
|
3
|
+
import { useRouter } from 'next/navigation'
|
|
4
|
+
import { observer } from 'mobx-react-lite'
|
|
5
|
+
import Link from 'next/link'
|
|
6
|
+
|
|
7
|
+
import { ApplyTypography, Button, Separator, toast, Toaster } from '@libreapps/ui/primitives'
|
|
8
|
+
import { cn } from '@libreapps/ui/util'
|
|
9
|
+
|
|
10
|
+
import { useAuth, type AuthProvider } from '../service'
|
|
11
|
+
import { Facebook, Google, GitHub } from '../icons'
|
|
12
|
+
import EmailPasswordForm from './email-password-form'
|
|
13
|
+
import { sendGAEvent } from '../util/analytics'
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
const ProviderLoginButton: React.FC<PropsWithChildren & {
|
|
17
|
+
provider: AuthProvider,
|
|
18
|
+
loginWithProvider: (provider: AuthProvider) => Promise<void>,
|
|
19
|
+
isLoading: boolean
|
|
20
|
+
}> = ({
|
|
21
|
+
provider,
|
|
22
|
+
loginWithProvider,
|
|
23
|
+
isLoading,
|
|
24
|
+
children
|
|
25
|
+
}) => {
|
|
26
|
+
|
|
27
|
+
return (
|
|
28
|
+
<Button
|
|
29
|
+
onClick={() => {loginWithProvider(provider)}}
|
|
30
|
+
className='w-full mx-auto flex items-center gap-2'
|
|
31
|
+
disabled={isLoading}
|
|
32
|
+
variant='outline'
|
|
33
|
+
>
|
|
34
|
+
{children}
|
|
35
|
+
</Button>
|
|
36
|
+
)
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
const LoginPanel: React.FC<PropsWithChildren & {
|
|
40
|
+
redirectUrl?: string,
|
|
41
|
+
getStartedUrl?: string,
|
|
42
|
+
className?: string,
|
|
43
|
+
inputClx?: string,
|
|
44
|
+
noHeading?: boolean
|
|
45
|
+
onLoginChanged?: (token: string) => void
|
|
46
|
+
termsOfServiceUrl?: string
|
|
47
|
+
privacyPolicyUrl?: string
|
|
48
|
+
setIsLogin?: React.Dispatch<React.SetStateAction<boolean>>
|
|
49
|
+
}> = observer(({
|
|
50
|
+
children,
|
|
51
|
+
redirectUrl,
|
|
52
|
+
getStartedUrl,
|
|
53
|
+
className,
|
|
54
|
+
inputClx,
|
|
55
|
+
noHeading,
|
|
56
|
+
onLoginChanged,
|
|
57
|
+
termsOfServiceUrl,
|
|
58
|
+
privacyPolicyUrl,
|
|
59
|
+
setIsLogin
|
|
60
|
+
}) => {
|
|
61
|
+
|
|
62
|
+
const router = useRouter()
|
|
63
|
+
const auth = useAuth()
|
|
64
|
+
const [isLoading, setIsLoading] = useState(false)
|
|
65
|
+
const [getStartUrl, setGetStartUrl] = useState('')
|
|
66
|
+
|
|
67
|
+
const succeed = async (loginMethod: AuthProvider | 'email' | null) => {
|
|
68
|
+
|
|
69
|
+
if (loginMethod) {
|
|
70
|
+
sendGAEvent('login', { method: loginMethod })
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
// If a callback is provided, don't redirect.
|
|
74
|
+
// Assume host code is handling (eg, mobile menu)
|
|
75
|
+
if (onLoginChanged) {
|
|
76
|
+
const res = await fetch(
|
|
77
|
+
'/api/auth/generate-custom-token',
|
|
78
|
+
{ method: 'POST' }
|
|
79
|
+
).then(res => res.json())
|
|
80
|
+
onLoginChanged(res.token?.token ?? null)
|
|
81
|
+
}
|
|
82
|
+
else if (redirectUrl) {
|
|
83
|
+
// TODO :aa shouldn't the token thing happen in this case too??
|
|
84
|
+
router.push(redirectUrl)
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
const loginWithEmailPassword = async (email: string, password: string) => {
|
|
89
|
+
setIsLoading(true)
|
|
90
|
+
try {
|
|
91
|
+
const res = await auth.loginEmailAndPassword(email, password)
|
|
92
|
+
if (res.success) {
|
|
93
|
+
succeed('email'); toast.success(res.message)
|
|
94
|
+
}
|
|
95
|
+
else {
|
|
96
|
+
toast.error(res.message)
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
catch (e) {
|
|
100
|
+
toast('User with this email already signed up using a different provider')
|
|
101
|
+
}
|
|
102
|
+
setIsLoading(false)
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
// const loginWithEthereum = async () => {
|
|
106
|
+
// setIsLoading(true)
|
|
107
|
+
// try {
|
|
108
|
+
// const res = await signInWithEthereum()
|
|
109
|
+
// if (res.success && res.user) {
|
|
110
|
+
// setUser({email: res.user?.email, displayName: res.user?.displayName, walletAddress: res.user?.walletAddress})
|
|
111
|
+
// if (redirectUrl) {
|
|
112
|
+
// router.push(redirectUrl)
|
|
113
|
+
// }
|
|
114
|
+
// }
|
|
115
|
+
// } catch (e) {
|
|
116
|
+
// toast({title: 'No Ethereum provider found'})
|
|
117
|
+
// }
|
|
118
|
+
// setIsLoading(false)
|
|
119
|
+
// }
|
|
120
|
+
|
|
121
|
+
const loginWithProvider = async (provider: AuthProvider) => {
|
|
122
|
+
setIsLoading(true)
|
|
123
|
+
const res = await auth.loginWithProvider(provider)
|
|
124
|
+
if (res.success) {
|
|
125
|
+
succeed(provider)
|
|
126
|
+
}
|
|
127
|
+
setIsLoading(false)
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
const logout = async () => {
|
|
131
|
+
setIsLoading(true)
|
|
132
|
+
const res = await auth.logout()
|
|
133
|
+
if (res.success) {
|
|
134
|
+
succeed(null)
|
|
135
|
+
}
|
|
136
|
+
setIsLoading(false)
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
const handleOnClick = () => {
|
|
140
|
+
if (setIsLogin) {
|
|
141
|
+
setIsLogin(false)
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
const generateUsernameFromEmail = (email: string) => {
|
|
146
|
+
const emailPrefix = email.split('@')[0]; // Get the part before the @ symbol
|
|
147
|
+
const randomSuffix = Math.random().toString(36).substring(2, 8); // Generate a random string
|
|
148
|
+
return `${emailPrefix}_${randomSuffix}`;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
useEffect(() => {
|
|
152
|
+
if (auth.loggedIn) {
|
|
153
|
+
const username = auth.user?.email
|
|
154
|
+
if (username) setGetStartUrl('https://' + generateUsernameFromEmail(username) + '.' + getStartedUrl)
|
|
155
|
+
}
|
|
156
|
+
}, [auth])
|
|
157
|
+
|
|
158
|
+
return (
|
|
159
|
+
<ApplyTypography className={cn('w-full flex flex-col text-center !gap-3 LOGIN_OUTER', className)}>
|
|
160
|
+
{auth.loggedIn && !redirectUrl ? (
|
|
161
|
+
<>
|
|
162
|
+
<h4>Welcome!</h4>
|
|
163
|
+
{auth.user && (<> {/* this means the libreapps user isn't loaded yet ...*/}
|
|
164
|
+
<p>You are signed in as {auth.user?.displayName ?? auth.user?.email}</p>
|
|
165
|
+
<div className='flex flex-col md:flex-row gap-3 items-center justify-center'>
|
|
166
|
+
<Button variant='primary' onClick={() => router.push(getStartUrl)} className='w-full'>GET STARTED</Button>
|
|
167
|
+
<Button onClick={logout} variant='outline' disabled={isLoading} className='w-full'>Sign Out</Button>
|
|
168
|
+
</div>
|
|
169
|
+
</>)}
|
|
170
|
+
</>
|
|
171
|
+
) : (
|
|
172
|
+
<>
|
|
173
|
+
{!noHeading && (
|
|
174
|
+
<h4 className='text-center'>Login</h4>
|
|
175
|
+
)}
|
|
176
|
+
{children}
|
|
177
|
+
<EmailPasswordForm onSubmit={loginWithEmailPassword} isLoading={isLoading} className='mb-4' inputClx={inputClx} />
|
|
178
|
+
|
|
179
|
+
<div className='flex flex-row gap-4 justify-center'>
|
|
180
|
+
<div className='text-muted-2 text-sm'>Don't have an account?</div>
|
|
181
|
+
<button className='bg-transparent text-foreground text-sm' onClick={handleOnClick}>Sign Up</button>
|
|
182
|
+
</div>
|
|
183
|
+
|
|
184
|
+
<div className='flex gap-2 whitespace-nowrap items-center my-1 sm:my-3 text-xs text-muted'>
|
|
185
|
+
<Separator className='grow w-auto' /><div className='shrink-0 mx-1'>or continue with</div><Separator className='grow w-auto' />
|
|
186
|
+
</div>
|
|
187
|
+
|
|
188
|
+
{/* <Button onClick={loginWithEthereum} className='w-full mx-auto flex items-center gap-2' disabled={isLoading}>
|
|
189
|
+
<Ethereum height={20}/>Login with your wallet
|
|
190
|
+
</Button> */}
|
|
191
|
+
<ProviderLoginButton provider='google' isLoading={isLoading} loginWithProvider={loginWithProvider}>
|
|
192
|
+
<Google height={20} />Google
|
|
193
|
+
</ProviderLoginButton>
|
|
194
|
+
<ProviderLoginButton provider='facebook' isLoading={isLoading} loginWithProvider={loginWithProvider}>
|
|
195
|
+
<Facebook height={20} />Facebook
|
|
196
|
+
</ProviderLoginButton>
|
|
197
|
+
<ProviderLoginButton provider='github' isLoading={isLoading} loginWithProvider={loginWithProvider}>
|
|
198
|
+
<GitHub height={20} />Github
|
|
199
|
+
</ProviderLoginButton>
|
|
200
|
+
<p className='text-sm text-muted-2'>By logging in, you agree to our <Link href={termsOfServiceUrl ?? ''} target='_blank'>Terms of Service</Link> and <Link href={privacyPolicyUrl ?? ''} target='_blank'>Privacy Policy</Link>.</p>
|
|
201
|
+
</>
|
|
202
|
+
)}
|
|
203
|
+
</ApplyTypography>
|
|
204
|
+
)
|
|
205
|
+
})
|
|
206
|
+
|
|
207
|
+
export default LoginPanel
|