save spotify config
This commit is contained in:
+40
-14
@@ -1,24 +1,50 @@
|
||||
import ThemedButton from "@/src/components/themed/ThemedButton";
|
||||
import ThemedBackground from "@/src/components/themed/ThemedBackground";
|
||||
import {useAuth} from "@/src/context/AuthProvider";
|
||||
import {useRouter} from "expo-router";
|
||||
import ThemedHeader from "@/src/components/themed/ThemedHeader";
|
||||
import React, {useEffect, useState} from "react";
|
||||
import Checkbox from 'expo-checkbox';
|
||||
import {StyleSheet, View} from "react-native";
|
||||
import {useTheme} from "@/src/context/ThemeProvider";
|
||||
import ThemedParagraph from "@/src/components/themed/ThemedParagraph";
|
||||
|
||||
export default function HomeScreen() {
|
||||
const {logout} = useAuth();
|
||||
const router = useRouter();
|
||||
const [idle, setIdle] = useState(false);
|
||||
const {authenticatedUser} = useAuth();
|
||||
const {theme} = useTheme();
|
||||
|
||||
useEffect(() => {
|
||||
setIdle(authenticatedUser?.lastState.global.mode === "idle")
|
||||
}, [])
|
||||
|
||||
return (
|
||||
<ThemedBackground>
|
||||
<ThemedHeader>Welcome to the Home Page!</ThemedHeader>
|
||||
<ThemedButton mode={"outlined"} onPress={() => {
|
||||
console.log("Button pressed");
|
||||
logout().then(() => {
|
||||
router.replace("/login");
|
||||
});
|
||||
}
|
||||
}>
|
||||
Logout
|
||||
</ThemedButton>
|
||||
<ThemedHeader>Willkommen!</ThemedHeader>
|
||||
<View style={styles.section}>
|
||||
<Checkbox
|
||||
style={styles.checkbox}
|
||||
value={idle}
|
||||
onValueChange={setIdle}
|
||||
color={idle ? theme.colors.tertiary : undefined}
|
||||
/>
|
||||
<ThemedParagraph>Energiesparmodus</ThemedParagraph>
|
||||
</View>
|
||||
</ThemedBackground>
|
||||
);
|
||||
}
|
||||
const styles = StyleSheet.create({
|
||||
container: {
|
||||
flex: 1,
|
||||
marginHorizontal: 16,
|
||||
marginVertical: 32,
|
||||
},
|
||||
section: {
|
||||
flexDirection: 'row',
|
||||
alignItems: 'center',
|
||||
},
|
||||
paragraph: {
|
||||
fontSize: 15,
|
||||
},
|
||||
checkbox: {
|
||||
margin: 8,
|
||||
},
|
||||
});
|
||||
|
||||
+28
-12
@@ -1,39 +1,55 @@
|
||||
import ThemedHeader from "@/src/components/themed/ThemedHeader";
|
||||
import React, {useState} from "react";
|
||||
import React from "react";
|
||||
import ThemedBackground from "@/src/components/themed/ThemedBackground";
|
||||
import ChangePasswordModal from "@/src/components/ChangePasswordModal";
|
||||
import ThemeToggleButton from "@/src/components/ThemeToggleButton";
|
||||
import SpotifyAuthButton from "@/src/components/SpotifyAuthButton";
|
||||
import {Token} from "@/src/services/RestService";
|
||||
import {Paragraph, Text} from 'react-native-paper';
|
||||
import {RestService, Token} from "@/src/services/RestService";
|
||||
|
||||
import {useAuth} from "@/src/context/AuthProvider";
|
||||
import {StyleSheet, View} from "react-native";
|
||||
import ThemedButton from "@/src/components/themed/ThemedButton";
|
||||
import {useRouter} from "expo-router";
|
||||
|
||||
export default function SettingsScreen() {
|
||||
const {token: jwtToken, authenticatedUser} = useAuth();
|
||||
const [token, setToken] = useState<Token | null>(null);
|
||||
const {token: jwtToken, authenticatedUser, logout} = useAuth();
|
||||
const router = useRouter();
|
||||
|
||||
const handleAuthSuccess = (token: Token) => {
|
||||
setToken(token);
|
||||
console.log('Erhaltener Authentifizierungscode:', token);
|
||||
const spotifyConfig = {
|
||||
accessToken: token.access_token,
|
||||
refreshToken: token.refresh_token,
|
||||
scope: token.scope,
|
||||
expirationDate: new Date(Date.now() + token.expires_in * 1000),
|
||||
};
|
||||
|
||||
RestService.updateSelfSpotifyConfig(spotifyConfig, jwtToken!).then((result) => {
|
||||
console.log("Spotify Token gespeichert");
|
||||
console.log(result);
|
||||
});
|
||||
};
|
||||
|
||||
return (
|
||||
<ThemedBackground>
|
||||
<ThemedHeader>
|
||||
Settings
|
||||
</ThemedHeader>
|
||||
<View style={styles.container}>
|
||||
<Paragraph>Guten Tag, {authenticatedUser?.name}</Paragraph>
|
||||
<ThemedHeader>Guten Tag, {authenticatedUser?.name}</ThemedHeader>
|
||||
<ChangePasswordModal/>
|
||||
<ThemeToggleButton/>
|
||||
<SpotifyAuthButton
|
||||
onAuthSuccess={handleAuthSuccess}
|
||||
jwtToken={jwtToken!}
|
||||
disabled={authenticatedUser?.spotifyConfig !== null}
|
||||
/>
|
||||
{token && <Text>Erhaltener Code: {token.access_token}</Text>}
|
||||
</View>
|
||||
<ThemedButton mode={"outlined"} onPress={() => {
|
||||
console.log("Button pressed");
|
||||
logout().then(() => {
|
||||
router.replace("/login");
|
||||
});
|
||||
}
|
||||
}>
|
||||
Logout
|
||||
</ThemedButton>
|
||||
</ThemedBackground>
|
||||
);
|
||||
}
|
||||
|
||||
@@ -6,9 +6,10 @@ import ThemedButton from "@/src/components/themed/ThemedButton";
|
||||
interface SpotifyAuthButtonProps {
|
||||
onAuthSuccess: (token: Token) => void;
|
||||
jwtToken: string;
|
||||
disabled: boolean;
|
||||
}
|
||||
|
||||
const SpotifyAuthButton = ({onAuthSuccess, jwtToken}: SpotifyAuthButtonProps) => {
|
||||
const SpotifyAuthButton = ({onAuthSuccess, jwtToken, disabled}: SpotifyAuthButtonProps) => {
|
||||
const {promptAuth, isReady, error} = useSpotifyAuth(onAuthSuccess, jwtToken);
|
||||
|
||||
if (error) {
|
||||
@@ -18,7 +19,9 @@ const SpotifyAuthButton = ({onAuthSuccess, jwtToken}: SpotifyAuthButtonProps) =>
|
||||
return (
|
||||
<ThemedButton
|
||||
onPress={promptAuth}
|
||||
mode={"outlined"}>
|
||||
mode={"outlined"}
|
||||
disabled={disabled}
|
||||
>
|
||||
{isReady ? 'Sign in with Spotify' : 'Loading...'}
|
||||
</ThemedButton>
|
||||
);
|
||||
|
||||
@@ -8,6 +8,7 @@ type Props = {
|
||||
style?: any;
|
||||
children: React.ReactNode;
|
||||
onPress: () => void;
|
||||
disabled?: boolean;
|
||||
}
|
||||
|
||||
export default function ThemedButton({mode, style, ...props}: Props) {
|
||||
|
||||
+20
-21
@@ -1,6 +1,6 @@
|
||||
import axios from 'axios';
|
||||
import {makeRedirectUri} from "expo-auth-session";
|
||||
import {User} from "@/src/model/User";
|
||||
import {SpotifyConfig, User} from "@/src/model/User";
|
||||
|
||||
const API_URL = process.env.EXPO_PUBLIC_API_URL;
|
||||
|
||||
@@ -139,26 +139,25 @@ const RestService = {
|
||||
}
|
||||
},
|
||||
|
||||
updateUser:
|
||||
async (user: User, jwtToken: string) => {
|
||||
const {_id, ...rest} = user;
|
||||
try {
|
||||
const response = await axios.put<User>(
|
||||
`${API_URL}/user/${_id}`,
|
||||
rest,
|
||||
{
|
||||
headers: {
|
||||
Authorization: `Bearer ${jwtToken}`,
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
}
|
||||
);
|
||||
return response.data;
|
||||
} catch (error) {
|
||||
console.error("Error updating user:", error);
|
||||
throw error;
|
||||
}
|
||||
},
|
||||
updateSelfSpotifyConfig: async (spotifyConfig: SpotifyConfig, jwtToken: string) => {
|
||||
try {
|
||||
const response = await axios.put<{ result: { success: boolean, message: string } }>(
|
||||
`${API_URL}/user/me/spotify`,
|
||||
spotifyConfig,
|
||||
{
|
||||
headers: {
|
||||
Authorization: `Bearer ${jwtToken}`,
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
}
|
||||
);
|
||||
return response.data;
|
||||
} catch (error) {
|
||||
console.error("Error updating self:", error);
|
||||
throw error;
|
||||
}
|
||||
},
|
||||
|
||||
|
||||
login: async (username: string, password: string) => {
|
||||
const response = await axios.post<{
|
||||
|
||||
Reference in New Issue
Block a user