Skip to content

Commit

Permalink
fix email verification bug
Browse files Browse the repository at this point in the history
  • Loading branch information
liberty-rising committed Jan 13, 2024
1 parent 833e0b0 commit 3eb8851
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 25 deletions.
13 changes: 10 additions & 3 deletions backend/llms/prompt_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,16 @@ def jpg_data_extraction_prompt(self, instructions: str):
Example output:
{{
"client_name":"John Doe",
"invoice_amount":"1000",
"date":"01-01-2021"
{{
"client_name":"John Doe",
"invoice_amount":"1000",
"date":"01-01-2021"
}},
{{
"client_name":"Jane Doe",
"invoice_amount":"2000",
"date":"01-01-2021"
}}
}}
In this example, the requested information would have been client name, invoice amount, and date.
Expand Down
27 changes: 15 additions & 12 deletions backend/routes/auth_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ async def login_for_access_token(
detail="Incorrect username or password",
headers={"WWW-Authenticate": "Bearer"},
)

if remember:
access_token = create_token(
{"sub": user.username},
Expand All @@ -85,7 +86,8 @@ async def login_for_access_token(
)
else:
access_token = create_token(
{"sub": user.username}, timedelta(minutes=ACCESS_TOKEN_EXPIRE_MINUTES)
{"sub": user.username},
timedelta(minutes=ACCESS_TOKEN_EXPIRE_MINUTES),
)
refresh_token = create_token(
{"sub": user.username}, timedelta(days=REFRESH_TOKEN_EXPIRE_DAYS)
Expand Down Expand Up @@ -125,7 +127,8 @@ async def refresh_access_token(
)

access_token = create_token(
{"sub": user.username}, timedelta(minutes=ACCESS_TOKEN_EXPIRE_MINUTES)
{"sub": user.username},
timedelta(minutes=ACCESS_TOKEN_EXPIRE_MINUTES),
)
new_refresh_token = create_token(
{"sub": user.username}, timedelta(days=REFRESH_TOKEN_EXPIRE_DAYS)
Expand Down Expand Up @@ -198,25 +201,25 @@ async def register(response: Response, user: UserCreate):
user_manager.create_user(db_user)

# Generate access token
access_token_expires = timedelta(minutes=ACCESS_TOKEN_EXPIRE_MINUTES)
access_token = create_token(
data={"sub": user.username}, expires_delta=access_token_expires
{"sub": user.username},
timedelta(minutes=ACCESS_TOKEN_EXPIRE_MINUTES),
)
refresh_token = create_token(
{"sub": user.username}, timedelta(days=REFRESH_TOKEN_EXPIRE_DAYS)
)

# Set cookie
response.set_cookie(
key="access_token",
value=f"Bearer {access_token}",
httponly=True,
max_age=1800,
secure=True,
samesite="lax",
update_user_refresh_token(
user_id=user.id,
refresh_token=refresh_token,
)

set_tokens_in_cookies(response, access_token, refresh_token)
return {"message": "Registration successful"}


@auth_router.post("/logout/", response_model=LogoutResponse)
async def logout(response: Response):
response.delete_cookie(key="access_token")
response.delete_cookie(key="refresh_token")
return {"message": "Logged out successfully"}
6 changes: 1 addition & 5 deletions frontend/src/App.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,7 @@ function App() {
path="/"
element={<LandingLayout><LandingPage /></LandingLayout>}
/>
<Route
path="/login"
element={isAuthenticated ? <Navigate to ="/dashboards" /> :
<LandingLayout><LoginPage /></LandingLayout>}
/>
<Route path="/login" element={<LandingLayout><LoginPage /></LandingLayout>} />
<Route path="/change-password" element={<RequireAuth><LandingLayout><ChangePasswordPage /></LandingLayout></RequireAuth>} />
<Route path="/reset-password" element={<LandingLayout><ResetPasswordPage /></LandingLayout>} />
<Route path="/forgot-password" element={<LandingLayout><ForgotPasswordPage /></LandingLayout>} />
Expand Down
1 change: 1 addition & 0 deletions frontend/src/components/auth/RequireAuth.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ function RequireAuth({ children }) {
}

if (!isEmailVerified) {
console.log("from require auth")
// Redirect to the verify-email page if email is not verified
return <Navigate to="/verify-email" />;
}
Expand Down
6 changes: 5 additions & 1 deletion frontend/src/contexts/AuthContext.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,14 @@ export const AuthProvider = ({ children }) => {
setIsAuthenticated(newAuthState);
};

const updateEmailVerification = (newEmailVerificationState) => {
setIsEmailVerified(newEmailVerificationState);
};

// 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, isEmailVerified, setIsEmailVerified, isLoading }}>
<AuthContext.Provider value={{ isAuthenticated, updateAuth, isEmailVerified, updateEmailVerification, isLoading }}>
{children}
</AuthContext.Provider>
);
Expand Down
14 changes: 10 additions & 4 deletions frontend/src/pages/login/LoginPage.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useState } from 'react';
import React, { useEffect, useState } from 'react';
import axios from 'axios';
import qs from 'qs';
import { Box, Button, Checkbox, Container, FormControlLabel, TextField, Typography } from '@mui/material';
Expand All @@ -14,17 +14,18 @@ function LoginPage({ onLogin }) {
const [password, setPassword] = useState('');
const [rememberMe, setRememberMe] = useState(false);
const navigate = useNavigate();
const { updateAuth } = useAuth();
const { updateAuth, updateEmailVerification } = useAuth();
const [errorMessage, setErrorMessage] = useState('');


const handleSubmit = async (event) => {
event.preventDefault();

// Determine if usernameOrEmail should be sent as username or email
const isEmail = validator.isEmail(usernameOrEmail);
const data = isEmail
? { email: usernameOrEmail, password, remember: rememberMe }
: { username: usernameOrEmail, password, rememer: rememberMe };
: { username: usernameOrEmail, password, remember: rememberMe };

try {
const response = await axios.post(`${API_URL}token/`, qs.stringify (data), {
Expand All @@ -42,8 +43,13 @@ function LoginPage({ onLogin }) {
});
if (userResponse.data.requires_password_update) {
navigate('/change-password');
} else if (userResponse.data.email_verified == false) {
console.log("email_verified is false")
navigate('/verify-email');
} else {
navigate('/dashboards');
console.log("email_verified is true")
updateEmailVerification(true);
navigate('/dashboards')
}
}
} catch (error) {
Expand Down

0 comments on commit 3eb8851

Please sign in to comment.