feat: gave a try on Gitee OAuth and gave up

This commit is contained in:
EnixCoda 2020-03-29 18:05:22 +08:00
parent 55870b94a1
commit 30a976969f
No known key found for this signature in database
GPG key ID: 0C1A07377913A1DD
4 changed files with 36 additions and 23 deletions

View file

@ -2,11 +2,12 @@ import { Button, TextInput } from '@primer/components'
import { wikiLinks } from 'components/settings/SettingsBar'
import { useConfigs } from 'containers/ConfigsContext'
import { usePlatform } from 'containers/PlatformContext'
import { Gitee } from 'platforms/Gitee'
import * as React from 'react'
import { useStates } from 'utils/hooks/useStates'
import { SettingsSection } from './SettingsSection'
const ACCESS_TOKEN_REGEXP = /^[0-9a-f]{40}$/
const ACCESS_TOKEN_REGEXP = /^[0-9a-f]+$/
type Props = {}
@ -75,15 +76,20 @@ export function AccessTokenSettings(props: React.PropsWithChildren<Props>) {
<Button onClick={() => configContext.set({ access_token: '' })}>Clear</Button>
) : (
<div>
<a
className={'link-button'}
onClick={() => {
// use js here to make sure redirect_uri is latest url
window.location.href = platform.getOAuthLink()
}}
>
Create with OAuth (recommended)
</a>
{platform === Gitee ? (
// disabled for Gitee as it does not support dynamic redirect_uri
<span>OAuth for Gitee is unavailable</span>
) : (
<a
className={'link-button'}
onClick={() => {
// use js here to make sure redirect_uri is latest url
window.location.href = platform.getOAuthLink()
}}
>
Create with OAuth (recommended)
</a>
)}
<div className={'access-token-input-control'}>
<TextInput
backgroundColor="#fff"

View file

@ -1,13 +1,13 @@
export const IN_PRODUCTION_MODE = process.env.NODE_ENV === 'production'
export const GITHUB_OAUTH = {
clientId: process.env.GITHUB_OAUTH_CLIENT_ID,
clientSecret: process.env.GITHUB_OAUTH_CLIENT_SECRET,
clientId: process.env.GITHUB_OAUTH_CLIENT_ID || '',
clientSecret: process.env.GITHUB_OAUTH_CLIENT_SECRET || '',
}
export const GITEE_OAUTH = {
clientId: process.env.GITEE_OAUTH_CLIENT_ID,
clientSecret: process.env.GITEE_OAUTH_CLIENT_SECRET,
clientId: process.env.GITEE_OAUTH_CLIENT_ID || '',
clientSecret: process.env.GITEE_OAUTH_CLIENT_SECRET || '',
}
export const VERSION = process.env.VERSION

View file

@ -157,9 +157,12 @@ export const GitHub: Platform = {
},
useResizeStylesheets,
getOAuthLink() {
return `https://github.com/login/oauth/authorize?client_id=${
GITHUB_OAUTH.clientId
}&scope=repo&redirect_uri=${encodeURIComponent(window.location.href)}`
const params = new URLSearchParams({
client_id: GITHUB_OAUTH.clientId,
scope: 'repo',
redirect_uri: window.location.href,
})
return `https://github.com/login/oauth/authorize?` + params.toString()
},
}

View file

@ -140,6 +140,16 @@ export const Gitee: Platform = {
getCurrentPath(branchName) {
return URLHelper.getCurrentPath(branchName)
},
useResizeStylesheets,
getOAuthLink() {
const params = new URLSearchParams({
client_id: GITEE_OAUTH.clientId,
scope: 'projects',
response_type: 'code',
redirect_uri: window.location.href,
})
return `https://gitee.com/oauth/authorize?` + params.toString()
},
async setOAuth(code) {
const res = await API.OAuth(code)
const { access_token: accessToken, scope, error_description: errorDescription } = res
@ -155,12 +165,6 @@ export const Gitee: Platform = {
}
return accessToken
},
useResizeStylesheets,
getOAuthLink() {
return `https://gitee.com/oauth/authorize?client_id=${
GITEE_OAUTH.clientId
}&scope=repo&response_type=code&redirect_uri=${encodeURIComponent(window.location.href)}`
},
}
export function useGiteeAttachCopySnippetButton(copySnippetButton: boolean) {