Merge pull request #1078 from spotify/remove-github-login

Auth Frontend: remove github login page
This commit is contained in:
Raghunandan Balachandran
2020-05-30 20:20:04 +02:00
committed by GitHub
4 changed files with 2 additions and 204 deletions
+2 -8
View File
@@ -14,14 +14,9 @@
* limitations under the License.
*/
import {
createApp,
AlertDisplay,
OAuthRequestDialog,
LoginPage,
} from '@backstage/core';
import { createApp, AlertDisplay, OAuthRequestDialog } from '@backstage/core';
import React, { FC } from 'react';
import { BrowserRouter as Router, Route } from 'react-router-dom';
import { BrowserRouter as Router } from 'react-router-dom';
import Root from './components/Root';
import * as plugins from './plugins';
import apis from './apis';
@@ -40,7 +35,6 @@ const App: FC<{}> = () => (
<OAuthRequestDialog />
<Router>
<Root>
<Route key="login" path="/login" component={LoginPage} exact />
<AppComponent />
</Root>
</Router>
@@ -1,178 +0,0 @@
/*
* Copyright 2020 Spotify AB
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import React, { FC, useState } from 'react';
import GitHubIcon from '@material-ui/icons/GitHub';
import { Page } from '../Page';
import { Header } from '../Header';
import { Content } from '../Content';
import { ContentHeader } from '../ContentHeader';
import { InfoCard } from '../InfoCard/InfoCard';
import {
Grid,
Typography,
Button,
TextField,
List,
ListItem,
Link,
} from '@material-ui/core';
enum AuthType {
GitHub,
}
export const LoginPage: FC<{}> = () => {
const [githubUsername, setGithubUsername] = useState(String);
const [githubPersonalAuthToken, setGithubPersonalAuthToken] = useState(
String,
);
const [loginDetails, setLoginDetails] = useState(Object);
const saveGithubInfo = (info: {}) => {
localStorage.setItem('githubLoginDetails', JSON.stringify(info));
setLoginDetails(info);
};
const deleteGithubInfo = () => {
localStorage.removeItem('githubLoginDetails');
setLoginDetails(undefined);
};
const handleTokenRegistration = (event: any) => {
switch (event.target.name) {
case 'github-username-tf':
setGithubUsername(event.target.value);
break;
case 'github-auth-tf':
setGithubPersonalAuthToken(event.target.value);
break;
default:
break;
}
};
const fetchGitHubToken = (username: String, token: String) => {
fetch('https://api.github.com/user', {
headers: new Headers({
Authorization: `Basic ${btoa(`${username}:${token}`)}`,
'Content-Type': 'application/x-www-form-urlencoded',
}),
})
.then(response => {
if (response.status === 200) return response.json();
throw Error(`${response.status} ${response.statusText}`);
})
.then(data => {
const info = {
username: username,
token: token,
name: data.name || data.login,
};
saveGithubInfo(info);
})
.catch(() => {});
};
const validateUsernameAndToken = (username: String, token: String) => {
if (username === undefined || username === null || username === '')
return false;
if (token === undefined || token === null || token === '') return false;
return true;
};
const authenticate = (type: AuthType) => {
switch (type) {
case AuthType.GitHub:
{
const username = githubUsername;
const token = githubPersonalAuthToken;
if (validateUsernameAndToken(username, token))
fetchGitHubToken(username, token);
}
break;
default:
break;
}
};
const LoginIndicator = () => {
const ls = localStorage.getItem('githubLoginDetails');
if (ls !== null) {
const obj = ls || loginDetails ? JSON.parse(ls) : loginDetails;
return (
<Typography variant="h6" component="h2">
{`Welcome, ${obj.name}!`}
<br />
<Link onClick={deleteGithubInfo}>Logout</Link>
</Typography>
);
}
return (
<Typography variant="h6" component="h2">
Welcome, guest!
</Typography>
);
};
return (
<Page>
<Header title="Login">
<LoginIndicator />
</Header>
<Content>
<ContentHeader title="Choose a method to authenticate" />
<Grid container>
<Grid item>
<InfoCard>
<Typography variant="h6">
<GitHubIcon /> GitHub
</Typography>
<List>
<ListItem>
<TextField
name="github-username-tf"
label="Username"
onChange={handleTokenRegistration}
/>
</ListItem>
<ListItem>
<TextField
name="github-auth-tf"
label="Token"
onChange={handleTokenRegistration}
/>
</ListItem>
<ListItem>
<Button
data-testid="github-auth-button"
variant="outlined"
color="primary"
onClick={() => authenticate(AuthType.GitHub)}
>
Authenticate
</Button>
</ListItem>
</List>
</InfoCard>
</Grid>
</Grid>
</Content>
</Page>
);
};
@@ -1,17 +0,0 @@
/*
* Copyright 2020 Spotify AB
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
export { LoginPage } from './LoginPage';
-1
View File
@@ -21,7 +21,6 @@ export * from './Header';
export * from './HeaderLabel';
export * from './HomepageTimer';
export * from './InfoCard';
export * from './LoginPage';
export * from './Page';
export * from './Sidebar';
export * from './TabbedCard';