Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Login Error #390

Merged
merged 3 commits into from
Jun 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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) };
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this will expire immediately, you need to add some buffer time. I prefer to add 2 days

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would like to know why would this expire immediately?

As per the Appwrite docs, the session object contains an expire property which is a date string. This string is converted to a date object here because it is required as such by nookies.

Also, the cookie expire time is set to be in sync with the appwrite session validation (1 year by default), that can be changed.

Cookie expire time (matches appwrite session length):
image

Appwrite console to set session length (auth > security):
image
Changing this to 2 days would set the cookie expire time to 2 days for users.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let me test that out locally, and then finalize the PR

} 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
Loading