Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(): login #4

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .env.production
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
VITE_APP_ID=123
VITE_APP_SECRET=foobar
98 changes: 47 additions & 51 deletions src/store/userStore.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ import React, {
} from 'react';
import {emitEvent} from '../utils/eventEmit';
import {defaultThemes, themeConfigList} from '../utils/loadVegaResource';
import request, {getServerUrl} from '../utils/request';
import {
requestV1, AccessTokenStorageKey, RefreshTokenStorageKey, getServerUrl,
} from '../utils/request';
import {requestAnotherWindow} from '../utils/requestAnotherWindow';

export interface IUserInfo {
userName: string;
Expand Down Expand Up @@ -132,11 +135,11 @@ export default class UserStore {
return;
}
const url = getServerUrl('/api/ce/personal');
const result = await request.get<never, IUserInfo>(url);
const result = requestV1.unwrap(await requestV1.get<void, IUserInfo>(url));
if (result !== null) {
const wspUrl = getServerUrl('/api/ce/simpleInfo/workspace');
// eslint-disable-next-line max-len
const wsp = await request.get<{userName: string}, {name: string}>(wspUrl, {userName: result.userName});
const wsp = requestV1.unwrap(await requestV1.get<{userName: string}, {name: string}>(wspUrl, {userName: result.userName}));
runInAction(() => {
this.user = {
...result,
Expand Down Expand Up @@ -166,9 +169,11 @@ export default class UserStore {
}
const url = getServerUrl('/api/ce/theme/list');
// eslint-disable-next-line no-spaced-func, func-call-spacing
const result = await request.get<{ workspaceName: string }, { list: IThemeOnCloud[] }>(url, {
workspaceName,
});
const result = requestV1.unwrap(
await requestV1.get<{ workspaceName: string }, { list: IThemeOnCloud[] }>(url, {
workspaceName,
}),
);
if (result !== null) {
runInAction(() => {
this.themes = result.list.map(thm => {
Expand All @@ -194,8 +199,10 @@ export default class UserStore {
this.loginStatus = 'pending';
});
try {
const url = getServerUrl('/api/loginStatus');
const res = await request.get<never, { loginStatus: boolean; userName: string }>(url);
const url = getServerUrl('/api/auth/v1/loginStatus');
const res = requestV1.unwrap(
await requestV1.get<void, { loginStatus: boolean; userName: string }>(url),
);
runInAction(() => {
this.loginStatus = res.loginStatus ? 'loggedIn' : 'loggedOut';
});
Expand All @@ -204,45 +211,32 @@ export default class UserStore {
runInAction(() => {
this.loginStatus = 'loggedOut';
});
return null;
return false;
}
}

public async signUp(): Promise<void> {
runInAction(() => {
this.loginStatus = 'pending';
});
const url = `${KanariesPath}/access?redirect_path=${encodeURIComponent(window.location.toString())}&redirect_close=1`;

const directed = await new Promise<boolean>(resolve => {
// Most browsers block popups if they are called
// outside of user-triggered event handlers like onclick.
const button = document.createElement('button');
button.onclick = () => {
const officialSite = window.open(url);
if (officialSite) {
const beginTime = Date.now();
const cb = (): void => {
if (officialSite.closed) {
resolve(true);
return;
}
if (Date.now() - beginTime > 1_000 * 60 * 10) {
resolve(false);
return;
}
setTimeout(cb, 200);
};
setTimeout(cb, 200);
} else {
resolve(false);
try {
await requestAnotherWindow(`${KanariesPath}/home/access?agentType=asp&clientId=${encodeURIComponent(import.meta.env.VITE_APP_ID)
}&clientSecret=${
encodeURIComponent(import.meta.env.VITE_APP_SECRET)
}&scopes=&state=${
encodeURIComponent(JSON.stringify({
redirect_path: window.location.href,
}))
}`, (tab, ev) => {
const {accessToken, refreshToken} = ev.data;
if (accessToken) {
localStorage.setItem(AccessTokenStorageKey, accessToken);
localStorage.setItem(RefreshTokenStorageKey, refreshToken);
tab.close();
}
};
button.click();
});
if (directed) {
});
await this.updateAuthStatus();
} else {
} catch (error) {
runInAction(() => {
this.loginStatus = 'loggedOut';
});
Expand All @@ -258,18 +252,20 @@ export default class UserStore {
try {
const config = JSON.parse(configRaw);
const url = getServerUrl('/api/ce/theme');
// eslint-disable-next-line no-spaced-func, func-call-spacing, max-len
const result = await request.post<{id?: string, workspaceName: string; name: string; config: object; cover: {name: string; size: number; type: string}}, IThemeOnCloud>(url, {
id,
workspaceName,
name,
config,
cover: {
type: cover.type,
name: cover.name,
size: cover.size,
},
});
const result = requestV1.unwrap(
// eslint-disable-next-line no-spaced-func, func-call-spacing, max-len
await requestV1.post<{id?: string, workspaceName: string; name: string; config: object; cover: {name: string; size: number; type: string}}, IThemeOnCloud>(url, {
id,
workspaceName,
name,
config,
cover: {
type: cover.type,
name: cover.name,
size: cover.size,
},
}),
);
const fileUploadRes = await fetch(result.cover.uploadUrl, {
method: 'PUT',
body: cover,
Expand All @@ -279,7 +275,7 @@ export default class UserStore {
}
const reportUploadSuccessApiUrl = getServerUrl('/api/ce/upload/callback');
// eslint-disable-next-line function-paren-newline
await request.get<{ storageId: string; status: 1 }, { downloadUrl: string }>(
await requestV1.get<{ storageId: string; status: 1 }, { downloadUrl: string }>(
reportUploadSuccessApiUrl, {storageId: result.cover.storageId, status: 1},
// eslint-disable-next-line function-paren-newline
);
Expand Down
Loading