Skip to content

Commit

Permalink
fix: Login Error (#390)
Browse files Browse the repository at this point in the history
  • Loading branch information
vinayak-vohra authored Jun 28, 2024
1 parent a489719 commit 9c076c2
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 11 deletions.
16 changes: 9 additions & 7 deletions src/backend/auth.api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,18 +143,20 @@ const getUserByUserId = async (userId: string) => {
};

const login = async (email: string, password: string) => {
// Delete current session, if any
try {
await account.deleteSessions();
const response = await account.createEmailPasswordSession(email, password);

if (!response) {
await account.deleteSession("current");
} catch (error: any) {
if (error?.type !== "general_unauthorized_scope") {
throw new Error("Login failed");
}
}

const accountId: string = response && response.userId;

try {
const session = await account.createEmailPasswordSession(email, password);
const { userId: accountId, expire } = session;
const user = await getUserByAccountId(accountId);
return user;
return { user, expires: new Date(expire) };
} catch (error: any) {
console.log(error);
throw new Error(error.message);
Expand Down
8 changes: 4 additions & 4 deletions src/components/pages/auth/login/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ export default function LoginComponent() {
throw new Error("password format not matched");
}

const resp = await login(data.email, data.password);
const { user: resp, expires } = await login(data.email, data.password);

if (resp && resp.email === data.email) {
const payload: userCollectionDB = {
Expand All @@ -78,9 +78,9 @@ export default function LoginComponent() {
$updatedAt: resp.$updatedAt,
};

setCookie(null, "accountId", payload?.accountId);
setCookie(null, "isVerified", String(payload?.isVerified));
setCookie(null, "userId", payload?.$id);
setCookie(null, "accountId", payload?.accountId, { expires });
setCookie(null, "isVerified", String(payload?.isVerified), { expires });
setCookie(null, "userId", payload?.$id, { expires });

dispatch(saveUserToStore(payload));
toastify("Login Successful", "success");
Expand Down

0 comments on commit 9c076c2

Please sign in to comment.