Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion client/src/App.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { BrowserRouter, Routes, Route } from "react-router-dom";
import "./App.css";

import axios from "axios";
axios.defaults.baseURL = 'http://localhost:4000/api';
axios.defaults.baseURL = 'https://ignite-mm4z.onrender.com/api/';

import Register from "./pages/auth/Register";
import Login from "./pages/auth/Login";
Expand Down
4 changes: 2 additions & 2 deletions client/src/components/newsletter/SinglePost.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ function SinglePost({ data, email, mainId, date }) {
{/* <h4 id="title">{data.title}</h4> */}
<div className="img-wrapper">
<img
src={`http://localhost:4000/${data.img}`}
alt=""
src="https://firebasestorage.googleapis.com/v0/b/mohamedhaliths.appspot.com/o/assets%2Fimgs%2FNewsletter%20App.png?alt=media&token=cea32243-e52d-44cc-ac7a-e7a7fc85f398"
alt="Sample Post Image"
className="card-img-post"
width={500}
height={380}
Expand Down
102 changes: 60 additions & 42 deletions client/src/pages/auth/Login.jsx
Original file line number Diff line number Diff line change
@@ -1,68 +1,86 @@
import React, { useEffect, useState } from 'react'
import Inputs from "../../components/auth/Inputs"
import { Link, useNavigate } from 'react-router-dom';
import axios from 'axios';
import { toast } from 'wc-toast';
import Cookies from 'js-cookie';
import React, { useEffect, useState } from "react";
import Inputs from "../../components/auth/Inputs";
import { Link, useNavigate } from "react-router-dom";
import axios from "axios";
import { toast } from "wc-toast";
import Cookies from "js-cookie";

function Login() {
const [email,setEmail] = useState("");
const [password,setPassword] = useState("");
const [email, setEmail] = useState("");
const [password, setPassword] = useState("");

const nav = useNavigate();

useEffect(() => {
const token = Cookies.get('token');
if (token){
nav('/');
const token = Cookies.get("token");
if (token) {
nav("/");
return;
}
},[]);
}, []);

axios.defaults.withCredentials = true;


const handleSubmit = async (e) => {
e.preventDefault();
const tLoader = toast.loading('Authenticating');
const tLoader = toast.loading("Authenticating");

try {
const result = await axios.post('auth/login', {email, password});
console.log(result.data);
toast.dismiss(tLoader);
toast.success('Login Successful');

nav('/');
const result = await axios.post("/auth/login", { email, password });
if (result) {
toast.dismiss(tLoader);
toast.success("Login Successful");
Cookies.set("token", result.data.token);
nav("/");
}
} catch (error) {
console.log(error);
toast.dismiss(tLoader);
toast.error(error.response.data);
console.error(error.response.data);
}
}
};

return (
<div className="registerPg">
<div className="mainLayout">
<div className="d-flex justify-content-between">

<div className="info col-5">
<h1 className="mb-4"> Sign In</h1>
<form onSubmit={handleSubmit}>
<Inputs value={email} label={'Email'} type={'mail'} placeholder={'example@email.com'} onChange={(e) => setEmail(e.target.value)} />
<Inputs value={password} label={'Password'} type={'password'} placeholder={'xxxxyyyyyzzz'} onChange={(e) => setPassword(e.target.value)} />
<button className="auth-btn my-4" type="submit">Sign In</button>
</form>
<h6>Don't have an account? <span className="primary-color"><Link className='ref' to='/sign-up'>Sign Up</Link></span></h6>
</div>

<div className="img col-6 loginWidth">

</div>
</div>
return (
<div className="registerPg">
<div className="mainLayout">
<div className="d-flex justify-content-between">
<div className="info col-5">
<h1 className="mb-4"> Sign In</h1>
<form onSubmit={handleSubmit}>
<Inputs
value={email}
label={"Email"}
type={"mail"}
placeholder={"example@email.com"}
onChange={(e) => setEmail(e.target.value)}
/>
<Inputs
value={password}
label={"Password"}
type={"password"}
placeholder={"xxxxyyyyyzzz"}
onChange={(e) => setPassword(e.target.value)}
/>
<button className="auth-btn my-4" type="submit">
Sign In
</button>
</form>
<h6>
Don't have an account?{" "}
<span className="primary-color">
<Link className="ref" to="/sign-up">
Sign Up
</Link>
</span>
</h6>
</div>

<div className="img col-6 loginWidth"></div>
</div>
);
</div>
</div>
);
}

export default Login
export default Login;
2 changes: 1 addition & 1 deletion client/src/pages/auth/Register.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ function Register() {
e.preventDefault();
const tLoader = toast.loading('Creating User...')
try{
const result = await axios.post('auth/register', {name,email,password});
const result = await axios.post('/auth/register', {name,email,password});
console.log(result);
toast.dismiss(tLoader);
toast.success('Registration successful...');
Expand Down
5 changes: 2 additions & 3 deletions server/controllers/Authentication.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@ const login = async (req, res) => {
department }, key, {
expiresIn: "1d",
});

res.status(200).cookie("token", token).send("Login Successful");
res.status(200).send({token: token});
} else {
res.status(500).send("The Password is incorrect");
}
Expand All @@ -44,7 +43,7 @@ const login = async (req, res) => {
}
};

const loginVerify = async (req, res, next) => {
const loginVerify = async (req, res) => {
const token = req.headers["x-access-token"];
if (!token) {
return res.status(401).send("Unauthorized: Token not provided");
Expand Down
2 changes: 1 addition & 1 deletion server/routes/Auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ router.post('/register', register);
router.post('/login',login);

//auth-login-verify
router.get('/login', verifyUser,loginVerify);
router.get('/login', verifyUser, loginVerify);


module.exports = router;
2 changes: 1 addition & 1 deletion server/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const app = express();
app.use(express.json());
app.use(
cors({
origin: "http://localhost:5173",
origin: "https://dynamic-newsletter-platform.onrender.com",
methods: ["GET", "POST", "PATCH"],
credentials: true,
})
Expand Down