feat: 允许通过登录获取 token email 和账号

This commit is contained in:
tiddlygit-test 2021-04-14 00:19:52 +08:00
parent 6d1cb90ab6
commit 983351ab50
8 changed files with 60 additions and 38 deletions

View file

@ -11,7 +11,7 @@
"subwiki's",
"tiddlywiki's"
],
"i18n-ally.sourceLanguage": "en",
"i18n-ally.sourceLanguage": "zh",
"i18n-ally.localesPaths": "localization/locales",
"i18n-ally.namespace": false,
"i18n-ally.review.user.name": "{locale}/{namespaces}.json",

View file

@ -93,7 +93,10 @@
"LocalWorkspace": "Local Workspace",
"LocalWorkspaceDescription": "Only use locally, fully control your own data. TiddlyGit will still create a local git backup system for you, allowing you to go back to the previous versions of tiddlers, but all contents will be lost when the local folder is deleted.",
"SyncedWorkspace": "Synced Workspace",
"SyncedWorkspaceDescription": "To synchronize to an online storage service (such as Github), you need to login to a storage service or enter your login credentials, and have a good network connection. You can sync data across devices, and you still own the data when you use a trusted storage service. And even after the folder is accidentally deleted, you can still download the data from the online service to the local again."
"SyncedWorkspaceDescription": "To synchronize to an online storage service (such as Github), you need to login to a storage service or enter your login credentials, and have a good network connection. You can sync data across devices, and you still own the data when you use a trusted storage service. And even after the folder is accidentally deleted, you can still download the data from the online service to the local again.",
"GitEmailDescription": "Email used for Git submission records, used to count daily submissions on Github and other services",
"GitUserNameDescription": "The account name used to log in to Git, not a nickname",
"LogoutToGetStorageServiceToken": "Log in to the online storage service to obtain credentials"
},
"EditWorkspace": {
"Path": "Wiki Path",

View file

@ -48,6 +48,8 @@
"GitToken": "Git Token",
"GitRepoUrl": "Git仓库线上网址",
"GitTokenDescription": "用于登录Git的凭证一定时间后会过期",
"GitEmailDescription": "用于Git提交记录的Email用于在Github等服务上统计每日提交量",
"GitUserNameDescription": "用于登录Git的账户名注意不是昵称",
"SwitchCreateNewOrOpenExisted": "切换创建新的还是打开现有的WIKI",
"ExistedWikiLocation": "现有的WIKI的位置",
"CreateNewWiki": "创建新WIKI",

View file

@ -9,13 +9,17 @@ import { SupportedStorageServices, IAuthingUserInfo } from '@services/types';
import { useUserInfoObservable } from '@services/auth/hooks';
import { usePromiseValueAndSetter } from '@/helpers/useServiceValue';
import { APP_ID, APP_DOMAIN } from '@/constants/auth';
import { IUserInfos, ServiceTokenTypes } from '@services/auth/interface';
import { ServiceEmailTypes, ServiceTokenTypes, ServiceUserNameTypes } from '@services/auth/interface';
const AuthingLoginButton = styled(Button)`
white-space: nowrap;
width: 100%;
`;
const GitTokenInput = styled(TextField)``;
GitTokenInput.defaultProps = {
fullWidth: true,
variant: 'standard',
};
export function GitTokenForm(props: {
children?: JSX.Element | Array<JSX.Element | undefined | string>;
@ -53,18 +57,19 @@ export function GitTokenForm(props: {
if ('userInfo' in response && response.userInfo?.thirdPartyIdentity?.accessToken !== undefined) {
const accessTokenToSet = response?.userInfo?.thirdPartyIdentity?.accessToken;
const authDataString = response?.userInfo?.oauth;
if (accessTokenToSet !== undefined) {
await window.service.auth.set(`${storageService}-token` as ServiceTokenTypes, accessTokenToSet);
}
// all data we need
if (accessTokenToSet !== undefined && authDataString !== undefined) {
const authData = JSON.parse(authDataString);
// DEBUG: console
console.log(`authData`, authData);
const nextUserInfo: IAuthingUserInfo = {
...response.userInfo,
...authData,
...response.userInfo?.thirdPartyIdentity,
};
void window.service.auth.set(`${storageService}-userName` as ServiceTokenTypes, nextUserInfo.username);
void window.service.auth.set(`${storageService}-token` as ServiceTokenTypes, accessTokenToSet);
void window.service.auth.set(`${storageService}-userName` as ServiceUserNameTypes, nextUserInfo.username);
void window.service.auth.set(`${storageService}-email` as ServiceEmailTypes, nextUserInfo.email);
if (userName === undefined || (userName === '' && nextUserInfo.username !== userName)) {
userNameSetter(nextUserInfo.username);
}
@ -119,12 +124,25 @@ export function GitTokenForm(props: {
<AuthingLoginButton onClick={onClickLogin}>{t('AddWorkspace.LogoutToGetStorageServiceToken')}</AuthingLoginButton>
<GitTokenInput
helperText={t('AddWorkspace.GitTokenDescription')}
fullWidth
onChange={(event) => {
void window.service.auth.set(`${storageService}-token` as ServiceTokenTypes, event.target.value);
}}
value={userInfo[`${storageService}-token` as ServiceTokenTypes] ?? ''}
/>
<GitTokenInput
helperText={t('AddWorkspace.GitUserNameDescription')}
onChange={(event) => {
void window.service.auth.set(`${storageService}-userName` as ServiceUserNameTypes, event.target.value);
}}
value={userInfo[`${storageService}-userName` as ServiceUserNameTypes] ?? ''}
/>
<GitTokenInput
helperText={t('AddWorkspace.GitEmailDescription')}
onChange={(event) => {
void window.service.auth.set(`${storageService}-email` as ServiceEmailTypes, event.target.value);
}}
value={userInfo[`${storageService}-email` as ServiceEmailTypes] ?? ''}
/>
{children}
</>
);

View file

@ -1,11 +1,10 @@
import React, { useState } from 'react';
import styled, { keyframes } from 'styled-components';
import { Tab as TabRaw, ListItemText } from '@material-ui/core';
import { TabPanel, TabContext, TabList } from '@material-ui/lab';
import { TabPanel as TabPanelRaw, TabContext, TabList as TabListRaw } from '@material-ui/lab';
import { SupportedStorageServices } from '@services/types';
import { useTranslation } from 'react-i18next';
import GitHubLogin from './AuthingLoginButton';
import { GitTokenForm } from './GitTokenForm';
const Container = styled.div`
@ -13,9 +12,16 @@ const Container = styled.div`
display: flex;
flex-direction: column;
`;
const TabPanel = styled(TabPanelRaw)`
padding: 5px 0 !important;
`;
const TabList = styled(TabListRaw)``;
const TabsContainer = styled.div`
display: flex;
flex-direction: row;
& ${TabList} {
min-width: 160px;
}
`;
const backgroundColorShift = keyframes`
from {background-color: #fdfdfd;}

View file

@ -19,8 +19,6 @@ import { useValidateNewWiki } from './useNewWiki';
export function NewWikiForm({ form, isCreateMainWorkspace }: IWikiWorkspaceFormProps & { isCreateMainWorkspace: boolean }): JSX.Element {
const { t } = useTranslation();
const [errorInWhichComponent] = useValidateNewWiki(isCreateMainWorkspace, form);
// DEBUG: console
console.log(`form.mainWikiToLink`, form.mainWikiToLink);
return (
<CreateContainer elevation={2} square>
<LocationPickerContainer>

View file

@ -5,7 +5,7 @@ import { IAuthingUserInfo, SupportedStorageServices } from '@services/types';
import { lazyInject } from '@services/container';
import type { IWindowService } from '@services/windows/interface';
import serviceIdentifier from '@services/serviceIdentifier';
import { IAuthenticationService, IUserInfos } from './interface';
import { IAuthenticationService, IUserInfos, ServiceEmailTypes, ServiceTokenTypes, ServiceUserNameTypes } from './interface';
import { BehaviorSubject } from 'rxjs';
import { IGitUserInfos } from '@services/git/interface';
@ -32,24 +32,15 @@ export class Authentication implements IAuthenticationService {
}
public getStorageServiceUserInfo(serviceName: SupportedStorageServices): IGitUserInfos | undefined {
switch (serviceName) {
case SupportedStorageServices.github: {
const gitUserName = this.get('github-userName');
const email = this.get('email');
const accessToken = this.get('github-token');
if (gitUserName !== undefined && email !== undefined && accessToken !== undefined) {
return {
gitUserName,
email,
accessToken,
};
}
break;
}
default:
break;
const gitUserName = this.get(`${serviceName}-userName` as ServiceUserNameTypes);
const email = this.get(`${serviceName}-email` as ServiceEmailTypes);
const accessToken = this.get(`${serviceName}-token` as ServiceTokenTypes);
if (gitUserName !== undefined && email !== undefined && accessToken !== undefined) {
return {
gitUserName,
email,
accessToken,
};
}
}

View file

@ -6,19 +6,23 @@ import { IGitUserInfos } from '@services/git/interface';
import { SupportedStorageServices } from '@services/types';
export type ServiceTokenTypes = `${SupportedStorageServices}-token`;
/** Github Login: token */
/** Git Login: token */
type TokenRecord = Record<ServiceTokenTypes, string>;
export type ServiceUserNameTypes = `${SupportedStorageServices}-token`;
/** Github Login: username , this is also used to filter user's repo when searching repo */
export type ServiceUserNameTypes = `${SupportedStorageServices}-userName`;
/** Git Login: username , this is also used to filter user's repo when searching repo */
type UserNameRecord = Record<ServiceUserNameTypes, string>;
export type ServiceEmailTypes = `${SupportedStorageServices}-email`;
/** Git push: Git commit message email, you may use different email for different storage service */
type EmailRecord = Record<ServiceEmailTypes, string>;
export type IUserInfos = {
/** UserName in TiddlyWiki */
userName: string;
/** Git commit message email */
email: string;
'github-userName'?: string;
} & Partial<TokenRecord> &
Partial<UserNameRecord>;
Partial<UserNameRecord> &
Partial<EmailRecord>;
/**
* Handle login to Github GitLab Coding.net