mirror of
https://github.com/makeplane/plane
synced 2024-06-14 14:31:34 +00:00
chore: updated api service and handled the set password in store
This commit is contained in:
parent
71d0355132
commit
9e60d2c40b
@ -19,7 +19,7 @@ export abstract class APIService {
|
||||
(response) => response,
|
||||
(error) => {
|
||||
if (error.response && error.response.status === 401) window.location.href = "/login";
|
||||
return Promise.reject(error.response?.data ?? error);
|
||||
return Promise.reject(error);
|
||||
}
|
||||
);
|
||||
}
|
||||
|
@ -20,7 +20,7 @@ abstract class APIService {
|
||||
(response) => response,
|
||||
(error) => {
|
||||
if (error.response && error.response.status === 401) window.location.href = "/";
|
||||
return Promise.reject(error.response?.data ?? error);
|
||||
return Promise.reject(error);
|
||||
}
|
||||
);
|
||||
}
|
||||
|
@ -56,8 +56,8 @@ const SetPasswordPage: NextPageWithLayout = observer(() => {
|
||||
const [isPasswordInputFocused, setIsPasswordInputFocused] = useState(false);
|
||||
// hooks
|
||||
const { resolvedTheme } = useTheme();
|
||||
|
||||
const { data: user } = useUser();
|
||||
// hooks
|
||||
const { data: user, handleSetPassword } = useUser();
|
||||
|
||||
useEffect(() => {
|
||||
if (csrfToken === undefined)
|
||||
@ -77,15 +77,12 @@ const SetPasswordPage: NextPageWithLayout = observer(() => {
|
||||
[passwordFormData]
|
||||
);
|
||||
|
||||
const handleSetPassword = async (password: string) => {
|
||||
if (!csrfToken) throw new Error("csrf token not found");
|
||||
await authService.setPassword(csrfToken, { password });
|
||||
};
|
||||
|
||||
const handleSubmit = async (e: FormEvent<HTMLFormElement>) => {
|
||||
try {
|
||||
e.preventDefault();
|
||||
await handleSetPassword(passwordFormData.password);
|
||||
if (!csrfToken) throw new Error("csrf token not found");
|
||||
await handleSetPassword(csrfToken, { password: passwordFormData.password });
|
||||
router.push("/");
|
||||
} catch (err: any) {
|
||||
setToast({
|
||||
type: TOAST_TYPE.ERROR,
|
||||
|
@ -1,107 +1,51 @@
|
||||
import axios from "axios";
|
||||
import Cookies from "js-cookie";
|
||||
/* eslint-disable @typescript-eslint/no-explicit-any */
|
||||
import axios, { AxiosInstance } from "axios";
|
||||
|
||||
export abstract class APIService {
|
||||
protected baseURL: string;
|
||||
protected headers: any = {};
|
||||
private axiosInstance: AxiosInstance;
|
||||
|
||||
constructor(baseURL: string) {
|
||||
this.baseURL = baseURL;
|
||||
}
|
||||
|
||||
setCSRFToken(token: string) {
|
||||
Cookies.set("csrf_token", token, { expires: 30 });
|
||||
}
|
||||
|
||||
getCSRFToken() {
|
||||
return Cookies.get("csrf_token");
|
||||
}
|
||||
|
||||
setRefreshToken(token: string) {
|
||||
Cookies.set("refresh_token", token, { expires: 30 });
|
||||
}
|
||||
|
||||
getRefreshToken() {
|
||||
return Cookies.get("refresh_token");
|
||||
}
|
||||
|
||||
purgeRefreshToken() {
|
||||
Cookies.remove("refresh_token", { path: "/" });
|
||||
}
|
||||
|
||||
setAccessToken(token: string) {
|
||||
Cookies.set("access_token", token, { expires: 30 });
|
||||
}
|
||||
|
||||
getAccessToken() {
|
||||
return Cookies.get("access_token");
|
||||
}
|
||||
|
||||
purgeAccessToken() {
|
||||
Cookies.remove("access_token", { path: "/" });
|
||||
}
|
||||
|
||||
getHeaders() {
|
||||
return {
|
||||
Authorization: `Bearer ${this.getAccessToken()}`,
|
||||
};
|
||||
}
|
||||
|
||||
get(url: string, config = {}): Promise<any> {
|
||||
return axios({
|
||||
method: "get",
|
||||
url: this.baseURL + url,
|
||||
headers: this.getAccessToken() ? this.getHeaders() : {},
|
||||
...config,
|
||||
this.axiosInstance = axios.create({
|
||||
baseURL,
|
||||
withCredentials: true,
|
||||
});
|
||||
|
||||
this.setupInterceptors();
|
||||
}
|
||||
|
||||
post(url: string, data = {}, config = {}): Promise<any> {
|
||||
return axios({
|
||||
method: "post",
|
||||
url: this.baseURL + url,
|
||||
data,
|
||||
headers: this.getAccessToken() ? this.getHeaders() : {},
|
||||
...config,
|
||||
withCredentials: true,
|
||||
});
|
||||
private setupInterceptors() {
|
||||
this.axiosInstance.interceptors.response.use(
|
||||
(response) => response,
|
||||
(error) => {
|
||||
if (error.response && error.response.status === 401) window.location.href = "/accounts/sign-in";
|
||||
return Promise.reject(error);
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
put(url: string, data = {}, config = {}): Promise<any> {
|
||||
return axios({
|
||||
method: "put",
|
||||
url: this.baseURL + url,
|
||||
data,
|
||||
headers: this.getAccessToken() ? this.getHeaders() : {},
|
||||
...config,
|
||||
withCredentials: true,
|
||||
});
|
||||
get(url: string, params = {}) {
|
||||
return this.axiosInstance.get(url, { params });
|
||||
}
|
||||
|
||||
patch(url: string, data = {}, config = {}): Promise<any> {
|
||||
return axios({
|
||||
method: "patch",
|
||||
url: this.baseURL + url,
|
||||
data,
|
||||
headers: this.getAccessToken() ? this.getHeaders() : {},
|
||||
...config,
|
||||
withCredentials: true,
|
||||
});
|
||||
post(url: string, data: any, config = {}) {
|
||||
return this.axiosInstance.post(url, data, config);
|
||||
}
|
||||
|
||||
delete(url: string, data?: any, config = {}): Promise<any> {
|
||||
return axios({
|
||||
method: "delete",
|
||||
url: this.baseURL + url,
|
||||
data: data,
|
||||
headers: this.getAccessToken() ? this.getHeaders() : {},
|
||||
...config,
|
||||
withCredentials: true,
|
||||
});
|
||||
put(url: string, data: any, config = {}) {
|
||||
return this.axiosInstance.put(url, data, config);
|
||||
}
|
||||
|
||||
patch(url: string, data: any, config = {}) {
|
||||
return this.axiosInstance.patch(url, data, config);
|
||||
}
|
||||
|
||||
delete(url: string, data?: any, config = {}) {
|
||||
return this.axiosInstance.delete(url, { data, ...config });
|
||||
}
|
||||
|
||||
request(config = {}) {
|
||||
return axios(config);
|
||||
return this.axiosInstance(config);
|
||||
}
|
||||
}
|
||||
|
@ -1,12 +1,5 @@
|
||||
// types
|
||||
import {
|
||||
ICsrfTokenData,
|
||||
IEmailCheckData,
|
||||
IEmailCheckResponse,
|
||||
ILoginTokenResponse,
|
||||
IMagicSignInData,
|
||||
IPasswordSignInData,
|
||||
} from "@plane/types";
|
||||
import { ICsrfTokenData, IEmailCheckData, IEmailCheckResponse } from "@plane/types";
|
||||
// helpers
|
||||
import { API_BASE_URL } from "@/helpers/common.helper";
|
||||
// services
|
||||
@ -39,18 +32,6 @@ export class AuthService extends APIService {
|
||||
throw error?.response?.data;
|
||||
});
|
||||
|
||||
async passwordSignIn(data: IPasswordSignInData): Promise<ILoginTokenResponse> {
|
||||
return this.post("/api/sign-in/", data, { headers: {} })
|
||||
.then((response) => {
|
||||
this.setAccessToken(response?.data?.access_token);
|
||||
this.setRefreshToken(response?.data?.refresh_token);
|
||||
return response?.data;
|
||||
})
|
||||
.catch((error) => {
|
||||
throw error?.response?.data;
|
||||
});
|
||||
}
|
||||
|
||||
async sendResetPasswordLink(data: { email: string }): Promise<any> {
|
||||
return this.post(`/auth/forgot-password/`, data)
|
||||
.then((response) => response?.data)
|
||||
@ -71,50 +52,6 @@ export class AuthService extends APIService {
|
||||
});
|
||||
}
|
||||
|
||||
async resetPassword(
|
||||
uidb64: string,
|
||||
token: string,
|
||||
data: {
|
||||
new_password: string;
|
||||
}
|
||||
): Promise<ILoginTokenResponse> {
|
||||
return this.post(`/api/reset-password/${uidb64}/${token}/`, data, { headers: {} })
|
||||
.then((response) => {
|
||||
if (response?.status === 200) {
|
||||
this.setAccessToken(response?.data?.access_token);
|
||||
this.setRefreshToken(response?.data?.refresh_token);
|
||||
return response?.data;
|
||||
}
|
||||
})
|
||||
.catch((error) => {
|
||||
throw error?.response?.data;
|
||||
});
|
||||
}
|
||||
|
||||
async emailSignUp(data: { email: string; password: string }): Promise<ILoginTokenResponse> {
|
||||
return this.post("/api/sign-up/", data, { headers: {} })
|
||||
.then((response) => {
|
||||
this.setAccessToken(response?.data?.access_token);
|
||||
this.setRefreshToken(response?.data?.refresh_token);
|
||||
return response?.data;
|
||||
})
|
||||
.catch((error) => {
|
||||
throw error?.response?.data;
|
||||
});
|
||||
}
|
||||
|
||||
async socialAuth(data: any): Promise<ILoginTokenResponse> {
|
||||
return this.post("/api/social-auth/", data, { headers: {} })
|
||||
.then((response) => {
|
||||
this.setAccessToken(response?.data?.access_token);
|
||||
this.setRefreshToken(response?.data?.refresh_token);
|
||||
return response?.data;
|
||||
})
|
||||
.catch((error) => {
|
||||
throw error?.response?.data;
|
||||
});
|
||||
}
|
||||
|
||||
async generateUniqueCode(data: { email: string }): Promise<any> {
|
||||
return this.post("/auth/magic-generate/", data, { headers: {} })
|
||||
.then((response) => response?.data)
|
||||
@ -123,34 +60,6 @@ export class AuthService extends APIService {
|
||||
});
|
||||
}
|
||||
|
||||
async magicSignIn(data: IMagicSignInData): Promise<any> {
|
||||
return await this.post("/api/magic-sign-in/", data, { headers: {} })
|
||||
.then((response) => {
|
||||
if (response?.status === 200) {
|
||||
this.setAccessToken(response?.data?.access_token);
|
||||
this.setRefreshToken(response?.data?.refresh_token);
|
||||
return response?.data;
|
||||
}
|
||||
})
|
||||
.catch((error) => {
|
||||
throw error?.response?.data;
|
||||
});
|
||||
}
|
||||
|
||||
async instanceAdminSignIn(data: IPasswordSignInData): Promise<ILoginTokenResponse> {
|
||||
return await this.post("/api/instances/admins/sign-in/", data, { headers: {} })
|
||||
.then((response) => {
|
||||
if (response?.status === 200) {
|
||||
this.setAccessToken(response?.data?.access_token);
|
||||
this.setRefreshToken(response?.data?.refresh_token);
|
||||
return response?.data;
|
||||
}
|
||||
})
|
||||
.catch((error) => {
|
||||
throw error?.response?.data;
|
||||
});
|
||||
}
|
||||
|
||||
async signOut(baseUrl: string): Promise<any> {
|
||||
await this.requestCSRFToken().then((data) => {
|
||||
const csrfToken = data?.csrf_token;
|
||||
|
@ -1,3 +1,4 @@
|
||||
import cloneDeep from "lodash/cloneDeep";
|
||||
import set from "lodash/set";
|
||||
import { action, makeObservable, observable, runInAction } from "mobx";
|
||||
// types
|
||||
@ -33,6 +34,7 @@ export interface IUserStore {
|
||||
// actions
|
||||
fetchCurrentUser: () => Promise<IUser | undefined>;
|
||||
updateCurrentUser: (data: Partial<IUser>) => Promise<IUser | undefined>;
|
||||
handleSetPassword: (csrfToken: string, data: { password: string }) => Promise<IUser | undefined>;
|
||||
deactivateAccount: () => Promise<void>;
|
||||
signOut: () => Promise<void>;
|
||||
}
|
||||
@ -75,6 +77,7 @@ export class UserStore implements IUserStore {
|
||||
// actions
|
||||
fetchCurrentUser: action,
|
||||
updateCurrentUser: action,
|
||||
handleSetPassword: action,
|
||||
deactivateAccount: action,
|
||||
signOut: action,
|
||||
});
|
||||
@ -153,6 +156,32 @@ export class UserStore implements IUserStore {
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* @description update the user password
|
||||
* @param data
|
||||
* @returns {Promise<IUser>}
|
||||
*/
|
||||
handleSetPassword = async (csrfToken: string, data: { password: string }): Promise<IUser | undefined> => {
|
||||
const currentUserData = cloneDeep(this.data);
|
||||
try {
|
||||
if (currentUserData && currentUserData.is_password_autoset && this.data) {
|
||||
const user = await this.authService.setPassword(csrfToken, { password: data.password });
|
||||
set(this.data, ["is_password_autoset"], false);
|
||||
return user;
|
||||
}
|
||||
return undefined;
|
||||
} catch (error) {
|
||||
if (this.data) set(this.data, ["is_password_autoset"], true);
|
||||
runInAction(() => {
|
||||
this.error = {
|
||||
status: "user-update-error",
|
||||
message: "Failed to update current user",
|
||||
};
|
||||
});
|
||||
throw error;
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* @description deactivates the current user
|
||||
* @returns {Promise<void>}
|
||||
|
Loading…
Reference in New Issue
Block a user