-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Auth.js
55 lines (45 loc) · 1.42 KB
/
Auth.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
// Libraries
import React from "react";
import TwitterLogin from "react-twitter-login";
import { withRouter } from "react-router-dom";
import { API, graphqlOperation } from "aws-amplify";
import { listUsers } from "./graphql/queries";
import { createUser } from "./graphql/mutations";
const Auth = ({ history }) => {
const authHandler = async (error, data) => {
if (error) {
alert("Please log into Twitter!");
return;
}
console.log(data);
// Save data to local storage
localStorage.setItem("user_id", data.user_id);
localStorage.setItem("user_name", data.screen_name);
// Create a user and add to db
const user = {
twitterId: data.user_id,
handle: data.screen_name,
};
try {
const users = await API.graphql(graphqlOperation(listUsers))
const u = users.data.listUsers.items.filter((x) => { return x.screen_name === user.screen_name })
if (!u) {
await API.graphql(graphqlOperation(createUser, { input: user }))
};
} catch (err) {
console.error(err);
}
window.close();
// Send them to the main donations page
history.push("/action");
};
return (
<TwitterLogin
authCallback={authHandler}
consumerKey={process.env.REACT_APP_CONSUMER_KEY}
consumerSecret={process.env.REACT_APP_CONSUMER_SECRET}
callbackUrl={process.env.REACT_APP_CALLBACK_URL}
/>
);
};
export default withRouter(Auth);