feat: remove appwrite and add nextjs backend
This commit is contained in:
36
frontend/src/services/api.service.ts
Normal file
36
frontend/src/services/api.service.ts
Normal file
@@ -0,0 +1,36 @@
|
||||
import axios, { AxiosError } from "axios";
|
||||
import { getCookie } from "cookies-next";
|
||||
import toast from "../utils/toast.util";
|
||||
|
||||
const api = axios.create({
|
||||
baseURL: "/api",
|
||||
});
|
||||
|
||||
api.interceptors.request.use(
|
||||
(config) => {
|
||||
const accessToken = getCookie("access_token");
|
||||
if (accessToken) {
|
||||
config!.headers!.Authorization = `Bearer ${accessToken}`;
|
||||
}
|
||||
return config;
|
||||
},
|
||||
(error: AxiosError) => {
|
||||
return Promise.reject(error);
|
||||
}
|
||||
);
|
||||
|
||||
api.interceptors.response.use(
|
||||
(response) => {
|
||||
return response;
|
||||
},
|
||||
(error: AxiosError) => {
|
||||
const status = error.response?.status;
|
||||
if (status == 400) {
|
||||
toast.error(error.response?.data?.message ?? "An unkown error occured");
|
||||
}
|
||||
|
||||
return Promise.reject(error);
|
||||
}
|
||||
);
|
||||
|
||||
export default api;
|
||||
41
frontend/src/services/auth.service.ts
Normal file
41
frontend/src/services/auth.service.ts
Normal file
@@ -0,0 +1,41 @@
|
||||
import { getCookie, setCookies } from "cookies-next";
|
||||
import * as jose from "jose";
|
||||
import api from "./api.service";
|
||||
|
||||
const signIn = async (email: string, password: string) => {
|
||||
const response = await api.post("auth/signIn", { email, password });
|
||||
setCookies("access_token", response.data.accessToken);
|
||||
setCookies("refresh_token", response.data.refreshToken);
|
||||
return response;
|
||||
};
|
||||
|
||||
const signUp = async (email: string, password: string) => {
|
||||
return await api.post("auth/signUp", { email, password });
|
||||
};
|
||||
|
||||
const signOut = () => {
|
||||
setCookies("access_token", null);
|
||||
window.location.reload();
|
||||
};
|
||||
|
||||
const refreshAccessToken = async () => {
|
||||
const currentAccessToken = getCookie("access_token") as string;
|
||||
|
||||
if (
|
||||
currentAccessToken &&
|
||||
(jose.decodeJwt(currentAccessToken).exp ?? 0) * 1000 <
|
||||
Date.now() + 2 * 60 * 1000
|
||||
) {
|
||||
const refreshToken = getCookie("refresh_token");
|
||||
|
||||
const response = await api.post("auth/token", { refreshToken });
|
||||
setCookies("access_token", response.data.accessToken);
|
||||
}
|
||||
};
|
||||
|
||||
export default {
|
||||
signIn,
|
||||
signUp,
|
||||
signOut,
|
||||
refreshAccessToken,
|
||||
};
|
||||
88
frontend/src/services/share.service.ts
Normal file
88
frontend/src/services/share.service.ts
Normal file
@@ -0,0 +1,88 @@
|
||||
import {
|
||||
MyShare,
|
||||
Share,
|
||||
ShareMetaData,
|
||||
ShareSecurity,
|
||||
} from "../types/share.type";
|
||||
import api from "./api.service";
|
||||
|
||||
const create = async (
|
||||
id: string,
|
||||
expiration: string,
|
||||
security?: ShareSecurity
|
||||
) => {
|
||||
return (await api.post("shares", { id, expiration, security })).data;
|
||||
};
|
||||
|
||||
const completeShare = async (id: string) => {
|
||||
return (await api.post(`shares/${id}/complete`)).data;
|
||||
};
|
||||
|
||||
const get = async (id: string): Promise<Share> => {
|
||||
const shareToken = localStorage.getItem(`share_${id}_token`);
|
||||
return (
|
||||
await api.get(`shares/${id}`, {
|
||||
headers: { "X-Share-Token": shareToken ?? "" },
|
||||
})
|
||||
).data;
|
||||
};
|
||||
|
||||
const getMetaData = async (id: string): Promise<ShareMetaData> => {
|
||||
const shareToken = localStorage.getItem(`share_${id}_token`);
|
||||
return (
|
||||
await api.get(`shares/${id}/metaData`, {
|
||||
headers: { "X-Share-Token": shareToken ?? "" },
|
||||
})
|
||||
).data;
|
||||
};
|
||||
|
||||
const remove = async (id: string) => {
|
||||
await api.delete(`shares/${id}`);
|
||||
};
|
||||
|
||||
const getMyShares = async (): Promise<MyShare[]> => {
|
||||
return (await api.get("shares")).data;
|
||||
};
|
||||
|
||||
const exchangeSharePasswordWithToken = async (id: string, password: string) => {
|
||||
const { token } = (await api.post(`/shares/${id}/password`, { password }))
|
||||
.data;
|
||||
|
||||
localStorage.setItem(`share_${id}_token`, token);
|
||||
};
|
||||
|
||||
const isShareIdAvailable = async (id: string): Promise<boolean> => {
|
||||
return (await api.get(`shares/isShareIdAvailable/${id}`)).data.isAvailable;
|
||||
};
|
||||
|
||||
const getFileDownloadUrl = async (shareId: string, fileId: string) => {
|
||||
const shareToken = localStorage.getItem(`share_${shareId}_token`);
|
||||
return (
|
||||
await api.get(`shares/${shareId}/files/${fileId}/download`, {
|
||||
headers: { "X-Share-Token": shareToken ?? "" },
|
||||
})
|
||||
).data.url;
|
||||
};
|
||||
|
||||
const downloadFile = async (shareId: string, fileId: string) => {
|
||||
window.location.href = await getFileDownloadUrl(shareId, fileId);
|
||||
};
|
||||
|
||||
const uploadFile = async (shareId: string, file: File) => {
|
||||
var formData = new FormData();
|
||||
formData.append("file", file);
|
||||
return (await api.post(`shares/${shareId}/files`, formData)).data;
|
||||
};
|
||||
|
||||
export default {
|
||||
create,
|
||||
completeShare,
|
||||
exchangeSharePasswordWithToken,
|
||||
get,
|
||||
remove,
|
||||
getMetaData,
|
||||
getMyShares,
|
||||
isShareIdAvailable,
|
||||
downloadFile,
|
||||
uploadFile,
|
||||
};
|
||||
16
frontend/src/services/user.service.ts
Normal file
16
frontend/src/services/user.service.ts
Normal file
@@ -0,0 +1,16 @@
|
||||
import { CurrentUser } from "../types/user.type";
|
||||
import api from "./api.service";
|
||||
import authService from "./auth.service";
|
||||
|
||||
const getCurrentUser = async (): Promise<CurrentUser | null> => {
|
||||
try {
|
||||
await authService.refreshAccessToken();
|
||||
return (await api.get("users/me")).data;
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
};
|
||||
|
||||
export default {
|
||||
getCurrentUser,
|
||||
};
|
||||
Reference in New Issue
Block a user