Add auth links to homepage for unregistered users

This commit is contained in:
Robert Long 2021-12-13 11:55:10 -08:00
commit 9d029e9847
4 changed files with 81 additions and 18 deletions

View file

@ -103,10 +103,13 @@ export async function fetchGroupCall(
export function ClientProvider({ homeserverUrl, children }) { export function ClientProvider({ homeserverUrl, children }) {
const history = useHistory(); const history = useHistory();
const [{ loading, isAuthenticated, isGuest, client, userName }, setState] = const [
useState({ { loading, isAuthenticated, isPasswordlessUser, isGuest, client, userName },
setState,
] = useState({
loading: true, loading: true,
isAuthenticated: false, isAuthenticated: false,
isPasswordlessUser: false,
isGuest: false, isGuest: false,
client: undefined, client: undefined,
userName: null, userName: null,
@ -118,7 +121,7 @@ export function ClientProvider({ homeserverUrl, children }) {
const authStore = localStorage.getItem("matrix-auth-store"); const authStore = localStorage.getItem("matrix-auth-store");
if (authStore) { if (authStore) {
const { user_id, device_id, access_token, guest } = const { user_id, device_id, access_token, guest, passwordlessUser } =
JSON.parse(authStore); JSON.parse(authStore);
const client = await initClient( const client = await initClient(
@ -133,10 +136,16 @@ export function ClientProvider({ homeserverUrl, children }) {
localStorage.setItem( localStorage.setItem(
"matrix-auth-store", "matrix-auth-store",
JSON.stringify({ user_id, device_id, access_token, guest }) JSON.stringify({
user_id,
device_id,
access_token,
guest,
passwordlessUser,
})
); );
return { client, guest }; return { client, guest, passwordlessUser };
} }
return { client: undefined, guest: false }; return { client: undefined, guest: false };
@ -147,11 +156,12 @@ export function ClientProvider({ homeserverUrl, children }) {
} }
restore() restore()
.then(({ client, guest }) => { .then(({ client, guest, passwordlessUser }) => {
setState({ setState({
client, client,
loading: false, loading: false,
isAuthenticated: !!client, isAuthenticated: !!client,
isPasswordlessUser: !!passwordlessUser,
isGuest: guest, isGuest: guest,
userName: client?.getUserIdLocalpart(), userName: client?.getUserIdLocalpart(),
}); });
@ -161,6 +171,7 @@ export function ClientProvider({ homeserverUrl, children }) {
client: undefined, client: undefined,
loading: false, loading: false,
isAuthenticated: false, isAuthenticated: false,
isPasswordlessUser: false,
isGuest: false, isGuest: false,
userName: null, userName: null,
}); });
@ -209,6 +220,7 @@ export function ClientProvider({ homeserverUrl, children }) {
client, client,
loading: false, loading: false,
isAuthenticated: true, isAuthenticated: true,
isPasswordlessUser: false,
isGuest: false, isGuest: false,
userName: client.getUserIdLocalpart(), userName: client.getUserIdLocalpart(),
}); });
@ -218,6 +230,7 @@ export function ClientProvider({ homeserverUrl, children }) {
client: undefined, client: undefined,
loading: false, loading: false,
isAuthenticated: false, isAuthenticated: false,
isPasswordlessUser: false,
isGuest: false, isGuest: false,
userName: null, userName: null,
}); });
@ -256,6 +269,7 @@ export function ClientProvider({ homeserverUrl, children }) {
loading: false, loading: false,
isAuthenticated: true, isAuthenticated: true,
isGuest: true, isGuest: true,
isPasswordlessUser: false,
userName: client.getUserIdLocalpart(), userName: client.getUserIdLocalpart(),
}); });
} catch (err) { } catch (err) {
@ -264,6 +278,7 @@ export function ClientProvider({ homeserverUrl, children }) {
client: undefined, client: undefined,
loading: false, loading: false,
isAuthenticated: false, isAuthenticated: false,
isPasswordlessUser: false,
isGuest: false, isGuest: false,
userName: null, userName: null,
}); });
@ -271,7 +286,7 @@ export function ClientProvider({ homeserverUrl, children }) {
} }
}, []); }, []);
const register = useCallback(async (username, password) => { const register = useCallback(async (username, password, passwordlessUser) => {
try { try {
const registrationClient = matrix.createClient(homeserverUrl); const registrationClient = matrix.createClient(homeserverUrl);
@ -289,7 +304,7 @@ export function ClientProvider({ homeserverUrl, children }) {
localStorage.setItem( localStorage.setItem(
"matrix-auth-store", "matrix-auth-store",
JSON.stringify({ user_id, device_id, access_token }) JSON.stringify({ user_id, device_id, access_token, passwordlessUser })
); );
setState({ setState({
@ -297,6 +312,7 @@ export function ClientProvider({ homeserverUrl, children }) {
loading: false, loading: false,
isGuest: false, isGuest: false,
isAuthenticated: true, isAuthenticated: true,
isPasswordlessUser: passwordlessUser,
userName: client.getUserIdLocalpart(), userName: client.getUserIdLocalpart(),
}); });
@ -308,6 +324,7 @@ export function ClientProvider({ homeserverUrl, children }) {
loading: false, loading: false,
isGuest: false, isGuest: false,
isAuthenticated: false, isAuthenticated: false,
isPasswordlessUser: false,
userName: null, userName: null,
}); });
throw err; throw err;
@ -323,6 +340,7 @@ export function ClientProvider({ homeserverUrl, children }) {
() => ({ () => ({
loading, loading,
isAuthenticated, isAuthenticated,
isPasswordlessUser,
isGuest, isGuest,
client, client,
login, login,
@ -334,6 +352,7 @@ export function ClientProvider({ homeserverUrl, children }) {
[ [
loading, loading,
isAuthenticated, isAuthenticated,
isPasswordlessUser,
isGuest, isGuest,
client, client,
login, login,
@ -417,7 +436,7 @@ export function useCreateRoom() {
let _client = client; let _client = client;
if (!_client) { if (!_client) {
_client = await register(userName, randomString(16)); _client = await register(userName, randomString(16), true);
} }
return await createRoom(_client, roomName); return await createRoom(_client, roomName);

View file

@ -15,7 +15,7 @@ limitations under the License.
*/ */
import React, { useCallback } from "react"; import React, { useCallback } from "react";
import { useHistory } from "react-router-dom"; import { useHistory, Link } from "react-router-dom";
import { import {
useClient, useClient,
useGroupCallRooms, useGroupCallRooms,
@ -32,7 +32,14 @@ import classNames from "classnames";
import { ErrorView, LoadingView } from "./FullScreenView"; import { ErrorView, LoadingView } from "./FullScreenView";
export function Home() { export function Home() {
const { isAuthenticated, isGuest, loading, error, client } = useClient(); const {
isAuthenticated,
isGuest,
isPasswordlessUser,
loading,
error,
client,
} = useClient();
const history = useHistory(); const history = useHistory();
const { createRoomError, creatingRoom, createRoom } = useCreateRoom(); const { createRoomError, creatingRoom, createRoom } = useCreateRoom();
@ -80,6 +87,8 @@ export function Home() {
return ( return (
<RegisteredView <RegisteredView
client={client} client={client}
isPasswordlessUser={isPasswordlessUser}
isGuest={isGuest}
onCreateRoom={onCreateRoom} onCreateRoom={onCreateRoom}
createRoomError={createRoomError} createRoomError={createRoomError}
creatingRoom={creatingRoom} creatingRoom={creatingRoom}
@ -168,6 +177,13 @@ function UnregisteredView({
</Button> </Button>
</FieldRow> </FieldRow>
</form> </form>
<div className={styles.authLinks}>
<p>
Not registered yet?{" "}
<Link to="/register">Create an account</Link>
</p>
</div>
</div> </div>
</div> </div>
</div> </div>
@ -178,6 +194,8 @@ function UnregisteredView({
function RegisteredView({ function RegisteredView({
client, client,
isPasswordlessUser,
isGuest,
onCreateRoom, onCreateRoom,
createRoomError, createRoomError,
creatingRoom, creatingRoom,
@ -257,6 +275,14 @@ function RegisteredView({
</Button> </Button>
</FieldRow> </FieldRow>
</form> </form>
{(isPasswordlessUser || isGuest) && (
<div className={styles.authLinks}>
<p>
Not registered yet?{" "}
<Link to="/register">Create an account</Link>
</p>
</div>
)}
</div> </div>
</div> </div>
</div> </div>

View file

@ -111,6 +111,24 @@
margin-top: 0; margin-top: 0;
} }
.authLinks {
display: flex;
flex-direction: column;
justify-content: flex-end;
align-items: center;
}
.authLinks {
margin-bottom: 100px;
font-size: 15px;
}
.authLinks a {
color: #0dbd8b;
font-weight: normal;
text-decoration: none;
}
@media (min-width: 800px) { @media (min-width: 800px) {
.left { .left {
background-color: var(--bgColor2); background-color: var(--bgColor2);

View file

@ -63,11 +63,11 @@
.authLinks { .authLinks {
margin-bottom: 100px; margin-bottom: 100px;
font-weight: normal;
font-size: 15px; font-size: 15px;
} }
.authLinks a { .authLinks a {
color: #0dbd8b; color: #0dbd8b;
text-decoration: none; text-decoration: none;
font-weight: normal;
} }