2022-05-04 16:09:48 +00:00
|
|
|
/*
|
|
|
|
Copyright 2022 Matrix.org Foundation C.I.C.
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2022-01-06 00:47:53 +00:00
|
|
|
import matrix, { InteractiveAuth } from "matrix-js-sdk/src/browser-index";
|
|
|
|
import { useState, useEffect, useCallback, useRef } from "react";
|
2022-01-06 01:19:03 +00:00
|
|
|
import { initClient, defaultHomeserver } from "../matrix-utils";
|
2022-01-06 00:47:53 +00:00
|
|
|
|
|
|
|
export function useInteractiveRegistration() {
|
|
|
|
const [state, setState] = useState({ privacyPolicyUrl: "#", loading: false });
|
|
|
|
|
|
|
|
const authClientRef = useRef();
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
authClientRef.current = matrix.createClient(defaultHomeserver);
|
|
|
|
|
|
|
|
authClientRef.current.registerRequest({}).catch((error) => {
|
|
|
|
const privacyPolicyUrl =
|
|
|
|
error.data?.params["m.login.terms"]?.policies?.privacy_policy?.en?.url;
|
|
|
|
|
|
|
|
const recaptchaKey = error.data?.params["m.login.recaptcha"]?.public_key;
|
|
|
|
|
|
|
|
if (privacyPolicyUrl || recaptchaKey) {
|
|
|
|
setState((prev) => ({ ...prev, privacyPolicyUrl, recaptchaKey }));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
const register = useCallback(
|
2022-02-15 20:46:58 +00:00
|
|
|
async (
|
|
|
|
username,
|
|
|
|
password,
|
|
|
|
displayName,
|
|
|
|
recaptchaResponse,
|
|
|
|
passwordlessUser
|
|
|
|
) => {
|
2022-01-06 00:47:53 +00:00
|
|
|
const interactiveAuth = new InteractiveAuth({
|
|
|
|
matrixClient: authClientRef.current,
|
|
|
|
busyChanged(loading) {
|
|
|
|
setState((prev) => ({ ...prev, loading }));
|
|
|
|
},
|
|
|
|
async doRequest(auth, _background) {
|
|
|
|
return authClientRef.current.registerRequest({
|
|
|
|
username,
|
|
|
|
password,
|
|
|
|
auth: auth || undefined,
|
|
|
|
});
|
|
|
|
},
|
|
|
|
stateUpdated(nextStage, status) {
|
|
|
|
if (status.error) {
|
|
|
|
throw new Error(error);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (nextStage === "m.login.terms") {
|
|
|
|
interactiveAuth.submitAuthDict({
|
|
|
|
type: "m.login.terms",
|
|
|
|
});
|
|
|
|
} else if (nextStage === "m.login.recaptcha") {
|
|
|
|
interactiveAuth.submitAuthDict({
|
|
|
|
type: "m.login.recaptcha",
|
|
|
|
response: recaptchaResponse,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
const { user_id, access_token, device_id } =
|
|
|
|
await interactiveAuth.attemptAuth();
|
|
|
|
|
|
|
|
const client = await initClient({
|
|
|
|
baseUrl: defaultHomeserver,
|
|
|
|
accessToken: access_token,
|
|
|
|
userId: user_id,
|
|
|
|
deviceId: device_id,
|
|
|
|
});
|
|
|
|
|
2022-02-15 20:46:58 +00:00
|
|
|
await client.setDisplayName(displayName);
|
2022-01-18 21:41:14 +00:00
|
|
|
|
2022-01-06 00:47:53 +00:00
|
|
|
const session = { user_id, device_id, access_token, passwordlessUser };
|
|
|
|
|
|
|
|
if (passwordlessUser) {
|
|
|
|
session.tempPassword = password;
|
|
|
|
}
|
|
|
|
|
2022-03-03 03:12:18 +00:00
|
|
|
const user = client.getUser(client.getUserId());
|
|
|
|
|
|
|
|
user.setRawDisplayName(displayName);
|
|
|
|
user.setDisplayName(displayName);
|
|
|
|
|
2022-05-17 16:34:05 +00:00
|
|
|
return [client, session];
|
2022-01-06 00:47:53 +00:00
|
|
|
},
|
2022-05-17 16:34:05 +00:00
|
|
|
[]
|
2022-01-06 00:47:53 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
return [state, register];
|
|
|
|
}
|