Skip to content

Commit

Permalink
Add password change thunk to auth store slice #107
Browse files Browse the repository at this point in the history
  • Loading branch information
knightburton committed Jan 27, 2022
1 parent 2205b76 commit f58607a
Showing 1 changed file with 21 additions and 1 deletion.
22 changes: 21 additions & 1 deletion src/store/auth/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
import { createSlice, createAsyncThunk, createSelector, PayloadAction } from '@reduxjs/toolkit';
import { getAuth, signInWithEmailAndPassword, signOut as firebaseSignOut, updateProfile, updateEmail, User as FirebaseUser, AuthError } from 'firebase/auth';
import {
getAuth,
signInWithEmailAndPassword,
signOut as firebaseSignOut,
updateProfile,
updateEmail,
updatePassword,
User as FirebaseUser,
AuthError,
} from 'firebase/auth';
import { getApp } from 'firebase/app';
import { getStorage, ref, uploadBytes, getDownloadURL, list, deleteObject } from 'firebase/storage';
import { InformationForm } from '../../components/pages/Account/Information/PersonalForm';
import { PasswordChangeForm } from '../../components/pages/Account/Security';
import { addAlert } from '../app';
import { SignInCredentials } from '../../interfaces';
import type { RootState } from '../configureStore';
Expand Down Expand Up @@ -134,6 +144,16 @@ export const updateProfileBase = createAsyncThunk<void, InformationForm>('auth/u
}
});

export const changePassword = createAsyncThunk<void, PasswordChangeForm>('auth/changePassword', async ({ confirmPassword }, { dispatch }) => {
try {
const { currentUser } = getAuth();
if (!currentUser) throw new Error('error:auth/user-not-found');
await updatePassword(currentUser, confirmPassword);
} catch (error) {
dispatch(addAlert(error as Error));
}
});

export const authSlice = createSlice({
name: 'auth',
initialState,
Expand Down

0 comments on commit f58607a

Please sign in to comment.