feat: login token state

This commit is contained in:
DarkSky
2022-12-22 18:22:38 +08:00
parent cf95f0abaf
commit 8e17d56a51
6 changed files with 104 additions and 158 deletions
+2 -24
View File
@@ -1,29 +1,7 @@
import useSWR from 'swr';
import type { SWRConfiguration } from 'swr';
import { login, getUserByEmail } from './user';
import type {
LoginParams,
LoginResponse,
GetUserByEmailParams,
User,
} from './user';
export const LOGIN_SWR_KEY = 'user.token';
export function useLogin(params: LoginParams, config?: SWRConfiguration) {
const { data, error, isValidating, isLoading, mutate } =
useSWR<LoginResponse>(
[LOGIN_SWR_KEY, params],
([_, params]) => login(params),
config
);
return {
loading: isLoading,
data,
error,
mutate,
};
}
import { getUserByEmail } from './user';
import type { GetUserByEmailParams, User } from './user';
export const GET_USER_BY_EMAIL_SWR_TOKEN = 'user.getUserByEmail';
export function useGetUserByEmail(
-32
View File
@@ -1,37 +1,5 @@
import { request } from '../request';
export interface ExchangeToken {
type: 'Google';
/**
* Token from firebase.
*/
token: string;
}
export interface RefreshToken {
type: 'Refresh';
token: string;
}
export type LoginParams = ExchangeToken | RefreshToken;
export interface LoginResponse {
/**
* JWT, expires in a very short time
*/
token: string;
/**
* Refresh token
*/
refresh: string;
}
export async function login(params: LoginParams): Promise<LoginResponse> {
return request
.post('/api/user/token', { json: params, headers: { token: undefined } })
.json<LoginResponse>();
}
export interface GetUserByEmailParams {
email: string;
workspaceId: string;
@@ -156,13 +156,6 @@ export async function downloadWorkspace(
export async function uploadBlob(params: { blob: Blob }): Promise<string> {
return request.post('/api/blob', { body: params.blob }).text();
// const data = await request({
// url: '/api/blob',
// method: 'PUT',
// data: params.blob,
// });
// return data.data;
}
export async function getBlob(params: {