Skip to content

Commit 46bf708

Browse files
committed
move authentication component to utils
1 parent c2d585b commit 46bf708

File tree

4 files changed

+57
-61
lines changed

4 files changed

+57
-61
lines changed

packages/gatsby-theme/src/components/Account.tsx

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@ import React from 'react';
22
import { Query } from 'react-apollo';
33
import { ViewerQuery } from '../graphqlTypes';
44
import gql from 'graphql-tag';
5-
import GithubAuth from './GithubAuth';
65
import { Text, Image, Flex } from './shared/base';
76
import { Link } from 'gatsby';
87
import { CenteredLoader } from './Loader';
8+
import CustomButton from './CustomButton';
9+
import { loginUser } from '../utils/auth';
910

1011
const Account = () => {
1112
return (
@@ -30,7 +31,11 @@ const Account = () => {
3031
if (data.viewer && data.viewer.user) {
3132
return <Profile user={data.viewer.user} />;
3233
}
33-
return <GithubAuth> Sign up </GithubAuth>;
34+
return (
35+
<CustomButton onClick={() => loginUser()} type="github">
36+
Sign up
37+
</CustomButton>
38+
);
3439
}}
3540
</Query>
3641
);

packages/gatsby-theme/src/components/GithubAuth.tsx

Lines changed: 0 additions & 55 deletions
This file was deleted.

packages/gatsby-theme/src/pages/signup.tsx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import * as React from 'react';
22
import Layout from '../components/layout';
3-
import GithubAuth from '../components/GithubAuth';
43
import { Query } from 'react-apollo';
54
import gql from 'graphql-tag';
65
import { ViewerQuery } from '../graphqlTypes';
76
import { navigate } from 'gatsby';
87
import { CenteredLoader } from '../components/Loader';
98
import { Flex, Text, Box } from '../components/shared/base';
9+
import CustomButton from '../components/CustomButton';
10+
import { loginUser } from '../utils/auth';
1011

1112
const Signup = () => (
1213
<Query<ViewerQuery>
@@ -47,7 +48,9 @@ const SignupPage = () => {
4748
Lorem ipsum dolor sit amet.
4849
</Text>
4950
<Box m={4}>
50-
<GithubAuth> Sign in with Github </GithubAuth>
51+
<CustomButton onClick={() => loginUser()} type="github">
52+
Sign in with Github
53+
</CustomButton>
5154
</Box>
5255
</Flex>
5356
</Layout>

packages/gatsby-theme/src/utils/auth.ts

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import gql from 'graphql-tag';
22
import { client } from '../Apollo';
3+
import config from '../../config';
34

45
const TOKEN_KEY = 'token';
56

@@ -10,6 +11,7 @@ const AUTHENTICATE_USER_MUTATION = gql`
1011
}
1112
}
1213
`;
14+
1315
export const authenticateUser = async (code: string) => {
1416
const res = await client.mutate({
1517
mutation: AUTHENTICATE_USER_MUTATION,
@@ -21,11 +23,52 @@ export const authenticateUser = async (code: string) => {
2123
client.resetStore();
2224
};
2325

24-
export const getAuthToken = () => {
25-
return localStorage.getItem('token');
26+
export const loginUser = async () => {
27+
const popup = openPopup();
28+
const code = await listen();
29+
try {
30+
await authenticateUser(code);
31+
} catch (e) {
32+
console.error(e);
33+
}
34+
popup.close();
2635
};
2736

2837
export const logoutUser = () => {
2938
localStorage.removeItem(TOKEN_KEY);
3039
client.resetStore();
3140
};
41+
42+
export const getAuthToken = () => {
43+
return localStorage.getItem('token');
44+
};
45+
46+
const listen = async () => {
47+
return new Promise(resolve => {
48+
window.addEventListener('message', receiveMessage, false);
49+
function receiveMessage(event: any) {
50+
if (event.data) {
51+
window.removeEventListener('message', receiveMessage);
52+
resolve(event.data);
53+
}
54+
}
55+
});
56+
};
57+
58+
const openPopup = () => {
59+
const width = 600,
60+
height = 600;
61+
const left = window.innerWidth / 2 - width / 2;
62+
const top = window.innerHeight / 2 - height / 2;
63+
const url = `https://github.com/login/oauth/authorize?scope=user:email&client_id=${
64+
config.githubClientId
65+
}`;
66+
67+
return window.open(
68+
url,
69+
'',
70+
`toolbar=no, location=no, directories=no, status=no, menubar=no,
71+
scrollbars=no, resizable=no, copyhistory=no, width=${width},
72+
height=${height}, top=${top}, left=${left}`,
73+
);
74+
};

0 commit comments

Comments
 (0)