-
Notifications
You must be signed in to change notification settings - Fork 214
Expand file tree
/
Copy pathuse-auth-helper.tsx
More file actions
56 lines (51 loc) · 2.01 KB
/
use-auth-helper.tsx
File metadata and controls
56 lines (51 loc) · 2.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/*
* Copyright (c) 2021, salesforce.com, inc.
* All rights reserved.
* SPDX-License-Identifier: BSD-3-Clause
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/
import React from 'react'
import {AuthHelpers, useAuthHelper} from '@salesforce/commerce-sdk-react'
import Json from '../components/Json'
const UseAuthHelper = () => {
// use string or enum
const loginGuestUser = useAuthHelper(AuthHelpers.LoginGuestUser)
const loginRegisteredUser = useAuthHelper(AuthHelpers.LoginRegisteredUserB2C)
const logout = useAuthHelper(AuthHelpers.Logout)
const isError = (err: unknown): err is Error => err instanceof Error
//logout before logging guest user in
const loginGuestUserFlow = async () => {
await logout.mutateAsync()
loginGuestUser.mutate()
}
return (
<>
<h1>LoginGuestUser</h1>
<Json data={loginGuestUser} />
<button onClick={() => void loginGuestUserFlow()}>loginGuestUser</button>
{isError(loginGuestUser.error) && (
<p style={{color: 'red'}}>Error: {loginGuestUser.error.message}</p>
)}
<hr />
<h1>LoginRegisteredUserB2C</h1>
<Json data={loginRegisteredUser} />
<button
onClick={() =>
loginRegisteredUser.mutate({username: 'alex@test.com', password: 'Test1234#'})
}
>
loginRegisteredUser
</button>
{isError(loginRegisteredUser.error) && (
<p style={{color: 'red'}}>Error: {loginRegisteredUser.error.message}</p>
)}
<hr />
<h1>Logout</h1>
<Json data={logout} />
<button onClick={() => logout.mutate()}>logout</button>
{isError(logout.error) && <p style={{color: 'red'}}>Error: {logout.error.message}</p>}
</>
)
}
UseAuthHelper.getTemplateName = () => 'UseAuthHelper'
export default UseAuthHelper