diff --git a/space/helpers/string.helper.ts b/space/helpers/string.helper.ts index 1b676ca57..4a265ba4e 100644 --- a/space/helpers/string.helper.ts +++ b/space/helpers/string.helper.ts @@ -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; +}; diff --git a/space/services/authentication.service.ts b/space/services/authentication.service.ts index 4d861994f..4befe0c33 100644 --- a/space/services/authentication.service.ts +++ b/space/services/authentication.service.ts @@ -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 { + 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 { + return this.post(`/api/forgot-password/`, data) + .then((response) => response?.data) + .catch((error) => { + throw error?.response; + }); + } + + async setPassword(data: { password: string }): Promise { + 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) => { diff --git a/space/services/user.service.ts b/space/services/user.service.ts index 21e9f941e..1d6e4146e 100644 --- a/space/services/user.service.ts +++ b/space/services/user.service.ts @@ -1,5 +1,6 @@ // services import APIService from "services/api.service"; +// helpers import { API_BASE_URL } from "helpers/common.helper"; class UserService extends APIService { diff --git a/space/store/user.ts b/space/store/user.ts index e2b6428ef..fd9096b3e 100644 --- a/space/store/user.ts +++ b/space/store/user.ts @@ -9,7 +9,7 @@ export interface IUserStore { loader: boolean; error: any | null; currentUser: any | null; - fetchCurrentUser: () => void; + fetchCurrentUser: () => Promise; 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; diff --git a/space/types/auth.ts b/space/types/auth.ts new file mode 100644 index 000000000..15853802c --- /dev/null +++ b/space/types/auth.ts @@ -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; +}