ente/src/services/userService.ts
2021-04-05 11:09:25 +05:30

80 lines
1.9 KiB
TypeScript

import HTTPService from './HTTPService';
import { KeyAttributes } from 'types';
import { getEndpoint } from 'utils/common/apiUtil';
import { clearKeys } from 'utils/storage/sessionStorage';
import router from 'next/router';
import { clearData } from 'utils/storage/localStorage';
import localForage from 'utils/storage/localForage';
import { getToken } from 'utils/common/key';
export interface UpdatedKey {
kekSalt: string;
encryptedKey: string;
keyDecryptionNonce: string;
memLimit: number;
opsLimit: number;
}
const ENDPOINT = getEndpoint();
export interface user {
id: number;
name: string;
email: string;
}
export const getOtt = (email: string) => {
return HTTPService.get(`${ENDPOINT}/users/ott`, {
email: email,
client: 'web',
});
};
export const verifyOtt = (email: string, ott: string) => {
return HTTPService.get(`${ENDPOINT}/users/credentials`, { email, ott });
};
export const putAttributes = (
token: string,
name: string,
keyAttributes: KeyAttributes
) => {
console.log('name ' + name);
return HTTPService.put(
`${ENDPOINT}/users/attributes`,
{ name: name, keyAttributes: keyAttributes },
null,
{
'X-Auth-Token': token,
}
);
};
export const setKeys = (token: string, updatedKey: UpdatedKey) => {
return HTTPService.put(`${ENDPOINT}/users/keys`, updatedKey, null, {
'X-Auth-Token': token,
});
};
export const logoutUser = async () => {
clearKeys();
clearData();
const cache = await caches.delete('thumbs');
await clearFiles();
router.push('/');
};
export const clearFiles = async () => {
await localForage.clear();
};
export const isTokenValid = async () => {
try {
await HTTPService.get(`${ENDPOINT}/users/session-validity`, null, {
'X-Auth-Token': getToken(),
});
return true;
} catch (e) {
return false;
}
};