import React from 'react'; import Typography from 'antd/lib/typography'; import { Info } from '../common/Info'; import Form, { FormComponentProps } from 'antd/lib/form'; import Input from 'antd/lib/input'; import Button from 'antd/lib/button'; import message from 'antd/lib/message'; import Icon from 'antd/lib/icon'; import Cookies from 'universal-cookie'; import encUtf8 from 'crypto-js/enc-utf8'; import AES from 'crypto-js/aes'; import { GitHubSave } from './GitHubSave'; import { MAX_AGE } from './githubCookie'; import { useReferer } from './useGitHub'; const handleSubmit = ( validateFields: any, redirect: () => void, ) => (event: React.FormEvent) => { event.preventDefault(); validateFields((err: any, values: any) => { if (!err) { // save const cookies = new Cookies(); cookies.set('github', values, { path: '/', maxAge: MAX_AGE * 60 }); cookies.set('githubUser', values.user, { path: '/' }); redirect(); } }); } const handleBlur = ( setShowSave: React.Dispatch>, ) => ({ target: { value } }: React.FocusEvent) => { if (value.length) { const cookies = new Cookies(); if (cookies.get('githubNoSave') !== 'true' && !cookies.get('githubToken')) { setShowSave(value); } } } const handleUnlock = ( setToken: React.Dispatch>, ) => () => { const cookies = new Cookies(); const encToken = cookies.get('githubToken'); if (encToken) { const pass = prompt('Please enter password to unlock token'); if (pass?.length) { const bytes = AES.decrypt(cookies.get('githubToken'), pass); if (bytes) { const token = bytes.toString(encUtf8); if (token.length) { setToken(token); return; } } message.error('Invalid password'); } } } const GitHubAuthForm = ({ form: { getFieldDecorator, validateFields } }: FormComponentProps) => { const redirect = useReferer(); const [showSave, setShowSave] = React.useState(''); const [token, setToken] = React.useState(''); const cookies = new Cookies(); const saveTokenAvailable = cookies.get('githubNoSave') !== 'true' && !cookies.get('githubToken'); return ( <> GitHub To save data in your GitHub repository, we need to provide a personal access tokens to the GitHub API. To create a token, go in developer settings, personal access tokens and then generate new token. In most of the case you will only need to give permission for public_repo.
{getFieldDecorator('user', { initialValue: cookies.get('githubUser'), rules: [{ required: true, message: 'Please provide your GitHub username.' }], })( )} {getFieldDecorator('token', { initialValue: token, rules: [{ required: true, message: 'Please provide your personal access tokens.' }], })( )}
); } export const GitHubAuth = Form.create({ name: 'github_auth' })(GitHubAuthForm);