-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
successfully pulls user chats now.
- Loading branch information
Showing
13 changed files
with
160 additions
and
59 deletions.
There are no files selected for viewing
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
from django.contrib.auth.models import User | ||
from django.conf import settings | ||
from rest_framework import authentication, exceptions | ||
import jwt | ||
|
||
class JWTAuthentication(authentication.BaseAuthentication): | ||
def authenticate(self, request): | ||
auth_header = authentication.get_authorization_header(request).decode('utf-8') | ||
if not auth_header or not auth_header.startswith('Bearer '): | ||
print("No JWT token found in request headers") | ||
return None | ||
|
||
token = auth_header.split(' ')[1] | ||
try: | ||
payload = jwt.decode(token, settings.SUPABASE_SECRET_KEY, algorithms=['HS256'], audience='authenticated') | ||
user_id = payload['sub'] | ||
email = payload.get('email', '') | ||
first_name = payload.get('user_metadata', {}).get('first_name', '') | ||
last_name = payload.get('user_metadata', {}).get('last_name', '') | ||
|
||
# Check if the user exists and update/create accordingly | ||
user, created = User.objects.get_or_create(username=user_id, defaults={ | ||
'first_name': first_name, | ||
'last_name': last_name, | ||
'email': email | ||
}) | ||
|
||
# If the user was not created (i.e., it already exists), update its details | ||
if not created: | ||
user.first_name = first_name | ||
user.last_name = last_name | ||
user.email = email | ||
user.save() | ||
|
||
if created: | ||
print("\nNew user authenticated and created") | ||
else: | ||
print("User authenticated") | ||
|
||
return (user, token) | ||
|
||
except jwt.ExpiredSignatureError: | ||
raise exceptions.AuthenticationFailed('Token expired, login again') | ||
except jwt.InvalidTokenError: | ||
raise exceptions.AuthenticationFailed('Invalid token') | ||
except Exception as e: | ||
raise exceptions.AuthenticationFailed(f'Unexpected error during authentication: {e}') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import React, { createContext, useContext, useState, useEffect } from "react"; | ||
|
||
const AuthContext = createContext(); | ||
|
||
export function AuthProvider({ children }) { | ||
const [token, setToken] = useState(() => { | ||
const storedToken = sessionStorage.getItem('token'); | ||
return storedToken ? JSON.parse(storedToken) : null; | ||
}); | ||
|
||
useEffect(() => { | ||
if (token) { | ||
sessionStorage.setItem('token', JSON.stringify(token)); | ||
} | ||
}, [token]); | ||
|
||
return ( | ||
<AuthContext.Provider value={{ token, setToken }}> | ||
{children} | ||
</AuthContext.Provider> | ||
); | ||
} | ||
|
||
export function useAuth() { | ||
return useContext(AuthContext); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,53 @@ | ||
import React, { useState, useEffect } from 'react'; | ||
import { useAuth } from '../lib/helper/AuthContext'; | ||
import { Link } from 'react-router-dom'; | ||
|
||
function ChatPage() { | ||
const [messages, setMessages] = useState([]); // State to store messages | ||
function Messenger() { | ||
const { token } = useAuth(); | ||
const [chats, setChats] = useState([]); | ||
|
||
|
||
useEffect(() => { | ||
// Fetch existing chats from Supabase and setMessages | ||
}, []); | ||
const fetchChats = async () => { | ||
if (!token || !token.session.access_token) { | ||
console.error('Token not available'); | ||
return; | ||
} | ||
const response = await fetch('/chat/getChats/', { | ||
method: 'GET', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
'Authorization': `Bearer ${token.session.access_token}`, | ||
}, | ||
}); | ||
const data = await response.json(); | ||
if (data && data.chats) { | ||
setChats(data.chats); | ||
} else { | ||
// Handle any errors or empty responses | ||
console.error('Failed to fetch chats or no chats available'); | ||
} | ||
}; | ||
|
||
const sendMessage = (content) => { | ||
// Function to send a message. Implement sending message to Supabase here. | ||
}; | ||
fetchChats(); | ||
}, [token]); | ||
|
||
return ( | ||
<div> | ||
<h1>Chat with a Mental Health Professional</h1> | ||
<h1>Chats with Mental Health Professionals</h1> | ||
<div> | ||
{/* Display messages here */} | ||
{messages.map((message) => ( | ||
<div key={message.message_id}>{message.content}</div> | ||
{chats.map((chat, index) => ( | ||
<div key={index} className='chat-container'> | ||
<p>Chat with: {chat.mhp_id}</p> | ||
<p>Chat ID: {chat.chat_id}</p> | ||
<p>Created At: {chat.created_at}</p> | ||
{/* You can add more details or interaction options like opening the chat */} | ||
</div> | ||
))} | ||
</div> | ||
<input type="text" placeholder="Type a message..." /> | ||
<button onClick={() => sendMessage("Your message content")}>Send</button> | ||
<Link to="/Homepage"> Homepage </Link> | ||
<Link to="/">Back to Homepage</Link> | ||
</div> | ||
); | ||
} | ||
|
||
export default ChatPage; | ||
export default Messenger; |