chore: services, type and helper function added

This commit is contained in:
Anmol Singh Bhatia 2023-12-01 19:04:25 +05:30
parent 586cff8744
commit a0e18e191f
5 changed files with 82 additions and 1 deletions

View File

@ -29,3 +29,21 @@ export const copyTextToClipboard = async (text: string) => {
}
await navigator.clipboard.writeText(text);
};
/**
* @returns {boolean} true if email is valid, false otherwise
* @description Returns true if email is valid, false otherwise
* @param {string} email string to check if it is a valid email
* @example checkEmailIsValid("hello world") => false
* @example checkEmailIsValid("example@plane.so") => true
*/
export const checkEmailValidity = (email: string): boolean => {
if (!email) return false;
const isEmailValid =
/^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/.test(
email
);
return isEmailValid;
};

View File

@ -1,12 +1,51 @@
// services
import APIService from "services/api.service";
import { API_BASE_URL } from "helpers/common.helper";
import { IEmailCheckData, ILoginTokenResponse, IPasswordSignInData } from "types/auth";
class AuthService extends APIService {
constructor() {
super(API_BASE_URL);
}
async emailCheck(data: IEmailCheckData): Promise<{
is_password_autoset: boolean;
}> {
return this.post("/api/email-check/", data, { headers: {} })
.then((response) => response?.data)
.catch((error) => {
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(`/api/forgot-password/`, data)
.then((response) => response?.data)
.catch((error) => {
throw error?.response;
});
}
async setPassword(data: { password: string }): Promise<any> {
return this.post(`/api/users/me/set-password/`, data)
.then((response) => response?.data)
.catch((error) => {
throw error?.response?.data;
});
}
async emailLogin(data: any) {
return this.post("/api/sign-in/", data, { headers: {} })
.then((response) => {

View File

@ -1,5 +1,6 @@
// services
import APIService from "services/api.service";
// helpers
import { API_BASE_URL } from "helpers/common.helper";
class UserService extends APIService {

View File

@ -9,7 +9,7 @@ export interface IUserStore {
loader: boolean;
error: any | null;
currentUser: any | null;
fetchCurrentUser: () => void;
fetchCurrentUser: () => Promise<IUser>;
currentActor: () => any;
}
@ -89,6 +89,7 @@ class UserStore implements IUserStore {
this.currentUser = response;
});
}
return response;
} catch (error) {
console.error("Failed to fetch current user", error);
this.loader = false;

22
space/types/auth.ts Normal file
View File

@ -0,0 +1,22 @@
export type TEmailCheckTypes = "magic_code" | "password";
export interface IEmailCheckData {
email: string;
type: TEmailCheckTypes;
}
export interface ILoginTokenResponse {
access_token: string;
refresh_toke: string;
}
export interface IMagicSignInData {
email: string;
key: string;
token: string;
}
export interface IPasswordSignInData {
email: string;
password: string;
}