Skip to content

Commit

Permalink
feat: added password reset functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
ndaba1 committed Dec 5, 2023
1 parent 687c1da commit 8e7b518
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/calm-readers-push.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@jumba/amplify-nextjs": patch
---

password reset functionality
49 changes: 49 additions & 0 deletions packages/amplify-nextjs/src/auth.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@ export const AuthContext = createContext<
attributes?: Record<string, string>;
}) => Promise<void>;
signOut: () => Promise<void>;
resetPassword: (data: {
code?: string;
password: string;
attributes?: Record<string, string>;
}) => Promise<void>;
sendPasswordResetCode: (username: string) => Promise<void>;
resendVerificationCode: () => Promise<void>;
confirmVerificationCode: (code: string) => Promise<void>;
})
Expand Down Expand Up @@ -319,6 +325,47 @@ export function AmplifyAuthProvider({
}
}

async function sendPasswordResetCode(username: string) {
try {
await Auth.forgotPassword(username);
setAuth((prev) => ({
...prev,
redirectUrl: `${authLinks.resetPassword}`,
loginCredentials: { username, password: "" },
}));
} catch (e) {
setError(e as Error);
throw e;
}
}

async function resetPassword({
code,
password,
attributes,
}: {
code?: string;
password: string;
attributes?: Record<string, string>;
}) {
try {
if (!code) {
await Auth.completeNewPassword(auth.loginSession, password, attributes);
} else if (!auth.loginCredentials?.username) {
throw new Error("User session is required");
} else {
await Auth.forgotPasswordSubmit(
auth.loginCredentials.username,
code,
password
);
}
} catch (error) {
console.log("Error resetting password", error);
throw error;
}
}

return (
<AuthContext.Provider
value={{
Expand All @@ -331,6 +378,8 @@ export function AmplifyAuthProvider({
inGroup,
signUp,
signOut,
resetPassword,
sendPasswordResetCode,
confirmVerificationCode,
resendVerificationCode,
}}
Expand Down

0 comments on commit 8e7b518

Please sign in to comment.