Commit fb57dd85 authored by Rasoul Madani's avatar Rasoul Madani
Browse files

شماره 30

parent 05a602b9
import express from "express";
import User from "../models/userModel.js";
import bcrypt from 'bcryptjs'
import bcrypt from "bcryptjs";
const userRouter = express.Router();
import {generateToken} from '../utils.js'
import { generateToken } from "../utils.js";
userRouter.post("/register", async(req, res) => {
userRouter.post("/register", async (req, res) => {
const user = new User({
name:req.body.name,
email:req.body.email,
password: bcrypt.hashSync(req.body.password,8),
name: req.body.name,
email: req.body.email,
password: bcrypt.hashSync(req.body.password, 8),
});
const createdUser = await user.save()
const createdUser = await user.save();
res.send({
_id:createdUser._id,
name:createdUser.name,
email: createdUser.email,
isAdmin: createdUser.isAdmin,
token: generateToken(createdUser)
})
_id: createdUser._id,
name: createdUser.name,
email: createdUser.email,
isAdmin: createdUser.isAdmin,
token: generateToken(createdUser),
});
});
export default userRouter
\ No newline at end of file
userRouter.post("/signin", async (req, res) => {
try {
const user = await User.findOne({ email: req.body.email });
if (user) {
if (bcrypt.compareSync(req.body.password, user.password)) {
res.send({
_id: user._id,
name: user.name,
email: user.email,
isAdmin: user.isAdmin,
token: generateToken(user),
});
return;
}
}
res.status(404).send({ message: "invalid email or password" });
} catch (error) {
res.send(error);
}
});
export default userRouter;
......@@ -2,7 +2,8 @@ import Axios from "axios";
import {
USER_REGISTER_REQUEST,
USER_REGISTER_SUCCESS,
USER_REGISTER_FAIL
USER_REGISTER_FAIL,
USER_SIGNIN_REQUEST,
} from "../constants/userConstants";
export const register = (name, email, password) => async (dispatch) => {
dispatch({
......@@ -29,3 +30,15 @@ export const register = (name, email, password) => async (dispatch) => {
});
}
};
export const signin = (email, password) => async (dispatch) => {
dispatch({
type: USER_SIGNIN_REQUEST,
});
try {
const { data } = await Axios.post("/api/users/signin", {
email,
password,
});
} catch (error) {}
};
export const USER_REGISTER_REQUEST = "USER_REGISTER_REQUEST";
export const USER_REGISTER_SUCCESS = "USER_REGISTER_SUCCESS";
export const USER_REGISTER_FAIL = "USER_REGISTER_FAIL";
export const USER_SIGNIN_REQUEST = 'USER_SIGNIN_REQUEST'
......@@ -2,6 +2,7 @@ import {
USER_REGISTER_REQUEST,
USER_REGISTER_SUCCESS,
USER_REGISTER_FAIL,
USER_SIGNIN_REQUEST
} from "../constants/userConstants";
export const userRegisterReducer = (state = {}, action) => {
switch (action.type) {
......@@ -15,3 +16,12 @@ export const userRegisterReducer = (state = {}, action) => {
return state;
}
};
export const userSigninReducer = (state={},action)=>{
switch(action.type){
case USER_SIGNIN_REQUEST:
return {loading:true}
default :
return state
}
}
import { useState } from "react";
import { Link } from "react-router-dom";
import { useDispatch } from "react-redux";
import { signin } from "../actions/userActions";
export default function SigninScreen(props) {
const dispatch = useDispatch();
const [email, setEmail] = useState("");
const [password, setPassword] = useState("");
const redirect = props.location.search
? props.location.search.split("=")[1]
: "/";
const submitHandler = (e) => {
return;
e.preventDefault();
dispatch(signin(email, password));
};
return (
<div>
......@@ -44,7 +48,10 @@ export default function SigninScreen(props) {
<div>
<label></label>
<div>
New customer ? <Link to={`/register?redirect=${redirect}`}>Create your account</Link>
New customer ?{" "}
<Link to={`/register?redirect=${redirect}`}>
Create your account
</Link>
</div>
</div>
</form>
......
......@@ -6,7 +6,10 @@ import {
productDetailsReducer,
} from "./reducers/productReducers";
import { cartReducer } from "./reducers/cartReducers";
import { userRegisterReducer } from "./reducers/userReducers";
import {
userRegisterReducer,
userSigninReducer,
} from "./reducers/userReducers";
const initialState = {
userRegister: {
......@@ -26,6 +29,7 @@ const reducer = combineReducers({
productDetails: productDetailsReducer,
cart: cartReducer,
userRegister: userRegisterReducer,
userSignin: userSigninReducer,
});
const composeEnhancer = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || compose;
......
Supports Markdown
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