-
Notifications
You must be signed in to change notification settings - Fork 2
/
AppMain.tsx
157 lines (150 loc) · 5.07 KB
/
AppMain.tsx
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import { NavigationProp } from "@react-navigation/native"
import { Auth } from "aws-amplify"
import { Authenticator } from "aws-amplify-react-native"
import * as Linking from "expo-linking"
import * as React from "react"
import { View } from "react-native"
import { SafeAreaProvider } from "react-native-safe-area-context"
import { AuthStateData, JCCognitoUser } from "src/types"
import NeedHelpButton from "./components/FloatingButton/NeedHelpButton"
import JCComponent, { JCState } from "./components/JCComponent/JCComponent"
import { SidebarMobile } from "./components/Sidebar/SidebarMobile"
import HomeScreen from "./screens/HomeScreen/index"
import * as RootNavigation from "./screens/HomeScreen/NavigationRoot"
// UserAgent.getUserAgent() //synchronous
interface Props {
navigation: NavigationProp<any, any>
onStateChange(state: string, data: AuthStateData): any
}
interface State extends JCState {
isLoggedIn: boolean
fontLoaded: boolean
authState: any
joinedAs: "individual" | "organization"
username: any
}
const federated = {
google_client_id: "",
facebook_app_id: "579712102531269",
amazon_client_id: "",
}
export class AppMain extends JCComponent<Props, State> {
constructor(props: Props) {
super(props)
this.state = {
...super.getInitialState(),
fontLoaded: false,
isLoggedIn: false,
authState: "",
username: "",
}
// this.ionViewCanEnter();
}
async UNSAFE_componentWillMount(): Promise<void> {
this.setState({ authState: await this.getAuthInitialState() })
// console.log("test")
}
async updateState(state: string, data: AuthStateData): Promise<void> {
console.log("updateState")
this.setState({ authState: state })
RootNavigation.getRoot()?.params as {
joinedAs: "individual" | "organization" | null
brand: "jc" | "oneStory" | null
}
if (state == "signUp") RootNavigation.navigate("signup", { joinedAs: null, brand: data?.brand })
else if (state == "signIn") {
RootNavigation.navigate("auth", {
screen: "signin",
brand: data?.brand,
email: data?.email,
fromVerified: data?.fromVerified,
})
} else if (state == "forgotPassword")
RootNavigation.navigate("forgotpassword", {
brand: RootNavigation.getRoot()?.params?.brand,
})
else if (state == "requireNewPassword")
RootNavigation.navigate("requirenewpassword", { brand: data?.brand })
else if (state == "verifyContact")
RootNavigation.navigate("verifycontact", { brand: data?.brand })
else if (state == "confirmSignIn")
RootNavigation.navigate("confirmsignin", { brand: data?.brand })
else if (state == "confirmSignUp")
RootNavigation.navigate("confirmsignup", { email: data?.email, brand: data?.brand })
else if (state == "signedIn") {
const user = (await Auth.currentAuthenticatedUser()) as JCCognitoUser
if (
user.getSignInUserSession()?.getAccessToken().payload["cognito:groups"].includes("admin") ||
user
.getSignInUserSession()
?.getAccessToken()
.payload["cognito:groups"].includes("subscriptionValid") ||
user
.getSignInUserSession()
?.getAccessToken()
.payload["cognito:groups"].includes("legacyUserGroup1")
) {
RootNavigation.navigate("auth", {
screen: "Payment3",
params: {
joinedProduct: data?.joinedProduct,
brand: data?.brand,
},
})
} else {
RootNavigation.navigate("auth", {
screen: "Payment1",
// params: {
joinedProduct: data?.joinedProduct,
brand: data?.brand,
//},
})
}
}
}
async getAuthInitialState(): Promise<string> {
const initialUrl = await Linking.getInitialURL()
console.log({ "INITIAL URL": initialUrl })
if (initialUrl?.toLowerCase().includes("/auth/signup")) return "signUp"
return "signIn"
}
render(): React.ReactNode {
if (this.state.authState != "") {
return (
<SafeAreaProvider>
<View
style={{
width: "100%",
top: 0,
left: 0,
height: "100%",
}}
>
<NeedHelpButton></NeedHelpButton>
<SidebarMobile authState={this.state.authState} />
<Authenticator
onStateChange={async (item1: string, data: any) => {
console.log("AUTHENTICATOR STATE CHANGE")
await this.updateState(item1, data)
}}
hideDefault={true}
authState={this.state.authState}
federated={federated}
usernameAttributes="email"
>
<HomeScreen
onStateChange={async (item1, data) => {
console.log("AUTHENTICATOR STATE CHANGE")
await this.updateState(item1, data)
}}
authState={this.state.authState}
/>
</Authenticator>
</View>
</SafeAreaProvider>
)
} else {
return null
}
}
}