ente/src/services/userService.ts

80 lines
1.9 KiB
TypeScript
Raw Normal View History

import HTTPService from './HTTPService';
2021-04-02 04:03:00 +00:00
import { KeyAttributes } from 'types';
import { getEndpoint } from 'utils/common/apiUtil';
2021-03-12 04:50:58 +00:00
import { clearKeys } from 'utils/storage/sessionStorage';
import router from 'next/router';
import { clearData } from 'utils/storage/localStorage';
2021-03-16 13:32:43 +00:00
import localForage from 'utils/storage/localForage';
2021-03-29 06:22:15 +00:00
import { getToken } from 'utils/common/key';
2021-04-05 05:39:25 +00:00
export interface UpdatedKey {
kekSalt: string;
encryptedKey: string;
keyDecryptionNonce: string;
memLimit: number;
opsLimit: number;
}
const ENDPOINT = getEndpoint();
2020-09-14 09:32:01 +00:00
2021-02-16 11:45:06 +00:00
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) => {
2020-09-14 09:32:01 +00:00
return HTTPService.get(`${ENDPOINT}/users/credentials`, { email, ott });
};
export const putAttributes = (
token: string,
name: string,
2021-04-02 04:03:00 +00:00
keyAttributes: KeyAttributes
) => {
console.log('name ' + name);
return HTTPService.put(
`${ENDPOINT}/users/attributes`,
{ name: name, keyAttributes: keyAttributes },
null,
{
'X-Auth-Token': token,
}
);
};
2021-03-12 04:50:58 +00:00
2021-04-05 05:39:25 +00:00
export const setKeys = (token: string, updatedKey: UpdatedKey) => {
return HTTPService.put(`${ENDPOINT}/users/keys`, updatedKey, null, {
'X-Auth-Token': token,
});
};
2021-03-12 04:50:58 +00:00
export const logoutUser = async () => {
clearKeys();
clearData();
const cache = 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;
}
};