-
Notifications
You must be signed in to change notification settings - Fork 1
/
auth.tsx
75 lines (58 loc) · 1.74 KB
/
auth.tsx
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import {
API_REGIONS,
Client,
} from "@amityco/ts-sdk";
import { useEffect, useState } from "react";
interface IAuth {
children?: any;
onConnected?: any;
}
function AuthProvider({ children, onConnected }: IAuth) {
const [sessionState, setSessionState] = useState('');
console.log('sessionState:', sessionState)
const apiKey = "b3babb0b3a89f4341d31dc1a01091edcd70f8de7b23d697f"; // Input your api key here
const client: Amity.Client = Client.createClient(apiKey, 'sg', {
apiEndpoint: { http: 'https://api.sg.amity.co' },
});
useEffect(() => {
return Client.onSessionStateChange((state: Amity.SessionStates) => setSessionState(state));
}, []);
const handleConnect = async (userId: string, displayName: string) => {
const response = await Client.login(
{
userId: userId,
displayName: displayName, // optional
},
sessionHandler,
);
console.log('response:', response)
Client.startUnreadSync();
};
useEffect(() => {
setSessionState(client.sessionState)
}, [client])
const sessionHandler: Amity.SessionHandler = {
sessionWillRenewAccessToken(renewal: Amity.AccessTokenRenewal) {
// for details on other renewal methods check session handler
renewal.renew();
},
};
function login() {
// console.log('client: ', client);
console.log('API_REGIONS.SG: ', API_REGIONS.SG);
// console.log("client: ", client.token?.accessToken);
handleConnect("top", "top");
// enableCache()y
}
function logClient(){
console.log('client==:', client)
}
return (
<div>
<button onClick={login}> Login</button>
<button onClick={logClient}> Log Client</button>
{children}
</div>
);
}
export default AuthProvider;