-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
176 lines (154 loc) · 6.03 KB
/
index.ts
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/* eslint-disable no-console */
import { SDKProvider } from '@metamask/sdk/dist/browser/es/src/provider/SDKProvider';
import { keyStores } from '@pitchtalk/contract-api-js';
import { EUserRoles } from '@pitchtalk/contract-api-js/dist/interfaces';
import { compose, createAsyncThunk } from '@reduxjs/toolkit';
import * as authAPI from 'services/api/authAPI';
import * as userAPI from 'services/api/userAPI';
import { networkId } from 'services/config';
import { ToastType } from 'services/toast/constants';
import web3Service from 'services/web3';
import { ToastLink } from 'shared/components/toast-link/ToastLink';
import { EMPTY_STRING } from 'shared/constants';
import { ESTORAGE_KEYS, getItem, setItem } from 'shared/utils/storage';
import {
setOffChainUserData,
setOffChainUserProjects,
setUserDisplayName,
setUserImg,
setUserOffChainId,
setUserProvider,
setUserRole,
} from 'store/slices/user';
import { AppDispatch } from 'store/store';
import { EProviders } from 'store/types/user';
export const authWithGoogle = () => () => authAPI.fetchLoginWithGoogle();
export const authWithTwitter = () => () =>
authAPI.fetchLoginWithTwitter().catch(console.error);
export const authWithFaceBook = () => () =>
authAPI.fetchLoginWithFacebook().catch(console.error);
export const getUserData = () => (dispatch: AppDispatch) =>
userAPI
.fetchUserData()
.then(compose(dispatch, setOffChainUserData))
.catch(console.error);
export const getUserProjects = createAsyncThunk(
'offchain/user/projects',
(_, { dispatch }) =>
userAPI.fetchUserProjects().then(compose(dispatch, setOffChainUserProjects))
);
export const logoutUser = () => (dispatch: AppDispatch) => {
localStorage.removeItem(ESTORAGE_KEYS.GOOGLE_ACCESS_TOKEN);
localStorage.removeItem(ESTORAGE_KEYS.GOOGLE_REFRESH_TOKEN);
localStorage.removeItem(ESTORAGE_KEYS.NEAR_ACCESS_TOKEN);
localStorage.removeItem(ESTORAGE_KEYS.NEAR_REFRESH_TOKEN);
dispatch(setOffChainUserData(null));
dispatch(setUserOffChainId(null));
dispatch(setUserRole(EUserRoles.USER));
dispatch(setUserImg(EMPTY_STRING));
dispatch(setUserDisplayName(EMPTY_STRING));
dispatch(setUserProvider(EProviders.NEAR));
dispatch(setOffChainUserProjects({ subProject: null, project: null }));
};
export const authWithMetamask = () => async (dispatch: AppDispatch) => {
const address = await web3Service.getMetamaskAddress();
if (!address) return;
const nonce = await authAPI.fetchMetamaskNonce(address);
const signed = await web3Service.signMessage(nonce, address);
const { accessToken, refreshToken } = await authAPI.loginWithMetamask(
address,
signed
);
if (accessToken && refreshToken) {
setItem(ESTORAGE_KEYS.GOOGLE_ACCESS_TOKEN, accessToken);
setItem(ESTORAGE_KEYS.GOOGLE_REFRESH_TOKEN, refreshToken);
}
const accessTokenStorage = getItem(ESTORAGE_KEYS.GOOGLE_ACCESS_TOKEN);
const refreshTokenStorage = getItem(ESTORAGE_KEYS.GOOGLE_REFRESH_TOKEN);
if (accessTokenStorage && refreshTokenStorage) {
dispatch(getUserData());
dispatch(getUserProjects());
}
};
export const authWithMetamaskSDK = () => async (dispatch: AppDispatch) => {
try {
if (!window.ethereum) {
await web3Service.initMetamaskSDK();
}
const ethereum = window.ethereum as SDKProvider;
const addresses = await ethereum.request({
method: 'eth_requestAccounts',
params: [],
});
const address: string = (addresses as any)?.[0] || '';
if (!address) return;
const nonce = await authAPI.fetchMetamaskNonce(address);
const message = web3Service.prepareMessage(nonce);
const toast = ToastLink(
'',
'Confirm your action in Metamask',
ToastType.Success
);
const signed = await ethereum.request({
method: 'personal_sign',
params: [message, address],
});
const { accessToken, refreshToken } = await authAPI.loginWithMetamask(
address,
signed as string
);
if (accessToken && refreshToken) {
setItem(ESTORAGE_KEYS.GOOGLE_ACCESS_TOKEN, accessToken);
setItem(ESTORAGE_KEYS.GOOGLE_REFRESH_TOKEN, refreshToken);
}
const accessTokenStorage = getItem(ESTORAGE_KEYS.GOOGLE_ACCESS_TOKEN);
const refreshTokenStorage = getItem(ESTORAGE_KEYS.GOOGLE_REFRESH_TOKEN);
if (accessTokenStorage && refreshTokenStorage) {
dispatch(getUserData());
dispatch(getUserProjects());
}
} catch (error) {
const toast = ToastLink(
'',
'Something when wrong. Please, try again',
ToastType.Error
);
}
};
export const authWithNear =
(accountId: string) => async (dispatch: AppDispatch) => {
const keystore = await new keyStores.BrowserLocalStorageKeyStore().getKey(
networkId,
accountId
);
const nonce = await authAPI
.fetchMetamaskNonce(accountId)
.catch(() => console.log('Error when login'));
if (!nonce) return;
const uint8Array = new TextEncoder().encode(nonce);
const { signature } = keystore.sign(uint8Array);
const publicKey = keystore.getPublicKey();
const message = Buffer.from(signature).toString('base64');
const nonceHash = new TextEncoder().encode(nonce);
const signature8 = Buffer.from(message, 'base64');
const isValid = publicKey.verify(nonceHash, signature8);
const res = await authAPI
.loginWithNear(accountId, message, publicKey.toString())
.catch(() => console.log('Error when login'));
if (res?.accessToken && res?.refreshToken) {
setItem(ESTORAGE_KEYS.NEAR_ACCESS_TOKEN, res.accessToken);
setItem(ESTORAGE_KEYS.NEAR_REFRESH_TOKEN, res.refreshToken);
userAPI.fetchUserData().then((user) => {
dispatch(setUserOffChainId(user.id));
dispatch(setUserRole(user.role));
dispatch(setUserImg(user.picture));
dispatch(setUserDisplayName(user.display_name));
dispatch(setUserProvider(user.provider as EProviders));
});
}
};
export const updateUserDisplayName =
(displayName: string) => (dispath: AppDispatch) =>
userAPI
.fetchChangeDisplayName(displayName)
.then(({ display_name }) => dispath(setUserDisplayName(display_name)));