Commit cf746962 authored by Ermolaev Timur's avatar Ermolaev Timur

Merge branch 'task-31-feature/connect_registration_login_with_back' into 'development'

Task 31 feature/connect registration login with back

See merge request !19
parents ba39a466 17659ca3
This diff is collapsed.
...@@ -15,6 +15,7 @@ ...@@ -15,6 +15,7 @@
"license": "ISC", "license": "ISC",
"devDependencies": { "devDependencies": {
"@faker-js/faker": "^7.6.0", "@faker-js/faker": "^7.6.0",
"@types/multer": "^1.4.7",
"@types/node": "^18.11.8", "@types/node": "^18.11.8",
"@typescript-eslint/eslint-plugin": "^5.41.0", "@typescript-eslint/eslint-plugin": "^5.41.0",
"@typescript-eslint/parser": "^5.41.0", "@typescript-eslint/parser": "^5.41.0",
...@@ -34,7 +35,9 @@ ...@@ -34,7 +35,9 @@
"cors": "^2.8.5", "cors": "^2.8.5",
"express": "^4.18.2", "express": "^4.18.2",
"mongoose": "^6.7.0", "mongoose": "^6.7.0",
"multer": "^1.4.5-lts.1",
"nanoid": "^3.3.4", "nanoid": "^3.3.4",
"path": "^0.12.7",
"pg": "^8.8.0", "pg": "^8.8.0",
"reflect-metadata": "^0.1.13", "reflect-metadata": "^0.1.13",
"typeorm": "^0.3.10" "typeorm": "^0.3.10"
......
*
!.gitignore
!_ACzV5N3r69BCMdpyqmGC.png
!lDmNHjGy5I2g1mWMR-n_c.png
!ExATi9O98uKnHxJ_wqUS2.png
!Aqvv8P8AgL_1xfC93CLKp.png
!NqUZ1Tq8pxbVptmhZPKcd.png
!46AlFQI4qKG2GpuJGdKbG.png
!ip5KgNCs30o3djHb8V_GR.png
!1tlFOumiL3MmH89O8OO6H.png
!nTVRV1ZePPe7tTIdGKKfX.png
\ No newline at end of file
import path = require('path');
const rootPath = __dirname;
export const config = {
rootPath,
uploadPath: path.join(rootPath, "public", "uploads"),
db: {
url: "mongodb://localhost/",
name: "planner"
}
};
...@@ -2,10 +2,25 @@ import express,{Router, Request, Response} from 'express'; ...@@ -2,10 +2,25 @@ import express,{Router, Request, Response} from 'express';
import {User} from '../models/User'; import {User} from '../models/User';
import {myDataSource} from '../app-data-source'; import {myDataSource} from '../app-data-source';
import { nanoid } from 'nanoid'; import { nanoid } from 'nanoid';
import multer = require('multer');
import path = require("path");
import {config} from "../config"
const router:Router = express.Router(); const router:Router = express.Router();
const dataSource = myDataSource; const dataSource = myDataSource;
const storage = multer.diskStorage({
destination: (req, file, cb) => {
cb(null, config.uploadPath);
},
filename: (req, file, cb) => {
cb(null, nanoid() + path.extname(file.originalname));
}
})
const upload = multer({ storage })
router.get('/', async (req : Request, res : Response):Promise<object> => { router.get('/', async (req : Request, res : Response):Promise<object> => {
const users = await dataSource const users = await dataSource
.getRepository(User) .getRepository(User)
...@@ -15,9 +30,10 @@ return res.send({users}) ...@@ -15,9 +30,10 @@ return res.send({users})
}) })
router.post('/', async (req : Request, res : Response):Promise<object> => { router.post('/', upload.single("avatar"), async (req : Request, res : Response):Promise<object> => {
console.log(req.body)
const {name,surname,password,email, role} = req.body; const {name,surname,password,email, role} = req.body;
console.log(req.body)
const displayName = surname+' '+name[0]+'.' const displayName = surname+' '+name[0]+'.'
const user = new User(); const user = new User();
user.name = name; user.name = name;
...@@ -27,16 +43,20 @@ router.post('/', async (req : Request, res : Response):Promise<object> => { ...@@ -27,16 +43,20 @@ router.post('/', async (req : Request, res : Response):Promise<object> => {
user.email = email; user.email = email;
user.role = role; user.role = role;
user.generateToken() user.generateToken()
console.log("user " + user)
await user.save(); await user.save();
console.log("saved")
const userToFront:User|null = await dataSource.manager.findOneBy(User, { const userToFront:User|null = await dataSource.manager.findOneBy(User, {
email: user.email email: user.email
}) })
console.log("userToFront " + userToFront)
return res.send({userToFront}) return res.send({userToFront})
}) })
router.post('/sessions/', async (req : Request, res : Response):Promise<object> => { router.post('/sessions/', async (req : Request, res : Response):Promise<object> => {
const {email, password} = req.body; const {email, password} = req.body;
console.log("email" + email, "password" + password )
const user = await dataSource const user = await dataSource
.createQueryBuilder() .createQueryBuilder()
.select("user") .select("user")
...@@ -46,6 +66,7 @@ router.post('/sessions/', async (req : Request, res : Response):Promise<object> ...@@ -46,6 +66,7 @@ router.post('/sessions/', async (req : Request, res : Response):Promise<object>
.getOne() .getOne()
if(!user) return res.status(404).send({Message:'user not found'}) if(!user) return res.status(404).send({Message:'user not found'})
const isMatch:boolean = await user.checkPassword(password); const isMatch:boolean = await user.checkPassword(password);
console.log("123")
if (!isMatch) return res.status(400).send({ if (!isMatch) return res.status(400).send({
error: "Wrong Password" error: "Wrong Password"
}) })
......
...@@ -6,6 +6,7 @@ import MyTasks from './containers/MyTasks/MyTasks'; ...@@ -6,6 +6,7 @@ import MyTasks from './containers/MyTasks/MyTasks';
import Login from './containers/Login/Login'; import Login from './containers/Login/Login';
import Register from './containers/Register/Register'; import Register from './containers/Register/Register';
import MonthCalendar from './containers/MonthCalendar/MonthCalendar'; import MonthCalendar from './containers/MonthCalendar/MonthCalendar';
import ForgottenPassword from "./containers/ForgottenPassword/ForgottenPassword";
const ProtectedRoute = ({isAllowed, roles, redirectUrl, children}) => { const ProtectedRoute = ({isAllowed, roles, redirectUrl, children}) => {
const user = useSelector(state => state.users?.user); const user = useSelector(state => state.users?.user);
...@@ -95,6 +96,7 @@ const App = () => { ...@@ -95,6 +96,7 @@ const App = () => {
}/> }/>
<Route path={"/sign-in"} element={<Login/>}/> <Route path={"/sign-in"} element={<Login/>}/>
<Route path={"/forgottenpassword"} element={<ForgottenPassword/>}/>
<Route path='*' element={<h1>404</h1>}/> <Route path='*' element={<h1>404</h1>}/>
</Route> </Route>
</Routes> </Routes>
......
import { Button, Grid} from "@mui/material";
import { NavLink } from "react-router-dom";
import FormElement from "../UI/Form/FormElement/FormElement";
const ResetPasswordForm = ({ state, onChange, onSubmit, getFieldError, buttonText }) => {
return <form onSubmit={onSubmit}>
<Grid container spacing={2}>
<FormElement
onChange={onChange}
name="email"
label="Enter your email address"
state={state}
error={getFieldError?.("email")}
/>
</Grid>
<Button
sx={{ mt: "15px" }}
type="submit"
fullWidth
variant="contained"
color="primary"
>
{buttonText}
</Button>
</form>
}
export default ResetPasswordForm;
\ No newline at end of file
import { Button, Grid} from "@mui/material"; import { Button, Grid} from "@mui/material";
// import { NavLink } from "react-router-dom"; import { NavLink } from "react-router-dom";
import FormElement from "../UI/Form/FormElement/FormElement"; import FormElement from "../UI/Form/FormElement/FormElement";
const UserForm = ({ state, onChange, onSubmit, getFieldError, buttonText, resetPassword }) => { const UserForm = ({ state, onChange, onSubmit, getFieldError, buttonText, resetPassword }) => {
...@@ -30,17 +30,17 @@ const UserForm = ({ state, onChange, onSubmit, getFieldError, buttonText, resetP ...@@ -30,17 +30,17 @@ const UserForm = ({ state, onChange, onSubmit, getFieldError, buttonText, resetP
> >
{buttonText} {buttonText}
</Button> </Button>
{/* <Button <Button
component={NavLink} component={NavLink}
to={"/forgottenpassword"} to={"/forgottenpassword"}
sx={{ mt: "15px" }} sx={{ mt: "15px", color: "red" }}
type="submit" type="submit"
fullWidth fullWidth
variant="contained" variant="contained"
color="primary" color="inherit"
> >
{resetPassword} {resetPassword}
</Button> */} </Button>
</form> </form>
} }
......
...@@ -25,13 +25,13 @@ const UserRegistrationForm = ({ state, onChange, onSubmit, getFieldError, button ...@@ -25,13 +25,13 @@ const UserRegistrationForm = ({ state, onChange, onSubmit, getFieldError, button
state={state} state={state}
error={getFieldError?.("email")} error={getFieldError?.("email")}
/> />
<FormElement {/* <FormElement
onChange={onChange} onChange={onChange}
name="number" name="number"
label="Number" label="Number"
state={state} state={state}
error={getFieldError?.("number")} error={getFieldError?.("number")}
/> /> */}
<FormElement <FormElement
onChange={onChange} onChange={onChange}
name="password" name="password"
......
// import { useState } from "react"; import { useState } from "react";
// import { useDispatch, useSelector } from "react-redux"; import { useDispatch, useSelector } from "react-redux";
// import { useNavigate } from "react-router-dom"; import { useNavigate } from "react-router-dom";
// import Loader from "../../components/UI/Loader/Loader"; import Loader from "../../components/UI/Loader/Loader";
// import UserForm from "../../components/UserForm/UserForm"; import { loginUser } from "../../store/actions/usersActions";
// import { loginUser } from "../../store/actions/usersActions"; import PersonIcon from '@mui/icons-material/Person';
// import PersonIcon from '@mui/icons-material/Person'; import styled from "@emotion/styled";
// import styled from "@emotion/styled"; import { Alert, Avatar, Container, Typography } from "@mui/material";
// import { Alert, Avatar, Container, Typography } from "@mui/material"; import ResetPasswordForm from "../../components/UserForm/ResetPasswordForm";
// const StyledContainer = styled(Container)` const StyledContainer = styled(Container)`
// padding-top: 30px; padding-top: 30px;
// padding-bottom: 30px; padding-bottom: 30px;
// box-shadow: 0 18px 30px 0 rgba(0, 0, 0, 0.6); box-shadow: 0 18px 30px 0 rgba(0, 0, 0, 0.6);
// border-radius: 6px; border-radius: 6px;
// `; `;
// const StyledTitle = styled(Typography)` const StyledTitle = styled(Typography)`
// text-align: center; text-align: center;
// font-size: 30px; font-size: 30px;
// margin-bottom: 30px; margin-bottom: 30px;
// `; `;
// const ForgottenPassword = () => { const ForgottenPassword = () => {
// const [state, setState] = useState({ const [state, setState] = useState({
// email: '', email: '',
// redirectUrl: 'http://localhost:3000/passwordreset' redirectUrl: 'http://localhost:3000/passwordreset'
// }); });
// const dispatch = useDispatch(); const dispatch = useDispatch();
// const { loginError, loading } = useSelector(state => state.users); const { loginError, loading } = useSelector(state => state.users);
// console.log(loginError) console.log(loginError)
// const navigate = useNavigate("/") const navigate = useNavigate("/")
// const inputChangeHandler = (e) => { const inputChangeHandler = (e) => {
// const { name, value } = e.target; const { name, value } = e.target;
// setState((prevState) => { setState((prevState) => {
// return { return {
// ...prevState, ...prevState,
// [name]: value [name]: value
// } }
// }); });
// }; };
// const submitHandler = async (e) => { const submitHandler = async (e) => {
// e.preventDefault(); e.preventDefault();
// await dispatch(loginUser(state, navigate)); await dispatch(loginUser(state, navigate));
// }; };
// return <> return <>
// <StyledContainer component={"section"} maxWidth={"xs"}> <StyledContainer component={"section"} maxWidth={"xs"}>
// {!!loginError && <Alert color="error">{loginError}</Alert>} {!!loginError && <Alert color="error">{loginError}</Alert>}
// <Avatar sx={{ m: "0 auto 30px" }}> <Avatar sx={{ m: "0 auto 30px" }}>
// <PersonIcon /> <PersonIcon />
// </Avatar> </Avatar>
// <StyledTitle variant={"h1"}> <StyledTitle variant={"h1"}>
// Password Reset Password Reset
// </StyledTitle> </StyledTitle>
// <UserForm <ResetPasswordForm
// onSubmit={submitHandler} onSubmit={submitHandler}
// state={state} state={state}
// onChange={inputChangeHandler} onChange={inputChangeHandler}
// buttonText={"Sign In"} buttonText={"Submit"}
// resetPassword={"Forgot your password?"} resetPassword={"Forgot your password?"}
// /> />
// </StyledContainer> </StyledContainer>
// <Loader loading={loading} /> <Loader loading={loading} />
// </> </>
// }; };
// export default ForgottenPassword; export default ForgottenPassword;
\ No newline at end of file \ No newline at end of file
...@@ -62,7 +62,7 @@ const Login = () => { ...@@ -62,7 +62,7 @@ const Login = () => {
state={state} state={state}
onChange={inputChangeHandler} onChange={inputChangeHandler}
buttonText={"Sign In"} buttonText={"Sign In"}
// resetPassword={"Forgot your password?"} resetPassword={"Forgot your password?"}
/> />
</StyledContainer> </StyledContainer>
<Loader loading={loading} /> <Loader loading={loading} />
......
...@@ -26,7 +26,7 @@ const Register = () => { ...@@ -26,7 +26,7 @@ const Register = () => {
name: '', name: '',
surname: "", surname: "",
email: "", email: "",
number: "", // number: "",
password: '', password: '',
// avatar: "", // avatar: "",
}); });
...@@ -62,9 +62,10 @@ const Register = () => { ...@@ -62,9 +62,10 @@ const Register = () => {
const formData = new FormData(); const formData = new FormData();
Object.keys(state).forEach(key => { Object.keys(state).forEach(key => {
formData.append(key, state[key]); formData.append(key, state[key]);
console.log("key " + key + "state " + state[key])
}) })
await dispatch(registerUser(formData, navigate)); await dispatch(registerUser(formData, navigate));
await dispatch(loginUser(state, navigate)) // dispatch(loginUser(state, navigate))
}; };
const getFieldError = (fieldname) => { const getFieldError = (fieldname) => {
......
...@@ -18,6 +18,7 @@ export const registerUser = (userData, navigate) => { ...@@ -18,6 +18,7 @@ export const registerUser = (userData, navigate) => {
return async (dispatch) => { return async (dispatch) => {
dispatch(registerUserRequest()); dispatch(registerUserRequest());
try { try {
console.log("register " + userData)
const response = await axios.post("/users", userData); const response = await axios.post("/users", userData);
dispatch(registerUserSuccess()) dispatch(registerUserSuccess())
navigate("/") navigate("/")
...@@ -49,7 +50,25 @@ const logoutUserFailure = (error) => { ...@@ -49,7 +50,25 @@ const logoutUserFailure = (error) => {
export const loginUser = (userData, navigate) => { export const loginUser = (userData, navigate) => {
return async (dispatch) => { return async (dispatch) => {
try { try {
console.log(userData)
const response = await axios.post("users/sessions", userData); const response = await axios.post("users/sessions", userData);
console.log(response)
dispatch(loginUserSuccess(response.data.user));
navigate("/")
} catch (e) {
dispatch(loginUserFailure(e?.response?.data?.err))
}
}
}
export const forgottenPassword = (userData, navigate) => {
return async (dispatch) => {
try {
console.log(userData)
const response = await axios.post("users/requestPasswordReset", userData);
// if (userData.email === response.data.email) {
// }
dispatch(loginUserSuccess(response.data)); dispatch(loginUserSuccess(response.data));
navigate("/") navigate("/")
} catch (e) { } catch (e) {
......
import { REGISTER_USER_REQUEST, REGISTER_USER_SUCCESS, REGISTER_USER_FAILURE, LOGIN_USER_SUCCESS, LOGIN_USER_FAILURE, LOGOUT_USER_SUCCESS } from "../actionTypes/actionTypes"; import { REGISTER_USER_REQUEST, REGISTER_USER_SUCCESS, REGISTER_USER_FAILURE, LOGIN_USER_SUCCESS, LOGIN_USER_FAILURE, LOGOUT_USER_SUCCESS } from "../actionTypes/actionTypes";
const initialState = { const initialState = {
user: { user: null,
name: 'Ivan',
surname: 'Petrov',
email: 'test@gmail.com',
role: 'superuser'
},
registerError: null, registerError: null,
loginError: null, loginError: null,
loading: false loading: false
...@@ -17,12 +12,15 @@ const usersReducer = (state = initialState, action) => { ...@@ -17,12 +12,15 @@ const usersReducer = (state = initialState, action) => {
case REGISTER_USER_REQUEST: case REGISTER_USER_REQUEST:
return {...state, loading: true}; return {...state, loading: true};
case REGISTER_USER_SUCCESS: case REGISTER_USER_SUCCESS:
console.log("register.user " + action)
return {...state, loading: false}; return {...state, loading: false};
case REGISTER_USER_FAILURE: case REGISTER_USER_FAILURE:
return {...state, loading: false, registerError: action.error}; return {...state, loading: false, registerError: action.error};
case LOGIN_USER_SUCCESS: case LOGIN_USER_SUCCESS:
console.log("action.user " + action)
return {...state, user: action.user}; return {...state, user: action.user};
case LOGIN_USER_FAILURE: case LOGIN_USER_FAILURE:
console.log("action.error" + action.error)
return {...state, loginError: action.error}; return {...state, loginError: action.error};
case LOGOUT_USER_SUCCESS: case LOGOUT_USER_SUCCESS:
return {...state, user: null}; return {...state, user: null};
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment