Skip to content

Commit

Permalink
add guest support (#1002)
Browse files Browse the repository at this point in the history
Add support for anonymous login and reformat the UI of the login page to make the new set of options intuitive.
  • Loading branch information
mizlan committed Apr 12, 2023
1 parent 1fb4a46 commit ef2bc4d
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 40 deletions.
2 changes: 1 addition & 1 deletion src/components/Login.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ const gradientColors = {
const themeColors = {
0: ['#FF5F54', 'white'],
1: ['#36C5FC', 'white'],
2: ['#ACF53F', 'white'],
2: ['#ACF53F', 'black'],
3: ['#FFF065', 'black'],
4: ['#FF94DB', 'white'],
};
Expand Down
92 changes: 65 additions & 27 deletions src/components/Login/LoginForm.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ import { RingLoader } from 'react-spinners';
import { Button } from 'reactstrap';

import { EMAIL_DOMAIN_NAME } from '../../constants';
import { signInWithEmailAndPassword, getCreateUserErrorMessage } from '../../firebase';
import {
signInWithEmailAndPassword,
signInAnonymously,
getCreateUserErrorMessage,
} from '../../firebase';
import LoginInput from './LoginInput';
import '../../styles/Login.scss';

Expand All @@ -16,6 +20,20 @@ export default function LoginModal(props) {
const [waiting, setWaiting] = useState(false);
const [hoverButton, setHoverButton] = useState(false);

const handleAnonymousLogin = (e) => {
setWaiting(true);
setErrorMsg('');

e.preventDefault();
signInAnonymously()
.then(() => {})
.catch((err) => {
console.error(err);
setErrorMsg(getCreateUserErrorMessage(err));
setWaiting(false);
});
};

const handleEmailLogin = (e) => {
setWaiting(true);
setErrorMsg('');
Expand Down Expand Up @@ -48,7 +66,7 @@ export default function LoginModal(props) {
);
}

return <br />;
return null;
};

const renderInputs = () => (
Expand All @@ -63,22 +81,22 @@ export default function LoginModal(props) {

const renderAction = () => {
const { themeColor, textColor } = props;
const unclickedStyle = {
const clickedStyle = {
backgroundColor: 'white',
borderColor: themeColor,
borderWidth: 'medium',
borderRadius: '4px',
color: 'black',
};

const clickedStyle = {
const unclickedStyle = {
backgroundColor: themeColor,
borderColor: themeColor,
borderWidth: 'medium',
borderRadius: '4px',
color: textColor,
};

const subButtonStyle = {
color: 'var(--bs-blue)',
};

if (waiting) {
return (
<div className="login-form-loader">
Expand All @@ -87,34 +105,45 @@ export default function LoginModal(props) {
);
}
return (
<div>
<div className="mt-2">
<Button
size="lg"
type="submit"
className="login-main-button"
style={hoverButton ? clickedStyle : unclickedStyle}
onMouseEnter={() => setHoverButton(!hoverButton)}
onMouseLeave={() => setHoverButton(!hoverButton)}
>
Login
</Button>
<Link
to={{
pathname: '/createUser',
state: { username, password },
}}
className="login-form-link ml-4"
>
or, create an account
</Link>
</div>
);
};

return (
<div>
<form className="login-form" onSubmit={handleEmailLogin}>
{renderInputs()}
{renderAction()}
<div className="login-sub-options">
<hr className="mt-4 mb-3" />
<p
style={{ color: '#595959', textAlign: 'center', width: '100%' }}
className="mb-4 w-100"
>
Don&apos;t have an account?
<br />
<Link
to={{
pathname: '/createUser',
state: { username, password },
}}
className="login-form-link"
style={subButtonStyle}
>
Sign up
</Link>
{' or '}
<Button
onClick={handleAnonymousLogin}
className="login-guest-button"
style={subButtonStyle}
>
continue as guest
</Button>
</p>
</div>
{!waiting && (
<details className="mt-2">
<summary>Forgot your password?</summary>
Expand All @@ -128,6 +157,15 @@ export default function LoginModal(props) {
</p>
</details>
)}
</div>
);
};

return (
<div>
<form className="login-form" onSubmit={handleEmailLogin}>
{renderInputs()}
{renderAction()}
</form>
</div>
);
Expand Down
3 changes: 3 additions & 0 deletions src/firebase/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
onAuthStateChanged,
signOut,
signInWithEmailAndPassword,
signInAnonymously,
createUserWithEmailAndPassword,
} from 'firebase/auth';

Expand Down Expand Up @@ -43,6 +44,7 @@ const onAuthStateChangedFn = (...args) => onAuthStateChanged(auth, ...args);
const signOutFn = (...args) => signOut(auth, ...args);
const createUserWithEmailAndPasswordFn = (...args) => createUserWithEmailAndPassword(auth, ...args);
const signInWithEmailAndPasswordFn = (...args) => signInWithEmailAndPassword(auth, ...args);
const signInAnonymouslyFn = (...args) => signInAnonymously(auth, ...args);

const getCreateUserErrorMessage = (err) => {
let newMsg = err.message;
Expand Down Expand Up @@ -89,5 +91,6 @@ export {
signOutFn as signOut,
createUserWithEmailAndPasswordFn as createUserWithEmailAndPassword,
signInWithEmailAndPasswordFn as signInWithEmailAndPassword,
signInAnonymouslyFn as signInAnonymously,
getCreateUserErrorMessage,
};
44 changes: 32 additions & 12 deletions src/styles/Login.scss
Original file line number Diff line number Diff line change
Expand Up @@ -107,32 +107,55 @@

.login-form-input {
appearance: none;
background-color: $theme-dark;
border: 0;
border-left: 0;
border-color: #949494;
border-radius: 8px;
border-right: 0;
border-top: 0;
border-style: solid;
box-shadow: none;
color: $off-white;
color: $theme-dark;
display: inline-block;
font-size: 1.5em;
font-variant-ligatures: none;
height: auto;
line-height: normal;
margin: 0 2px 1rem; /* last value controls how far the next input starts */
max-width: 400px;
min-width: 300px;
outline: 0;
padding: 0.2rem 0.5rem;
transition: box-shadow 70ms ease-out, border-color 70ms ease-out;
width: 50%;
width: clamp(300px, 50%, 400px);
}

.login-form-input:disabled {
color: #686868;
}

.login-main-button {
border-radius: 4px;
border-width: medium;
width: clamp(300px, 50%, 400px);
}

.login-sub-options {
width: clamp(300px, 50%, 400px);

@media only screen and (max-width: 1024px) {
margin: auto;
}
}

.login-guest-button {
background: none;
border: none;
cursor: pointer;
padding: 0;
text-decoration: underline;
vertical-align: baseline;
}

.login-guest-button:hover {
background: none;
border: none;
}

/* stylelint-disable-next-line selector-pseudo-element-no-unknown */
.login-modal ::input-placeholder {
/* Chrome/Opera/Safari */
Expand All @@ -143,9 +166,6 @@

.login-form-link {
color: inherit;
font-size: 1.2rem;
margin-left: 5px;
text-decoration: none;
}

.login-form-link:hover {
Expand Down

0 comments on commit ef2bc4d

Please sign in to comment.