ente/src/services/userService.ts

122 lines
3.1 KiB
TypeScript
Raw Normal View History

2021-05-30 16:56:48 +00:00
import { KeyAttributes } from 'types';
import { getEndpoint } from 'utils/common/apiUtil';
import { clearKeys } from 'utils/storage/sessionStorage';
2021-03-12 04:50:58 +00:00
import router from 'next/router';
2021-05-30 16:56:48 +00:00
import { clearData } from 'utils/storage/localStorage';
2021-03-16 13:32:43 +00:00
import localForage from 'utils/storage/localForage';
2021-05-30 16:56:48 +00:00
import { getToken } from 'utils/common/key';
2021-05-29 06:27:52 +00:00
import HTTPService from './HTTPService';
2021-04-05 05:39:25 +00:00
export interface UpdatedKey {
kekSalt: string;
encryptedKey: string;
keyDecryptionNonce: string;
memLimit: number;
opsLimit: number;
}
2021-04-05 09:03:12 +00:00
export interface RecoveryKey {
masterKeyEncryptedWithRecoveryKey: string;
masterKeyDecryptionNonce: string;
recoveryKeyEncryptedWithMasterKey: string;
recoveryKeyDecryptionNonce: string;
}
const ENDPOINT = getEndpoint();
2020-09-14 09:32:01 +00:00
export interface User {
2021-02-16 11:45:06 +00:00
id: number;
name: string;
email: string;
}
2021-06-14 14:54:10 +00:00
export interface VerificationResponse {
id: number;
keyAttributes: KeyAttributes;
encryptedToken?: string;
token?: string;
}
2021-06-17 12:41:40 +00:00
export interface TwoFactorSecret {
secretCode: string
2021-06-17 10:45:52 +00:00
qrCode: string
}
2021-02-16 11:45:06 +00:00
2021-05-29 06:27:52 +00:00
export const getOtt = (email: string) => HTTPService.get(`${ENDPOINT}/users/ott`, {
email,
client: 'web',
});
2021-04-28 08:00:15 +00:00
export const getPublicKey = async (email: string) => {
const token = getToken();
const resp = await HTTPService.get(
`${ENDPOINT}/users/public-key`,
2021-05-30 16:56:48 +00:00
{ email },
2021-04-28 08:00:15 +00:00
{
'X-Auth-Token': token,
2021-05-29 06:27:52 +00:00
},
2021-04-28 08:00:15 +00:00
);
2021-05-29 06:27:52 +00:00
return resp.data.publicKey;
};
2021-06-14 14:54:10 +00:00
export const verifyOtt = (email: string, ott: string) => HTTPService.post(`${ENDPOINT}/users/verify-email`, { email, ott });
2021-03-12 04:50:58 +00:00
2021-05-29 06:27:52 +00:00
export const putAttributes = (token: string, keyAttributes: KeyAttributes) => HTTPService.put(
`${ENDPOINT}/users/attributes`,
2021-05-30 16:56:48 +00:00
{ keyAttributes },
2021-05-29 06:27:52 +00:00
null,
{
2021-04-05 05:39:25 +00:00
'X-Auth-Token': token,
2021-05-29 06:27:52 +00:00
},
);
2021-04-05 05:39:25 +00:00
2021-05-29 06:27:52 +00:00
export const setKeys = (token: string, updatedKey: UpdatedKey) => HTTPService.put(`${ENDPOINT}/users/keys`, updatedKey, null, {
'X-Auth-Token': token,
});
export const setRecoveryKey = (token: string, recoveryKey: RecoveryKey) => HTTPService.put(
`${ENDPOINT}/users/recovery-key`,
recoveryKey,
null,
{
'X-Auth-Token': token,
},
);
2021-04-05 09:03:12 +00:00
2021-03-12 04:50:58 +00:00
export const logoutUser = async () => {
clearKeys();
clearData();
2021-05-29 06:27:52 +00:00
await caches.delete('thumbs');
await clearFiles();
2021-03-12 04:50:58 +00:00
router.push('/');
};
export const clearFiles = async () => {
await localForage.clear();
};
2021-03-29 06:22:15 +00:00
export const isTokenValid = async () => {
try {
2021-04-01 13:52:30 +00:00
await HTTPService.get(`${ENDPOINT}/users/session-validity`, null, {
2021-03-29 06:22:15 +00:00
'X-Auth-Token': getToken(),
});
return true;
} catch (e) {
return false;
}
};
2021-06-17 10:45:52 +00:00
export const setupTwoFactor = async () => {
const resp = await HTTPService.post(`${ENDPOINT}/users/two-factor/setup`, null, null, {
'X-Auth-Token': getToken(),
});
return resp.data as TwoFactorSecret;
};
export const enableTwoFactor = async (otp) => {
console.log(getToken());
const resp = await HTTPService.post(`${ENDPOINT}/users/two-factor/enable`, {
otp,
}, null, {
2021-06-17 10:45:52 +00:00
'X-Auth-Token': getToken(),
});
2021-06-17 12:41:40 +00:00
return resp.data as TwoFactorSecret;
2021-06-17 10:45:52 +00:00
};