Skip to content

Commit

Permalink
force user to verify email when using the application
Browse files Browse the repository at this point in the history
  • Loading branch information
liberty-rising committed Dec 30, 2023
1 parent e4a79f1 commit c62e9ec
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 2 deletions.
1 change: 1 addition & 0 deletions backend/envs/dev/initialization/setup_dev_environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ def create_admin_user():
email="[email protected]",
organization_id=1,
role="admin",
email_verified=True,
)

with DatabaseManager() as session:
Expand Down
5 changes: 5 additions & 0 deletions backend/routes/user_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,3 +196,8 @@ async def verify_email(request: VerifyEmailRequest):
user_manager.update_user_email_verified(username=user.username)

return {"message": f"Successfully verified email for {user.email}."}


@user_router.get("/users/is-email-verified/")
async def is_user_verified(current_user: User = Depends(get_current_user)):
return {"email_verified": current_user.email_verified}
7 changes: 6 additions & 1 deletion frontend/src/components/Auth/RequireAuth.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Navigate } from 'react-router-dom';
import { useAuth } from '../../contexts/AuthContext';

function RequireAuth({ children }) {
const { isAuthenticated, isLoading } = useAuth();
const { isAuthenticated, isLoading, isEmailVerified } = useAuth();

if (isLoading) {
return <div>Loading...</div>; // Or your preferred loading indicator/component
Expand All @@ -13,6 +13,11 @@ function RequireAuth({ children }) {
return <Navigate to="/login" />;
}

if (!isEmailVerified) {
// Redirect to the verify-email page if email is not verified
return <Navigate to="/verify-email" state={{ email: '[email protected]' }} />;
}

return children;
}

Expand Down
7 changes: 6 additions & 1 deletion frontend/src/contexts/AuthContext.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import React, { createContext, useContext, useEffect, useState } from 'react';
import axios from 'axios';
import { API_URL } from '../utils/constants';
import { set } from 'date-fns';

//Contexts in React are used for passing data deeply through the component tree without having to pass props down manually at every level
export const AuthContext = createContext();
Expand All @@ -14,6 +15,7 @@ export const useAuth = () => useContext(AuthContext);
export const AuthProvider = ({ children }) => {
// State for keeping track of whether the user is authenticated.
const [isAuthenticated, setIsAuthenticated] = useState(false);
const [isEmailVerified, setIsEmailVerified] = useState(false); // Add a state for email verification
const [isLoading, setIsLoading] = useState(true); // Add a loading state

useEffect(() => {
Expand All @@ -24,6 +26,9 @@ export const AuthProvider = ({ children }) => {
// Update based on the response message
if (response.data.message === "User is authenticated") {
setIsAuthenticated(true);

const emailResponse = await axios.get(`${API_URL}users/is-email-verified/`);
setIsEmailVerified(emailResponse.data.email_verified);
} else {
setIsAuthenticated(false);
}
Expand All @@ -50,7 +55,7 @@ export const AuthProvider = ({ children }) => {
// The Provider component from our created context is used here.
// It makes the `isAuthenticated` state and `updateAuth` function available to any descendants of this component
return (
<AuthContext.Provider value={{ isAuthenticated, updateAuth, isLoading }}>
<AuthContext.Provider value={{ isAuthenticated, updateAuth, isEmailVerified, isLoading }}>
{children}
</AuthContext.Provider>
);
Expand Down

0 comments on commit c62e9ec

Please sign in to comment.