diff --git a/backend/app/api/auth.py b/backend/app/api/auth.py index 1970087..2dc66d6 100644 --- a/backend/app/api/auth.py +++ b/backend/app/api/auth.py @@ -77,7 +77,7 @@ async def login(data: dict, session: Session = Depends(get_session)): if not user or not verify_password(data.get("password"), user.hashed_password): raise HTTPException(status_code=401, detail="Invalid credentials") - return {"access_token": create_access_token({"sub": user.username}), "token_type": "bearer"} + return {"access_token": create_access_token({"sub": user.username}), "token_type": "bearer", "role": user.role} @router.put("/change-password") async def change_password(data: PasswordChange, user=Depends(get_current_user), session: Session = Depends(get_session)): diff --git a/frontend/src/navigation/AppNavigator.tsx b/frontend/src/navigation/AppNavigator.tsx index 769fc56..a828a4d 100644 --- a/frontend/src/navigation/AppNavigator.tsx +++ b/frontend/src/navigation/AppNavigator.tsx @@ -23,7 +23,7 @@ export default function AppNavigator() { const token = useAuthStore((state) => state.token); const linking = { - prefixes: ['http://localhost:8081'], + prefixes: ['http://localhost:8081', 'http://127.0.0.1:8081'], config: { screens: { Login: '', diff --git a/frontend/src/screens/AdminPanelScreen.tsx b/frontend/src/screens/AdminPanelScreen.tsx index 4983172..2a1e6d2 100644 --- a/frontend/src/screens/AdminPanelScreen.tsx +++ b/frontend/src/screens/AdminPanelScreen.tsx @@ -1,19 +1,20 @@ import React, { useEffect, useState } from 'react'; -import { View, Text, FlatList, TouchableOpacity, StyleSheet, Alert } from 'react-native'; +import { View, Text, FlatList, TouchableOpacity, StyleSheet, Alert, ScrollView } from 'react-native'; import api from '../api/client'; import { useAuthStore } from '../store/authStore'; export default function AdminPanelScreen() { const user = useAuthStore((state) => state.user); const [users, setUsers] = useState([]); + const [pendingSongs, setPendingSongs] = useState([]); useEffect(() => { fetchUsers(); + fetchPendingSongs(); }, []); const fetchUsers = async () => { try { - // Assuming there's an endpoint /admin/users as per plan const response = await api.get('/admin/users'); setUsers(response.data); } catch (error) { @@ -21,6 +22,15 @@ export default function AdminPanelScreen() { } }; + const fetchPendingSongs = async () => { + try { + const response = await api.get('/admin/moderation'); + setPendingSongs(response.data); + } catch (error) { + console.error('Error fetching pending songs:', error); + } + }; + const deleteUser = async (userId: number) => { Alert.alert('Confirm', 'Are you sure you want to delete this user?', [ { text: 'Cancel', style: 'cancel' }, @@ -40,6 +50,21 @@ export default function AdminPanelScreen() { ]); }; + const handleSongAction = async (songId: number, action: 'approve' | 'reject') => { + try { + if (action === 'approve') { + await api.post(`/admin/moderation/${songId}/approve`); + Alert.alert('Success', 'Song approved'); + } else { + await api.delete(`/admin/moderation/${songId}/reject`); + Alert.alert('Success', 'Song rejected'); + } + fetchPendingSongs(); + } catch (error) { + Alert.alert('Error', `Failed to ${action} song`); + } + }; + if (user?.role !== 'admin') { return ( @@ -48,34 +73,65 @@ export default function AdminPanelScreen() { ); } - const renderItem = ({ item }) => ( - + const renderUserItem = ({ item }) => ( + - {item.username} + {item.username} {item.email} - {item.role} - deleteUser(item.id)} style={styles.deleteButton}> + deleteUser(item.id)} style={[styles.actionButton, styles.deleteButton]}> Delete ); + const renderSongItem = ({ item }) => ( + + + {item.title} + {item.genre} | Author: {item.author_id} + + + handleSongAction(item.id, 'approve')} style={[styles.actionButton, styles.approveButton]}> + Approve + + handleSongAction(item.id, 'reject')} style={[styles.actionButton, styles.deleteButton]}> + Reject + + + + ); + return ( - + Admin Moderation + + Pending Songs ({pendingSongs.length}) + item.id.toString()} + renderItem={renderSongItem} + scrollEnabled={false} + /> + + + + User Management item.id.toString()} - renderItem={renderItem} + renderItem={renderUserItem} + scrollEnabled={false} /> - + ); } const styles = StyleSheet.create({ - container: { flex: 1, backgroundColor: '#f5f5f5', padding: 20 }, + container: { flexGrow: 1, backgroundColor: '#f5f5f5', padding: 20 }, title: { fontSize: 24, fontWeight: 'bold', marginBottom: 20, textAlign: 'center' }, - userItem: { + sectionTitle: { fontSize: 20, fontWeight: 'bold', marginTop: 20, marginBottom: 10 }, + item: { flexDirection: 'row', justifyContent: 'space-between', alignItems: 'center', @@ -86,7 +142,9 @@ const styles = StyleSheet.create({ borderWidth: 1, borderColor: '#ddd' }, - userName: { fontSize: 16, fontWeight: 'bold' }, - deleteButton: { backgroundColor: 'red', padding: 8, borderRadius: 5 }, - errorText: { textAlign: 'center', marginTop: 50, color: 'red', fontSize: 18 } + itemName: { fontSize: 16, fontWeight: 'bold' }, + actionButton: { padding: 8, borderRadius: 5, marginLeft: 5 }, + approveButton: { backgroundColor: 'green' }, + deleteButton: { backgroundColor: 'red' }, + errorText: { textAlign: 'center', marginTop: 50, color: 'red', fontSize: 18 }, }); diff --git a/frontend/src/screens/LoginScreen.tsx b/frontend/src/screens/LoginScreen.tsx index 3076edd..35f20a9 100644 --- a/frontend/src/screens/LoginScreen.tsx +++ b/frontend/src/screens/LoginScreen.tsx @@ -16,8 +16,19 @@ export default function LoginScreen({ navigation }) { password, }); - setAuth({ username }, response.data.access_token); - Alert.alert('Success', 'Logged in successfully!'); + setAuth({ username, role: response.data.role }, response.data.access_token); + console.log('User role from API:', response.data.role); + + // Small delay to allow AppNavigator to switch stacks based on the new token + setTimeout(() => { + if (response.data.role === 'admin') { + navigation.navigate('AdminPanel'); + } else if (response.data.role === 'author') { + navigation.navigate('Upload'); + } else { + navigation.navigate('Discovery'); + } + }, 100); } catch (error) { Alert.alert('Error', 'Login failed'); } @@ -51,4 +62,4 @@ const styles = StyleSheet.create({ container: { flex: 1, justifyContent: 'center', padding: 20 }, title: { fontSize: 24, fontWeight: 'bold', textAlign: 'center', marginBottom: 20 }, input: { borderWidth: 1, borderColor: '#ccc', padding: 10, marginBottom: 10, borderRadius: 5 }, -}); +}); \ No newline at end of file diff --git a/frontend/src/screens/SettingsScreen.tsx b/frontend/src/screens/SettingsScreen.tsx index 1215722..293c529 100644 --- a/frontend/src/screens/SettingsScreen.tsx +++ b/frontend/src/screens/SettingsScreen.tsx @@ -5,15 +5,22 @@ import { useAuthStore } from '../store/authStore'; export default function SettingsScreen({ navigation }) { const [oldPassword, setOldPassword] = useState(''); - const { user } = useAuthStore(); + const [newPassword, setNewPassword] = useState(''); + const { user, logout } = useAuthStore(); const handlePasswordChange = async () => { + if (!oldPassword || !newPassword) { + Alert.alert('Error', 'Please provide both old and new passwords'); + return; + } try { await api.put('/auth/change-password', { old_password: oldPassword, - new_password: 'newpassword123', // In real UI this would be an input + new_password: newPassword, }); Alert.alert('Success', 'Password changed successfully!'); + setOldPassword(''); + setNewPassword(''); } catch (error) { Alert.alert('Error', 'Failed to change password'); } @@ -35,8 +42,21 @@ export default function SettingsScreen({ navigation }) { onChangeText={setOldPassword} secureTextEntry /> - {/* New password input would go here */} +