add tanstack/react-query
This commit is contained in:
+13
-14
@@ -1,4 +1,5 @@
|
||||
'use server'
|
||||
import { getSessionData } from '@/app/lib/session';
|
||||
|
||||
const FRUITS_URL = 'https://www.fruityvice.com/api/fruit/';
|
||||
const ALL = 'all'
|
||||
@@ -25,26 +26,24 @@ export async function fetchFruit(id: number) {
|
||||
}
|
||||
}
|
||||
|
||||
export async function testConnect() {
|
||||
export async function getUserData() {
|
||||
const userEmail = await getSessionData('email');
|
||||
const token = await getSessionData('token');
|
||||
|
||||
try {
|
||||
const response = await fetch('http://localhost:8080/api/auth/register', {
|
||||
method: 'POST',
|
||||
body: JSON.stringify({
|
||||
"fullName": "Vladislav Sergevich",
|
||||
"email": "s2@e.ru",
|
||||
"password": "1121231412"
|
||||
}),
|
||||
const response = await fetch(`http://localhost/api/user?email=${userEmail}`, {
|
||||
method: 'GET',
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
"Accept": "application/json"
|
||||
"Accept": "application/json",
|
||||
"Authorization": `Bearer ${token}`,
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
let parsed = await response.json();
|
||||
|
||||
return parsed;
|
||||
if (response.ok) {
|
||||
return await response.json();
|
||||
}
|
||||
} catch (error) {
|
||||
throw error;
|
||||
console.error('error');
|
||||
}
|
||||
}
|
||||
@@ -25,9 +25,13 @@ export async function decrypt(session: string | undefined = '') {
|
||||
}
|
||||
}
|
||||
|
||||
export async function createSession(token: string) {
|
||||
export async function createSession(token: string, email: string) {
|
||||
const expiresAt = new Date(Date.now() + 7 * 24 * 60 * 60 * 1000);
|
||||
const session = await encrypt({ token, expiresAt });
|
||||
const session = await encrypt({
|
||||
token,
|
||||
email,
|
||||
expiresAt
|
||||
});
|
||||
const cookieStore = await cookies();
|
||||
|
||||
cookieStore.set('session', session, {
|
||||
@@ -39,7 +43,7 @@ export async function createSession(token: string) {
|
||||
})
|
||||
}
|
||||
|
||||
export async function getSessionToken(): Promise<string | null> {
|
||||
export async function getSessionData(type: 'token' | 'email'): Promise<string | null> {
|
||||
const cookieStore = await cookies();
|
||||
const sessionCookie = cookieStore.get('session')?.value;
|
||||
|
||||
@@ -49,15 +53,14 @@ export async function getSessionToken(): Promise<string | null> {
|
||||
|
||||
const payload = await decrypt(sessionCookie);
|
||||
|
||||
if (payload && typeof payload === 'object' && 'token' in payload) {
|
||||
return payload.token as string;
|
||||
if (payload && typeof payload === 'object' && type in payload) {
|
||||
return payload[type] as string;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
|
||||
export async function deleteSession() {
|
||||
const cookieStore = await cookies()
|
||||
cookieStore.delete('session')
|
||||
|
||||
Reference in New Issue
Block a user