Browse Source

implemented telegram login

feature/telegramLogin
Nareshkumar Rao 3 years ago
parent
commit
b8cba89c39
  1. 1
      .env.template
  2. 1
      .gitignore
  3. 1
      package.json
  4. 4
      src/App.js
  5. 107
      src/screens/CreateScreen.js
  6. 59
      src/screens/LoginScreen.js
  7. 2
      src/screens/VerifyScreen.js
  8. 16
      yarn.lock

1
.env.template

@ -1 +1,2 @@
REACT_APP_API_URL= REACT_APP_API_URL=
REACT_APP_TELEGRAM_BOT_NAME=

1
.gitignore

@ -24,3 +24,4 @@ npm-debug.log*
yarn-debug.log* yarn-debug.log*
yarn-error.log* yarn-error.log*
.env.production .env.production
.env.development

1
package.json

@ -20,6 +20,7 @@
"react-redux": "^7.2.4", "react-redux": "^7.2.4",
"react-router-dom": "^5.2.0", "react-router-dom": "^5.2.0",
"react-scripts": "4.0.3", "react-scripts": "4.0.3",
"react-telegram-login": "^1.1.2",
"web-vitals": "^0.2.2" "web-vitals": "^0.2.2"
}, },
"scripts": { "scripts": {

4
src/App.js

@ -3,7 +3,6 @@ import { React } from 'react';
import { Redirect, Route, Switch } from 'react-router-dom'; import { Redirect, Route, Switch } from 'react-router-dom';
import Home from './screens/HomeScreen'; import Home from './screens/HomeScreen';
import Login from './screens/LoginScreen'; import Login from './screens/LoginScreen';
import Create from './screens/CreateScreen';
import Success from './screens/SuccessScreen'; import Success from './screens/SuccessScreen';
import './App.css'; import './App.css';
import Verify from './screens/VerifyScreen'; import Verify from './screens/VerifyScreen';
@ -18,9 +17,6 @@ function App() {
<Route path="/login"> <Route path="/login">
<Login /> <Login />
</Route> </Route>
<Route path="/create">
<Create />
</Route>
<Route path="/success"> <Route path="/success">
<Success /> <Success />
</Route> </Route>

107
src/screens/CreateScreen.js

@ -1,107 +0,0 @@
import {
Button,
Divider,
Flex,
FormControl,
FormLabel,
Heading,
Input, useToast
} from '@chakra-ui/react';
import axios from 'axios';
import { useState } from 'react';
import { useDispatch } from 'react-redux';
import { useHistory } from 'react-router-dom';
import { authLogin } from '../features/auth/authSlice';
function Create() {
const dispatch = useDispatch();
const toast = useToast();
const history = useHistory();
const [telegram, setTelegram] = useState(null);
const [password, setPassword] = useState(null);
const handleSubmit = e => {
e.preventDefault();
if (!telegram | !password) {
toast({
title: 'Problem!',
description: 'Please fill in all the fields',
status: 'error',
duration: 2000,
isClosable: true,
});
return;
}
axios
.post(
`${process.env.REACT_APP_API_URL}/create`,
{
telegram: telegram,
password: password,
},
{
withCredentials: true,
}
)
.then(response => {
if (response.data.success) {
dispatch(authLogin());
history.push('/success');
} else {
toast({
title: 'An error occurred',
description: response.data.message,
status: 'error',
duration: 5000,
isClosable: true,
});
}
})
.catch(e => {
if (e.response.status === 401) {
toast({
title: 'Verification Error',
description:
"You couldn't be verified. Please try scanning the verification QR Code again.",
status: 'error',
duration: 9000,
isClosable: true,
});
}
});
};
return (
<Flex
height="100vh"
background="teal.100"
alignItems="center"
justifyContent="center"
>
<Flex direction="column" background="white" p={12} rounded={6}>
<Heading mb={6}>Create Account</Heading>
<Button onClick={()=>{history.push("/login");}} mb={6}>I already have an account!</Button>
<Divider mb={6} />
<form onSubmit={handleSubmit}>
<FormControl mb={6}>
<FormLabel>Telegram Username:</FormLabel>
<Input onChange={e => setTelegram(e.target.value)} />
</FormControl>
<FormControl mb={6}>
<FormLabel>Password:</FormLabel>
<Input
type="password"
onChange={e => setPassword(e.target.value)}
/>
</FormControl>
<Button type="submit">Create!</Button>
</form>
</Flex>
</Flex>
);
}
export default Create;

59
src/screens/LoginScreen.js

@ -1,21 +1,14 @@
import { import {
Button,
Flex,
FormControl,
FormLabel,
Heading,
Input,
useToast
Flex, Heading, useToast
} from '@chakra-ui/react'; } from '@chakra-ui/react';
import axios from 'axios'; import axios from 'axios';
import { React, useState } from 'react';
import { React } from 'react';
import { useDispatch, useSelector } from 'react-redux'; import { useDispatch, useSelector } from 'react-redux';
import { Redirect, useHistory } from 'react-router-dom'; import { Redirect, useHistory } from 'react-router-dom';
import TelegramLoginButton from 'react-telegram-login';
import { authLogin, authLogout } from '../features/auth/authSlice'; import { authLogin, authLogout } from '../features/auth/authSlice';
function Login() { function Login() {
const [telegram, setTelegram] = useState(null);
const [password, setPassword] = useState(null);
const toast = useToast(); const toast = useToast();
const history = useHistory(); const history = useHistory();
const dispatch = useDispatch(); const dispatch = useDispatch();
@ -23,31 +16,12 @@ function Login() {
const isAuthenticated = useSelector(state => state.auth.isAuthenticated); const isAuthenticated = useSelector(state => state.auth.isAuthenticated);
if (isAuthenticated) return <Redirect to="/home" />; if (isAuthenticated) return <Redirect to="/home" />;
const handleSubmit = e => {
if (!telegram | !password) {
toast({
title: 'Invalid Login',
description: 'Please fill in Telegram Username and password',
status: 'error',
duration: 9000,
isClosable: true,
});
return;
}
toast({
title: 'Loading',
status: 'info',
duration: 9000,
isClosable: true,
});
const handleTelegramResponse = (response) => {
axios axios
.post( .post(
`${process.env.REACT_APP_API_URL}/login`, `${process.env.REACT_APP_API_URL}/login`,
{ {
telegram: telegram,
password: password,
telegramResponse: response,
}, },
{ {
withCredentials: true, withCredentials: true,
@ -96,8 +70,6 @@ function Login() {
} }
}); });
e.preventDefault();
}; };
return ( return (
@ -114,26 +86,7 @@ function Login() {
<Heading size="lg" mb={4}> <Heading size="lg" mb={4}>
Login Login
</Heading> </Heading>
<form onSubmit={handleSubmit}>
<FormControl mb={6}>
<FormLabel>Telegram Username:</FormLabel>
<Input
colorScheme="teal"
onChange={event => setTelegram(event.target.value)}
/>
</FormControl>
<FormControl mb={6}>
<FormLabel>Password:</FormLabel>
<Input
type="password"
colorScheme="teal"
onChange={event => setPassword(event.target.value)}
/>
</FormControl>
<Button mb={6} type="submit">
Login!
</Button>
</form>
<TelegramLoginButton dataOnauth={handleTelegramResponse} botName={process.env.REACT_APP_TELEGRAM_BOT_NAME} />
</Flex> </Flex>
</Flex> </Flex>
); );

2
src/screens/VerifyScreen.js

@ -22,7 +22,7 @@ function Verify(props) {
if (response.data.loggedIn) { if (response.data.loggedIn) {
history.push("/success"); history.push("/success");
} else { } else {
history.push("/create");
history.push("/login");
} }
} }
}) })

16
yarn.lock

@ -10065,6 +10065,22 @@ react-style-singleton@^2.1.0:
invariant "^2.2.4" invariant "^2.2.4"
tslib "^1.0.0" tslib "^1.0.0"
react-telegram-login@^1.1.2:
version "1.1.2"
resolved "https://registry.yarnpkg.com/react-telegram-login/-/react-telegram-login-1.1.2.tgz#28b9bdd68bb2710afca19354ac1f9428092836f0"
integrity sha512-pDP+bvfaklWgnK5O6yvZnIwgky0nnYUU6Zhk0EjdMSkPsLQoOzZRsXIoZnbxyBXhi7346bsxMH+EwwJPTxClDw==
dependencies:
react "^16.13.1"
react@^16.13.1:
version "16.14.0"
resolved "https://registry.yarnpkg.com/react/-/react-16.14.0.tgz#94d776ddd0aaa37da3eda8fc5b6b18a4c9a3114d"
integrity sha512-0X2CImDkJGApiAlcf0ODKIneSwBPhqJawOa5wCtKbu7ZECrmS26NvtSILynQ66cgkT/RJ4LidJOc3bUESwmU8g==
dependencies:
loose-envify "^1.1.0"
object-assign "^4.1.1"
prop-types "^15.6.2"
react@^17.0.2: react@^17.0.2:
version "17.0.2" version "17.0.2"
resolved "https://registry.yarnpkg.com/react/-/react-17.0.2.tgz#d0b5cc516d29eb3eee383f75b62864cfb6800037" resolved "https://registry.yarnpkg.com/react/-/react-17.0.2.tgz#d0b5cc516d29eb3eee383f75b62864cfb6800037"

Loading…
Cancel
Save