Skip to content

Commit

Permalink
add navigation in login and register page
Browse files Browse the repository at this point in the history
  • Loading branch information
codingwalebhaiya committed Jul 21, 2024
1 parent 78c90c0 commit 1399a92
Show file tree
Hide file tree
Showing 8 changed files with 23 additions and 14 deletions.
File renamed without changes.
2 changes: 1 addition & 1 deletion backend/src/db/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {DB_NAME} from "../constants.js";
const connectDB = async () => {
try {
const connectionInstance = await mongoose.connect(`${process.env.MONGODB_URI}/${DB_NAME}`)
console.log(`\n MongoDB connected successfully !! DB HOST: ${connectionInstance.connection.host}`);
// console.log(`\n MongoDB connected successfully !! DB HOST: ${connectionInstance.connection.host}`);
} catch (error) {
console.log("MONGODB connection FAILED", error);
process.exit(1)
Expand Down
11 changes: 5 additions & 6 deletions backend/src/index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import connectDB from "./db/index.js";
import dotenv from "dotenv";
import app from "./app.js";
import app from "./app.js";

dotenv.config({
path: "./env",
});
dotenv.config();

connectDB()
.then(() => {
Expand All @@ -18,5 +16,6 @@ connectDB()
});
})
.catch((err) => {
console.error("Error connecting to MongoDB:", err);
});
console.error("Error connecting to MongoDB:", err);
});

4 changes: 2 additions & 2 deletions backend/src/routes/user.routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ import { verifyJWT } from "../middlewares/auth.middleware.js";
const router = Router()

router.route("/register").post(registerUser)
router.route("/login").post(loginUser)
router.route("/login").post(verifyJWT,loginUser)

// secure route
router.route("/logout").post( verifyJWT ,logoutUser)
router.route("/refresh-token").post( refreshAccessToken)


export default router;

Expand Down
9 changes: 7 additions & 2 deletions frontend/src/pages/Login.jsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { useState } from 'react';
import axios from 'axios';
import { useNavigate } from 'react-router-dom';



const Login = () => {
Expand All @@ -11,6 +13,8 @@ const Login = () => {

const [error, setError] = useState(null);
const [success, setSuccess] = useState(null);
const navigate = useNavigate()


const handleChange = (e) => {
setFormData({
Expand All @@ -28,6 +32,7 @@ const Login = () => {
const response = await axios.post('/api/v1/users/login', formData);
setSuccess('Login successful!'); // Show success message
console.log('Login successful:', response.data);
navigate('/')
} catch (error) {
setError('Login failed. Please try again.'); // Show error message
console.error('Error logging in:', error);
Expand Down Expand Up @@ -81,8 +86,8 @@ const Login = () => {
required
/>
</div>
{error && <p className="text-red-500 text-xs italic mb-4">{error}</p>}
{success && <p className="text-green-500 text-xs italic mb-4">{success}</p>}
{error && <p className="text-red-800 text-xl italic mb-4">{error}</p>}
{success && <p className="text-green-800 text-xl italic mb-4">{success}</p>}
<div className="flex items-center justify-between">
<button
type="submit"
Expand Down
7 changes: 5 additions & 2 deletions frontend/src/pages/Register.jsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { useState } from 'react';
import axios from 'axios';
import {useNavigate} from "react-router-dom"

const Register = () => {
const [formData, setFormData] = useState({
Expand All @@ -11,6 +12,7 @@ const Register = () => {

const [error, setError] = useState(null);
const [success, setSuccess] = useState(null);
const navigate = useNavigate()

const handleChange = (e) => {
setFormData({
Expand All @@ -28,6 +30,7 @@ const Register = () => {
const response = await axios.post('/api/v1/users/register', formData);
setSuccess('Registration successful!'); // Show success message
console.log('Form submitted:', response.data);
navigate('/login')
} catch (error) {
setError('Registration failed. Please try again.'); // Show error message
console.error('Error registering:', error);
Expand Down Expand Up @@ -95,8 +98,8 @@ const Register = () => {
required
/>
</div>
{error && <p className="text-red-500 text-xs italic mb-4">{error}</p>}
{success && <p className="text-green-500 text-xs italic mb-4">{success}</p>}
{error && <p className="text-red-800 text-xl italic mb-4">{error}</p>}
{success && <p className="text-green-800 text-xl italic mb-4">{success}</p>}
<div className="flex items-center justify-between">
<button
type="submit"
Expand Down
File renamed without changes.
4 changes: 3 additions & 1 deletion backend/package.json → package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
"type": "module",
"main": "index.js",
"scripts": {
"dev": "nodemon -r dotenv/config --experimental-json-modules src/index.js"
"dev": "nodemon backend/src/index.js",
"start": "node backend/src/index.js"

},
"keywords": [
"javascript"
Expand Down

0 comments on commit 1399a92

Please sign in to comment.