-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
96 lines (91 loc) ยท 2.98 KB
/
index.js
File metadata and controls
96 lines (91 loc) ยท 2.98 KB
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import React from "react";
import ReactDOM from "react-dom";
import { Provider } from "react-redux";
import App from "./App";
import { customHistory as history } from "./router/AppRouter";
import Amplify, { Auth, Hub } from "aws-amplify";
import * as serviceWorker from "./serviceWorker";
import "./styles/custom.scss";
import "bootstrap/dist/js/bootstrap.bundle";
import awsconfig from "./aws-exports";
import configureStore from "./store/configureStore";
import { signInCognitoUser } from "./actions";
const store = configureStore();
ReactDOM.render(
<React.StrictMode>
<Provider store={store}>
<App />
</Provider>
</React.StrictMode>,
document.getElementById("root")
);
const handleSignin = async (userInfo) => {
try {
if (!userInfo) {
userInfo = await Auth.currentAuthenticatedUser();
}
console.log("๐ Dispatching user data to Redux...");
store.dispatch(signInCognitoUser(userInfo));
} catch (error) {
console.warn(error);
}
};
Hub.listen("auth", async (data) => {
console.log("๐๏ธ HUB EVENT/PAYLOAD", data.payload.event, data.payload);
switch (data.payload.event) {
case "configured":
try {
if (history.location.pathname.includes("auth-callback")) {
console.log("๐ AuthCallback detected!");
} else {
const userInfo = await Auth.currentAuthenticatedUser();
console.log("โ๏ธ User is already signed in", userInfo);
if (history.location.pathname === "/") {
console.log("๐ฆ User coming from sign in. Pushing to loading...");
history.push("/loading");
}
handleSignin(userInfo);
}
} catch (error) {
if (error === "not authenticated") {
console.log("๐ฎ User is not signed in");
} else {
console.warn("๐ฃ Error in Hub:", error);
}
// Auth.currentAuthenticatedUser() throws an error if not signed in
// We don't push from here as the redirect will be handled by protected routing
}
break;
case "signIn":
handleSignin();
break;
case "signIn_failure":
store.dispatch({
type: "CONGNITO_USER_SIGNIN_FAIL",
payload: new Error(
data.payload.data.message.split("PreSignUp failed with error")[1]
)
});
break;
case "signUp_failure":
const errorMessage = data.payload.data.message.includes(
"PreSignUp failed with error"
)
? data.payload.data.message
.split("PreSignUp failed with error")[1]
.slice(1)
: data.payload.data.message;
store.dispatch({
type: "COGNITO_USER_SIGNUP_FAIL",
payload: new Error(errorMessage)
});
break;
default:
break;
}
});
Amplify.configure(awsconfig);
// If you want your app to work offline and load faster, you can change
// unregister() to register() below. Note this comes with some pitfalls.
// Learn more about service workers: https://bit.ly/CRA-PWA
serviceWorker.unregister();