Skip to content

Commit

Permalink
Merge branch 'main' into lk_branch
Browse files Browse the repository at this point in the history
  • Loading branch information
ArslanKamchybekov authored Oct 12, 2024
2 parents 16a6526 + 15ed587 commit a8c1346
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 24 deletions.
54 changes: 43 additions & 11 deletions backend/src/auth/auth.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,10 @@ export class AuthController {
template: 'activation.ejs',
data: { activationUrl },
});

res.status(201).json({ message: 'User created. Check your email to activate your account',
activationUrl });
res.status(201).json({
message: 'User created. Check your email to activate your account',
activationUrl
});
} catch (error: any) {
console.error('Error sending email:', error);
next(error);
Expand All @@ -50,17 +51,11 @@ export class AuthController {
return { token, code };
}

// Generate reset password token
async generateResetPasswordToken(user: any) {
const token = jwt.sign(user, process.env.JWT_SECRET, { expiresIn: '1h' });
return { token };
}

// Account activation
@Post('activate')
async activate(@Req() req: Request, @Res() res: Response) {
const { token, code } = req.body;

try {
const user: any = jwt.verify(token, process.env.JWT_SECRET);

Expand Down Expand Up @@ -91,9 +86,46 @@ export class AuthController {
}
}

/**
* Generates a JWT for a user.
*
* @param user **Any** *(supposed to be a User)*
* @param expiration **string** *Numerical value (ie: '10m' for 10 minutes; '1h' for 1 hr)*
* @returns **JSON object** *with signed JWT*
*/
async generateToken(user: any) {
const token = jwt.sign({ user: user }, process.env.JWT_SECRET, { expiresIn: "1h" });
return { token };
}

// https://supertokens.com/blog/implementing-a-forgot-password-flow
// https://cheatsheetseries.owasp.org/cheatsheets/Forgot_Password_Cheat_Sheet.html
@Post('forgot-password')
async forgotPassword(@Req() req: Request, @Res() res: Response) {

const { email } = req.body;
const user = await User.findOne({ email });

if (!user) return res.status(400).json({ message: 'User not found' });

const resetToken = await this.generateToken(user)
const resetLink = `${process.env.CLIENT_URL}/reset-password?token=${resetToken.token}`;
const template = 'reset-password.ejs';

try {
await sendEmail({
email: user.email,
subject: 'Reset Password',
template,
data: { resetLink },
});
res.status(200).json({
success: true,
message: 'Reset password link sent to email',
resetToken: resetToken.token,
});
}catch (error: any) {
return res.status(500).json({ message: error.message });
}
}

@Post('reset-password')
Expand Down
53 changes: 40 additions & 13 deletions frontend/src/components/Navbar.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Image from "next/image";
import Link from "next/link";
import React, { useState } from "react";
import React, { useState, useEffect } from "react";
import styles from "../styles/Navbar.module.css";
import { useRouter } from "next/router";
import logo from '../../public/logo.png';
Expand All @@ -10,10 +10,28 @@ export default function Navbar() {
const [isSignedIn, setIsSignedIn] = useState(false);
const [isMenuOpen, setIsMenuOpen] = useState(false);

// Mock function to simulate user authentication check (you can replace this with your real logic)
const checkUserAuth = () => {
return true
};

// useEffect to check if the user is signed in when the component mounts
useEffect(() => {
const userSignedIn = checkUserAuth();
setIsSignedIn(userSignedIn);
}, []);

const toggleMenu = () => {
setIsMenuOpen(!isMenuOpen);
};

const handleSignOut = () => {
// Clear authentication token (or other methods of sign out)
localStorage.removeItem("authToken");
setIsSignedIn(false);
router.push("/signin"); // Redirect to the sign-in page
};

return (
<nav className={styles.navbar}>
<div className={styles.container}>
Expand All @@ -35,21 +53,30 @@ export default function Navbar() {

{/* Navigation Links */}
<ul className={`${styles.navLinks} ${isMenuOpen ? styles.active : ''}`}>
<li className={router.pathname === '/' ? styles.active : ''}>
<Link href="/">Home</Link>
</li>
<li className={router.pathname === '/about' ? styles.active : ''}>
<Link href="/about">About Us</Link>
</li>
<li className={router.pathname === '/signup' ? styles.active : ''}>
<Link href="/signup">Sign Up</Link>
</li>
<li className={router.pathname === '/signin' ? styles.active : ''}>
<Link href="/signin">Sign In</Link>
</li>
<li className={router.pathname === '/profile' ? styles.active : ''}>
<Link href="/profile">Profile</Link>
</li>
{/* Conditionally render based on authentication status */}
{isSignedIn ? (
<>
<li className={router.pathname === '/profile' ? styles.active : ''}>
<Link href="/profile">Profile</Link>
</li>
<li>
<button className="text-red-500"
onClick={handleSignOut}>Sign Out</button>
</li>
</>
) : (
<>
<li className={router.pathname === '/signup' ? styles.active : ''}>
<Link href="/signup">Sign Up</Link>
</li>
<li className={router.pathname === '/signin' ? styles.active : ''}>
<Link href="/signin">Sign In</Link>
</li>
</>
)}
</ul>
</div>
</nav>
Expand Down

0 comments on commit a8c1346

Please sign in to comment.