From a7539eb34e7dfb538d7ad31f25760d8d940d19f2 Mon Sep 17 00:00:00 2001 From: Robert Long Date: Thu, 16 Dec 2021 16:43:35 -0800 Subject: [PATCH] Add change password flow for anonymous users --- src/ConferenceCallManagerHooks.jsx | 63 ++++++++++++++++-- src/Input.jsx | 19 +++++- src/Input.module.css | 11 +++- src/RegisterPage.jsx | 100 ++++++++++++++++++++++------- src/Room.jsx | 33 ++++++---- 5 files changed, 181 insertions(+), 45 deletions(-) diff --git a/src/ConferenceCallManagerHooks.jsx b/src/ConferenceCallManagerHooks.jsx index b559a25e..2e5d32d7 100644 --- a/src/ConferenceCallManagerHooks.jsx +++ b/src/ConferenceCallManagerHooks.jsx @@ -130,8 +130,14 @@ export function ClientProvider({ homeserverUrl, children }) { const authStore = localStorage.getItem("matrix-auth-store"); if (authStore) { - const { user_id, device_id, access_token, guest, passwordlessUser } = - JSON.parse(authStore); + const { + user_id, + device_id, + access_token, + guest, + passwordlessUser, + tempPassword, + } = JSON.parse(authStore); const client = await initClient( { @@ -151,6 +157,7 @@ export function ClientProvider({ homeserverUrl, children }) { access_token, guest, passwordlessUser, + tempPassword, }) ); @@ -311,10 +318,13 @@ export function ClientProvider({ homeserverUrl, children }) { deviceId: device_id, }); - localStorage.setItem( - "matrix-auth-store", - JSON.stringify({ user_id, device_id, access_token, passwordlessUser }) - ); + const session = { user_id, device_id, access_token, passwordlessUser }; + + if (passwordlessUser) { + session.tempPassword = password; + } + + localStorage.setItem("matrix-auth-store", JSON.stringify(session)); setState({ client, @@ -340,6 +350,45 @@ export function ClientProvider({ homeserverUrl, children }) { } }, []); + const changePassword = useCallback( + async (password) => { + const { tempPassword, passwordlessUser, ...existingSession } = JSON.parse( + localStorage.getItem("matrix-auth-store") + ); + + await client.setPassword( + { + type: "m.login.password", + identifier: { + type: "m.id.user", + user: existingSession.user_id, + }, + user: existingSession.user_id, + password: tempPassword, + }, + password + ); + + localStorage.setItem( + "matrix-auth-store", + JSON.stringify({ + ...existingSession, + passwordlessUser: false, + }) + ); + + setState({ + client, + loading: false, + isGuest: false, + isAuthenticated: true, + isPasswordlessUser: false, + userName: client.getUserIdLocalpart(), + }); + }, + [client] + ); + const logout = useCallback(() => { localStorage.removeItem("matrix-auth-store"); window.location = "/"; @@ -355,6 +404,7 @@ export function ClientProvider({ homeserverUrl, children }) { login, registerGuest, register, + changePassword, logout, userName, }), @@ -367,6 +417,7 @@ export function ClientProvider({ homeserverUrl, children }) { login, registerGuest, register, + changePassword, logout, userName, ] diff --git a/src/Input.jsx b/src/Input.jsx index d890ab3f..b9ccdc1a 100644 --- a/src/Input.jsx +++ b/src/Input.jsx @@ -22,17 +22,30 @@ export function Field({ children, className, ...rest }) { } export const InputField = forwardRef( - ({ id, label, className, type, checked, prefix, suffix, ...rest }, ref) => { + ( + { id, label, className, type, checked, prefix, suffix, disabled, ...rest }, + ref + ) => { return ( {prefix && {prefix}} - +