diff options
author | poeti8 <ezzati.upt@gmail.com> | 2020-08-09 13:55:18 +0300 |
---|---|---|
committer | poeti8 <ezzati.upt@gmail.com> | 2020-08-09 13:55:18 +0300 |
commit | b229a3ad882a26e413b5c4348007eba951d5259c (patch) | |
tree | cddf3f85e1528ea99d0b49efe5d357f66d55f4f4 /client | |
parent | dbc402f0c58a60bc68291702455ef82bcbcfbe72 (diff) |
feat: add disallow registration and anonymous links option
Resolves #290, #210, #34
Diffstat (limited to 'client')
-rw-r--r-- | client/components/Header.tsx | 11 | ||||
-rw-r--r-- | client/consts/consts.ts | 10 | ||||
-rw-r--r-- | client/pages/index.tsx | 11 | ||||
-rw-r--r-- | client/pages/login.tsx | 38 |
4 files changed, 50 insertions, 20 deletions
diff --git a/client/components/Header.tsx b/client/components/Header.tsx index 4b4cc9e..2d4fb44 100644 --- a/client/components/Header.tsx +++ b/client/components/Header.tsx @@ -5,6 +5,7 @@ import Router from "next/router"; import useMedia from "use-media"; import Link from "next/link"; +import { DISALLOW_REGISTRATION } from "../consts"; import { useStoreState } from "../store"; import styled from "styled-components"; import { RowCenterV } from "./Layout"; @@ -55,8 +56,14 @@ const Header: FC = () => { const login = !isAuthenticated && ( <Li> <Link href="/login"> - <ALink href="/login" title="login / signup" forButton> - <Button height={[32, 40]}>Login / Sign up</Button> + <ALink + href="/login" + title={!DISALLOW_REGISTRATION ? "login / signup" : "login"} + forButton + > + <Button height={[32, 40]}> + {!DISALLOW_REGISTRATION ? "Log in / Sign up" : "Log in"} + </Button> </ALink> </Link> </Li> diff --git a/client/consts/consts.ts b/client/consts/consts.ts index 15b1cf8..75e5944 100644 --- a/client/consts/consts.ts +++ b/client/consts/consts.ts @@ -1,3 +1,13 @@ +import getConfig from "next/config"; + +const { publicRuntimeConfig } = getConfig(); + +export const DISALLOW_ANONYMOUS_LINKS = + publicRuntimeConfig.DISALLOW_ANONYMOUS_LINKS === "true"; + +export const DISALLOW_REGISTRATION = + publicRuntimeConfig.DISALLOW_REGISTRATION === "true"; + export enum API { BAN_LINK = "/api/url/admin/ban", STATS = "/api/url/stats" diff --git a/client/pages/index.tsx b/client/pages/index.tsx index a08da12..1e61300 100644 --- a/client/pages/index.tsx +++ b/client/pages/index.tsx @@ -1,5 +1,7 @@ import React from "react"; +import Router from "next/router"; +import { DISALLOW_ANONYMOUS_LINKS } from "../consts"; import NeedToLogin from "../components/NeedToLogin"; import Extensions from "../components/Extensions"; import LinksTable from "../components/LinksTable"; @@ -12,6 +14,15 @@ import { useStoreState } from "../store"; const Homepage = () => { const isAuthenticated = useStoreState(s => s.auth.isAuthenticated); + if ( + !isAuthenticated && + DISALLOW_ANONYMOUS_LINKS && + typeof window !== "undefined" + ) { + Router.push("/login"); + return null; + } + return ( <AppWrapper> <Shortener /> diff --git a/client/pages/login.tsx b/client/pages/login.tsx index 55ae2e6..5a9986c 100644 --- a/client/pages/login.tsx +++ b/client/pages/login.tsx @@ -8,6 +8,7 @@ import Link from "next/link"; import axios from "axios"; import { useStoreState, useStoreActions } from "../store"; +import { APIv2, DISALLOW_REGISTRATION } from "../consts"; import { ColCenterV } from "../components/Layout"; import AppWrapper from "../components/AppWrapper"; import { TextInput } from "../components/Input"; @@ -16,7 +17,6 @@ import { Button } from "../components/Button"; import Text, { H2 } from "../components/Text"; import ALink from "../components/ALink"; import Icon from "../components/Icon"; -import { APIv2 } from "../consts"; const LoginForm = styled(Flex).attrs({ as: "form", @@ -77,7 +77,7 @@ const LoginPage = () => { } } - if (type === "signup") { + if (type === "signup" && !DISALLOW_REGISTRATION) { setLoading(s => ({ ...s, signup: true })); try { await axios.post(APIv2.AuthSignup, { email, password }); @@ -120,7 +120,7 @@ const LoginPage = () => { autoFocus /> <Text {...label("password")} as="label" mb={2} bold> - Password (min chars: 8): + Password{!DISALLOW_REGISTRATION ? " (min chars: 8)" : ""}: </Text> <TextInput {...password("password")} @@ -135,7 +135,7 @@ const LoginPage = () => { <Flex justifyContent="center"> <Button flex="1 1 auto" - mr={["8px", 16]} + mr={!DISALLOW_REGISTRATION ? ["8px", 16] : 0} height={[44, 56]} onClick={onSubmit("login")} > @@ -146,20 +146,22 @@ const LoginPage = () => { /> Log in </Button> - <Button - flex="1 1 auto" - ml={["8px", 16]} - height={[44, 56]} - color="purple" - onClick={onSubmit("signup")} - > - <Icon - name={loading.signup ? "spinner" : "signup"} - stroke="white" - mr={2} - /> - Sign up - </Button> + {!DISALLOW_REGISTRATION && ( + <Button + flex="1 1 auto" + ml={["8px", 16]} + height={[44, 56]} + color="purple" + onClick={onSubmit("signup")} + > + <Icon + name={loading.signup ? "spinner" : "signup"} + stroke="white" + mr={2} + /> + Sign up + </Button> + )} </Flex> <Link href="/reset-password"> <ALink |