add user to the authenticationProvider
This commit is contained in:
@@ -5,7 +5,7 @@ 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 {Text} from 'react-native-paper';
|
||||
import {Paragraph, Text} from 'react-native-paper';
|
||||
|
||||
import * as WebBrowser from "expo-web-browser";
|
||||
import {useAuth} from "@/src/context/AuthProvider";
|
||||
@@ -13,7 +13,7 @@ import {useAuth} from "@/src/context/AuthProvider";
|
||||
WebBrowser.maybeCompleteAuthSession();
|
||||
|
||||
export default function SettingsScreen() {
|
||||
const {token: jwtToken} = useAuth();
|
||||
const {token: jwtToken, authenticatedUser} = useAuth();
|
||||
const [token, setToken] = useState<Token | null>(null);
|
||||
|
||||
const handleAuthSuccess = (token: Token) => {
|
||||
@@ -21,11 +21,14 @@ export default function SettingsScreen() {
|
||||
console.log('Erhaltener Authentifizierungscode:', token);
|
||||
};
|
||||
|
||||
console.log('authenticatedUser', authenticatedUser)
|
||||
|
||||
return (
|
||||
<ThemedBackground>
|
||||
<ThemedHeader>
|
||||
Settings
|
||||
</ThemedHeader>
|
||||
<Paragraph>Guten Tag, {authenticatedUser?.name}</Paragraph>
|
||||
<ChangePasswordModal/>
|
||||
<ThemeToggleButton/>
|
||||
<SpotifyAuthButton onAuthSuccess={handleAuthSuccess} jwtToken={jwtToken!}/>
|
||||
|
||||
@@ -1,21 +1,29 @@
|
||||
import React, {createContext, useContext, useEffect, useState} from "react";
|
||||
import {getFromStorage, JWT_TOKEN_KEY, removeFromStorage, saveInStorage} from "@/src/utils/secureStorage";
|
||||
import {RestService} from "@/src/services/RestService";
|
||||
import {User} from "@/src/model/User";
|
||||
|
||||
interface AuthError {
|
||||
message: string;
|
||||
id: "username" | "password" | "general";
|
||||
}
|
||||
|
||||
type AuthContextType = {
|
||||
isAuthenticated: boolean | null;
|
||||
token: string | null;
|
||||
login: (username: string, password: string) => Promise<void>;
|
||||
logout: () => Promise<void>;
|
||||
error: { message: string; id: "username" | "password" } | null;
|
||||
error: AuthError | null;
|
||||
authenticatedUser: User | null;
|
||||
};
|
||||
|
||||
const AuthContext = createContext<AuthContextType | undefined>(undefined);
|
||||
|
||||
export const AuthProvider: React.FC<{ children: React.ReactNode }> = ({children}) => {
|
||||
const [isAuthenticated, setIsAuthenticated] = useState<boolean | null>(null);
|
||||
const [authenticatedUser, setAuthenticatedUser] = useState<User | null>(null);
|
||||
const [token, setToken] = useState<string | null>(null);
|
||||
const [error, setError] = useState<{ message: string; id: "username" | "password" } | null>(null);
|
||||
const [error, setError] = useState<AuthError | null>(null);
|
||||
|
||||
useEffect(() => {
|
||||
const checkAuthStatus = async () => {
|
||||
@@ -23,6 +31,7 @@ export const AuthProvider: React.FC<{ children: React.ReactNode }> = ({children}
|
||||
if (storedToken) {
|
||||
setToken(storedToken);
|
||||
setIsAuthenticated(true);
|
||||
await saveUser(storedToken);
|
||||
} else {
|
||||
setIsAuthenticated(false);
|
||||
}
|
||||
@@ -31,6 +40,22 @@ export const AuthProvider: React.FC<{ children: React.ReactNode }> = ({children}
|
||||
checkAuthStatus();
|
||||
}, []);
|
||||
|
||||
const saveUser = async (token: string) => {
|
||||
const user = await RestService.getSelf(token);
|
||||
if (!user) {
|
||||
// token is invalid
|
||||
await removeFromStorage(JWT_TOKEN_KEY);
|
||||
setToken(null);
|
||||
setIsAuthenticated(false);
|
||||
setError({
|
||||
message: "Token invalid",
|
||||
id: "general",
|
||||
});
|
||||
return;
|
||||
}
|
||||
setAuthenticatedUser(user);
|
||||
}
|
||||
|
||||
const login = async (username: string, password: string) => {
|
||||
if (isAuthenticated) {
|
||||
console.log("Already authenticated");
|
||||
@@ -51,6 +76,7 @@ export const AuthProvider: React.FC<{ children: React.ReactNode }> = ({children}
|
||||
setIsAuthenticated(true);
|
||||
// correctly logged in, reset error
|
||||
setError(null);
|
||||
await saveUser(response.token)
|
||||
};
|
||||
|
||||
const logout = async () => {
|
||||
@@ -60,7 +86,7 @@ export const AuthProvider: React.FC<{ children: React.ReactNode }> = ({children}
|
||||
};
|
||||
|
||||
return (
|
||||
<AuthContext.Provider value={{isAuthenticated, token, login, logout, error}}>
|
||||
<AuthContext.Provider value={{isAuthenticated, token, login, logout, error, authenticatedUser}}>
|
||||
{children}
|
||||
</AuthContext.Provider>
|
||||
);
|
||||
|
||||
@@ -62,6 +62,20 @@ const RestService = {
|
||||
}
|
||||
},
|
||||
|
||||
getSelf: async (jwtToken: string) => {
|
||||
try {
|
||||
const response = await axios.get<User>(`${API_URL}/user/me`, {
|
||||
headers: {
|
||||
Authorization: `Bearer ${jwtToken}`,
|
||||
},
|
||||
});
|
||||
return response.data;
|
||||
} catch (error) {
|
||||
console.error("Error fetching self:", error);
|
||||
throw error;
|
||||
}
|
||||
},
|
||||
|
||||
sendPayloadToSocket: async (userId: string, payload: object, jwtToken: string) => {
|
||||
try {
|
||||
const response = await axios.post(
|
||||
|
||||
Reference in New Issue
Block a user