-
Notifications
You must be signed in to change notification settings - Fork 27
/
login.tsx
152 lines (140 loc) · 4.49 KB
/
login.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
import React from "react";
import { CustomAuth, TorusLoginResponse } from "@toruslabs/customauth";
import dynamic from "next/dynamic";
import {
verifierMap,
GOOGLE,
GITHUB,
TWITTER,
APPLE,
AUTH_DOMAIN,
EMAIL_PASSWORD,
HOSTED_EMAIL_PASSWORDLESS,
HOSTED_SMS_PASSWORDLESS,
LINE,
LINKEDIN,
WEIBO,
COGNITO_AUTH_DOMAIN,
COGNITO,
REDDIT,
} from "../../lib/constants";
let ReactJsonView;
if (typeof window === "object") {
ReactJsonView = dynamic(() => import("react-json-view"));
}
interface IState {
selectedVerifier: string;
torusdirectsdk: CustomAuth | null;
loginHint: string;
loginDetails?: TorusLoginResponse | null;
}
interface IProps {}
class MyApp extends React.Component<IProps, IState> {
constructor(props) {
super(props);
this.state = {
selectedVerifier: GOOGLE,
torusdirectsdk: null as CustomAuth | null,
loginHint: "",
loginDetails: null,
};
}
componentDidMount = async () => {
try {
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 });
} catch (error) {
console.error(error, "mounted caught");
}
};
login = async (e) => {
e.preventDefault();
const { selectedVerifier, torusdirectsdk } = this.state;
try {
const jwtParams = this._loginToConnectionMap()[selectedVerifier] || {};
const { typeOfLogin, clientId, verifier } = verifierMap[selectedVerifier];
const loginDetails = await torusdirectsdk.triggerLogin({
typeOfLogin,
verifier,
clientId,
jwtParams,
});
console.log(loginDetails);
this.setState({
loginDetails,
});
} catch (error) {
console.error(error, "login caught");
}
};
_loginToConnectionMap = () => {
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" },
[REDDIT]: { domain: AUTH_DOMAIN, connection: "Reddit", verifierIdField: "name", isVerifierIdCaseSensitive: false },
};
};
render() {
const { selectedVerifier, loginDetails } = this.state;
return (
<div className="App">
<form onSubmit={this.login}>
<div>
<span style={{ marginRight: "10px" }}>Verifier:</span>
<select value={selectedVerifier} onChange={(e) => this.setState({ selectedVerifier: e.target.value })}>
{Object.keys(verifierMap).map((login) => (
<option value={login} key={login.toString()}>
{verifierMap[login].name}
</option>
))}
</select>
</div>
<div style={{ marginTop: "20px" }}>
<button>Login with Torus</button>
</div>
</form>
<div id="app">
<p>
Please note that the verifiers listed in the example have http://localhost:3000/serviceworker/redirect configured as the redirect uri.
</p>
<p>If you use any other domains, they won't work.</p>
<p>The verifiers listed here only work with the client id's specified in example. Please don't edit them</p>
<p>The verifiers listed here are for example reference only. Please don't use them for anything other than testing purposes.</p>
<div>
Reach out to us at <a href="mailto:hello@tor.us">hello@tor.us</a> or <a href="https://t.me/torusdev">telegram group</a> to get your
verifier deployed for your client id.
</div>
</div>
{loginDetails && (
<div>
<h2>Login Response</h2>
<ReactJsonView src={loginDetails} style={{ marginTop: 20, textAlign: "left" }} />
</div>
)}
</div>
);
}
}
export default MyApp;