-
Notifications
You must be signed in to change notification settings - Fork 27
/
App.tsx
183 lines (170 loc) · 6.3 KB
/
App.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/* eslint-disable class-methods-use-this */
import React from "react";
import { Link } from "react-router-dom";
import { CustomAuth, TorusLoginResponse } from "@toruslabs/customauth";
import ReactJsonView from "react-json-view";
import {
verifierMap,
GITHUB,
TWITTER,
APPLE,
AUTH_DOMAIN,
EMAIL_PASSWORD,
HOSTED_EMAIL_PASSWORDLESS,
HOSTED_SMS_PASSWORDLESS,
LINE,
LINKEDIN,
WEIBO,
COGNITO,
COGNITO_AUTH_DOMAIN,
} from "./constants";
interface IState {
selectedVerifier: string;
torusdirectsdk: CustomAuth | null;
loginHint: string;
loginResponse?: TorusLoginResponse | null;
}
interface IProps {}
class HomePage extends React.PureComponent<IProps, IState> {
constructor(props: IProps) {
super(props);
this.state = {
selectedVerifier: "",
torusdirectsdk: null,
loginHint: "",
loginResponse: null,
};
}
componentDidMount = async () => {
try {
/**
* Important Note:
* After user completes the oauth login process, user will be redirected to redirectPathName
* which you will specify while initializing sdk. If you are using serviceworker or redirect.html
* file provided in this package to handle the redirect result which parses the login result
* and sends the result back to original window (i.e. where login was initiated).
* But User might close original window before completing login process (in popup uxMode)
* or only one window exists during login (in redirect mode), in that case
* service worker will not able to send login result back to original window and it will
* simply redirect to root path of app ('i.e': '/') with the login results in hash params
* and search params.
* So a best practice is to add a fallback handler in the root page of your app.
* Here we are simply parsing hash params and search params to get the login results.
* If you are using redirect uxMode (recommended) , you don't have to include this fallback handler
* in your code.
*/
const url = new URL(window.location.href);
const hash = url.hash.substr(1);
const queryParams = {} as Record<string, any>;
for (const key of url.searchParams.keys()) {
queryParams[key] = url.searchParams.get(key);
}
const { error, instanceParameters } = this.handleRedirectParameters(hash, queryParams);
const torusdirectsdk = new CustomAuth({
baseUrl: `${window.location.origin}/serviceworker`,
enableLogging: true,
network: "testnet", // details for test net
});
await torusdirectsdk.init({ skipSw: false });
this.setState({ torusdirectsdk });
if (hash) {
if (error) throw new Error(error);
const { verifier: returnedVerifier } = instanceParameters as Record<string, any>;
const selectedVerifier = Object.keys(verifierMap).find((x) => verifierMap[x].verifier === returnedVerifier) as string;
this.setState({
selectedVerifier,
});
this._loginWithParams(hash, queryParams);
}
} catch (error) {
console.error(error, "mounted caught");
}
};
_loginWithParams = async (hash: string, queryParameters: Record<string, any>) => {
const { selectedVerifier, torusdirectsdk } = this.state;
console.log(hash, queryParameters);
try {
const jwtParams = this._loginToConnectionMap()[selectedVerifier] || {};
const { typeOfLogin, clientId, verifier } = verifierMap[selectedVerifier];
const loginDetails = await torusdirectsdk?.triggerLogin({
typeOfLogin,
verifier,
clientId,
jwtParams,
hash,
queryParameters,
});
this.setState({ loginResponse: loginDetails });
} catch (error) {
console.error(error, "login caught");
}
};
handleRedirectParameters = (hash: string, queryParameters: Record<string, any>) => {
const hashParameters = hash.split("&").reduce((result: Record<string, any>, item) => {
const [part0, part1] = item.split("=");
result[part0] = part1;
return result;
}, {});
let instanceParameters = {};
let error = "";
if (!queryParameters.preopenInstanceId) {
if (Object.keys(hashParameters).length > 0 && hashParameters.state) {
instanceParameters = JSON.parse(atob(decodeURIComponent(decodeURIComponent(hashParameters.state)))) || {};
error = hashParameters.error_description || hashParameters.error || error;
} else if (Object.keys(queryParameters).length > 0 && queryParameters.state) {
instanceParameters = JSON.parse(atob(decodeURIComponent(decodeURIComponent(queryParameters.state)))) || {};
if (queryParameters.error) error = queryParameters.error;
}
}
return { error, instanceParameters, hashParameters };
};
_loginToConnectionMap = (): Record<string, any> => {
return {
[EMAIL_PASSWORD]: { domain: AUTH_DOMAIN },
[HOSTED_EMAIL_PASSWORDLESS]: {
domain: AUTH_DOMAIN,
verifierIdField: "name",
connection: "",
isVerifierIdCaseSensitive: false,
},
[HOSTED_SMS_PASSWORDLESS]: { domain: AUTH_DOMAIN, verifierIdField: "name", connection: "" },
[APPLE]: { domain: AUTH_DOMAIN },
[GITHUB]: { domain: AUTH_DOMAIN },
[LINKEDIN]: { domain: AUTH_DOMAIN },
[TWITTER]: { domain: AUTH_DOMAIN },
[WEIBO]: { domain: AUTH_DOMAIN },
[LINE]: { domain: AUTH_DOMAIN },
[COGNITO]: { domain: COGNITO_AUTH_DOMAIN, identity_provider: "Google", response_type: "token", user_info_endpoint: "userInfo" },
};
};
render() {
const { loginResponse } = this.state;
return (
<div className="app">
<div
style={{
display: "flex",
flexDirection: "row",
justifyContent: "center",
alignItems: "center",
margin: 100,
}}
>
<Link to="/redirectMode">
<button>Login with Redirect Mode (Recommended)</button>
</Link>
<Link to="/popupMode">
<button>Login with Popup Mode</button>
</Link>
</div>
{loginResponse && (
<div>
<h2>Login Response</h2>
<ReactJsonView src={loginResponse} style={{ marginTop: 20, textAlign: "left" }} />
</div>
)}
</div>
);
}
}
export default HomePage;