Skip to content

Commit 526eb10

Browse files
Ryan J. Petersonprescottprue
Ryan J. Peterson
authored andcommitted
confirmPasswordReset action (prescottprue#74)
* Adds `confirmPasswordReset` action * Adds tests for `confirmPasswordReset` action * Fixes typo in `test` npm script
1 parent 6219748 commit 526eb10

File tree

5 files changed

+78
-4
lines changed

5 files changed

+78
-4
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"clean": "rimraf dist",
1111
"lint": "eslint src/** test/**",
1212
"lint:fix": "npm run lint -- --fix",
13-
"test": "mocha -R spec --compilers js:babel-core/register ./tests/setup.js ./test/**/*.spec.js",
13+
"test": "mocha -R spec --compilers js:babel-core/register ./tests/setup.js ./tests/**/*.spec.js",
1414
"test:cov": "istanbul cover ./node_modules/mocha/bin/_mocha -- ./tests/** --recursive --report lcov --compilers js:babel-register --require babel-polyfill",
1515
"codecov": "cat coverage/*/lcov.info | codecov",
1616
"build:commonjs": "cross-env BABEL_ENV=commonjs babel src --out-dir lib",

src/actions/auth.js

+42-1
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,46 @@ export const resetPassword = (dispatch, firebase, email) => {
388388
})
389389
}
390390

391+
/**
392+
* @description Confirm the password reset with code and password
393+
* @param {Function} dispatch - Action dispatch function
394+
* @param {Object} firebase - Internal firebase object
395+
* @param {String} code - Email confirmation reset code
396+
* @param {String} password - Password to set it to
397+
* @return {Promise}
398+
* @private
399+
*/
400+
export const confirmPasswordReset = (dispatch, firebase, code, password) => {
401+
dispatchLoginError(dispatch, null)
402+
return firebase.auth()
403+
.confirmPasswordReset(code, password)
404+
.catch((err) => {
405+
if (err) {
406+
switch (err.code) {
407+
case 'auth/expired-action-code':
408+
dispatchLoginError(dispatch, new Error('The action code has expired.'))
409+
break
410+
case 'auth/invalid-action-code':
411+
dispatchLoginError(dispatch, new Error('The action code is invalid.'))
412+
break
413+
case 'auth/user-disabled':
414+
dispatchLoginError(dispatch, new Error('The user is disabled.'))
415+
break
416+
case 'auth/user-not-found':
417+
dispatchLoginError(dispatch, new Error('The user is not found.'))
418+
break
419+
case 'auth/weak-password':
420+
dispatchLoginError(dispatch, new Error('The password is not strong enough.'))
421+
break
422+
default:
423+
dispatchLoginError(dispatch, err)
424+
}
425+
return Promise.reject(err)
426+
}
427+
})
428+
}
429+
430+
391431
export default {
392432
dispatchLoginError,
393433
dispatchUnauthorizedError,
@@ -399,5 +439,6 @@ export default {
399439
login,
400440
logout,
401441
createUser,
402-
resetPassword
442+
resetPassword,
443+
confirmPasswordReset
403444
}

src/compose.js

+4
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,9 @@ export default (fbConfig, otherConfig) => next =>
141141
const resetPassword = (credentials) =>
142142
authActions.resetPassword(dispatch, firebase, credentials)
143143

144+
const confirmPasswordReset = (code, password) =>
145+
authActions.confirmPasswordReset(dispatch, firebase, code, password)
146+
144147
firebase.helpers = {
145148
ref: path => Firebase.database().ref(path),
146149
set,
@@ -155,6 +158,7 @@ export default (fbConfig, otherConfig) => next =>
155158
deleteFile,
156159
createUser,
157160
resetPassword,
161+
confirmPasswordReset,
158162
watchEvent,
159163
unWatchEvent,
160164
storage: () => Firebase.storage()

tests/unit/actions/auth.spec.js

+22-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ import {
1010
login,
1111
logout,
1212
createUser,
13-
resetPassword
13+
resetPassword,
14+
confirmPasswordReset,
1415
} from '../../../src/actions/auth'
1516
import { promisesForPopulate } from '../../../src/utils/populate'
1617

@@ -69,7 +70,11 @@ const fakeFirebase = {
6970
? Promise.reject({code: 'auth/user-not-found'})
7071
: email === 'error2'
7172
? Promise.reject({code: 'asdfasdf'})
72-
: Promise.resolve({some: 'val'})
73+
: Promise.resolve({some: 'val'}),
74+
confirmPasswordReset: (code, password) =>
75+
password === 'error'
76+
? Promise.reject({code: code})
77+
: Promise.resolve()
7378
})
7479
}
7580

@@ -283,4 +288,19 @@ describe('Actions: Auth', () => {
283288
})
284289
})
285290
})
291+
292+
describe('confirmPasswordReset', () => {
293+
it('resets password for real user', () => {
294+
return confirmPasswordReset(dispatch, fakeFirebase, 'test', 'test')
295+
.then((err) => {
296+
expect(err).to.be.undefined
297+
})
298+
})
299+
it('dispatches for all other errors', () => {
300+
return confirmPasswordReset(dispatch, fakeFirebase, 'auth/user-not-found', 'error')
301+
.catch((err) => {
302+
expect(err.code).to.be.a.string
303+
})
304+
})
305+
})
286306
})

tests/unit/compose.spec.js

+9
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,15 @@ describe('Compose', () => {
108108
expect(err).to.be.an.object
109109
}
110110
})
111+
112+
describe('confirmPasswordReset', () => {
113+
try {
114+
helpers.confirmPasswordReset({ code: 'test', password: 'test' })
115+
} catch (err) {
116+
expect(err).to.be.an.object
117+
}
118+
})
119+
111120
describe('storage', () => {
112121
try {
113122
helpers.storage()

0 commit comments

Comments
 (0)