import GoogleIcon from '@mui/icons-material/Google' import { Avatar, Box, Button, Container, Divider, Input, Sheet, Snackbar, Typography, } from '@mui/joy' import Cookies from 'js-cookie' import React from 'react' import { useNavigate } from 'react-router-dom' import { LoginSocialGoogle } from 'reactjs-social-login' import { API_URL, GOOGLE_CLIENT_ID, REDIRECT_URL } from '../../Config' import { UserContext } from '../../contexts/UserContext' import Logo from '../../Logo' import { GetUserProfile } from '../../utils/Fetcher' const LoginView = () => { const { userProfile, setUserProfile } = React.useContext(UserContext) const [username, setUsername] = React.useState('') const [password, setPassword] = React.useState('') const [error, setError] = React.useState(null) const Navigate = useNavigate() const handleSubmit = async e => { e.preventDefault() fetch(`${API_URL}/auth/login`, { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify({ username, password }), }) .then(response => { if (response.status === 200) { return response.json().then(data => { localStorage.setItem('ca_token', data.token) localStorage.setItem('ca_expiration', data.expire) const redirectUrl = Cookies.get('ca_redirect') // console.log('redirectUrl', redirectUrl) if (redirectUrl) { Cookies.remove('ca_redirect') Navigate(redirectUrl) } else { Navigate('/my/chores') } }) } else if (response.status === 401) { setError('Wrong username or password') } else { setError('An error occurred, please try again') console.log('Login failed') } }) .catch(err => { setError('Unable to communicate with server, please try again') console.log('Login failed', err) }) } const loggedWithProvider = function (provider, data) { return fetch(API_URL + `/auth/${provider}/callback`, { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify({ provider: provider, token: data['access_token'] || // data["access_token"] is for Google data['accessToken'], // data["accessToken"] is for Facebook data: data, }), }).then(response => { if (response.status === 200) { return response.json().then(data => { localStorage.setItem('ca_token', data.token) localStorage.setItem('ca_expiration', data.expire) const redirectUrl = Cookies.get('ca_redirect') if (redirectUrl) { Cookies.remove('ca_redirect') Navigate(redirectUrl) } else { getUserProfileAndNavigateToHome() } }) } return response.json().then(error => { setError("Couldn't log in with Google, please try again") }) }) } const getUserProfileAndNavigateToHome = () => { GetUserProfile().then(data => { data.json().then(data => { setUserProfile(data.res) // check if redirect url is set in cookie: const redirectUrl = Cookies.get('ca_redirect') if (redirectUrl) { Cookies.remove('ca_redirect') Navigate(redirectUrl) } else { Navigate('/my/chores') } }) }) } const handleForgotPassword = () => { Navigate('/forgot-password') } return ( {/* logo */} Done tick {userProfile && ( <> Welcome back,{' '} {userProfile?.displayName || userProfile?.username} )} {!userProfile && ( <> Sign in to your account to continue Username { setUsername(e.target.value) }} /> Password: { setPassword(e.target.value) }} /> )} or { loggedWithProvider(provider, data) }} onReject={err => { setError("Couldn't log in with Google, please try again") }} > setError(null)} autoHideDuration={3000} message={error} > {error} ) } export default LoginView