Skip to content

Version v1.3.2 #75

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

Merged
merged 4 commits into from
Mar 10, 2017
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
3 changes: 1 addition & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ addons:
repo_token: $CODE_CLIMATE

before_install:
- "npm install react firebase lodash redux react-redux"
- "npm install react redux react-redux"

after_success:
- npm install -g codeclimate-test-reporter
Expand All @@ -35,5 +35,4 @@ deploy:
email: $NPM_EMAIL
api_key: $NPM_TOKEN
on:
tags: true
branch: master
36 changes: 35 additions & 1 deletion docs/auth.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ Similar to Firebase's `ref.createUser(credentials)` but with support for automat
* `credentials` [**Object**](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)
* `credentials.email` [**String**](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String) - User's email
* `credentials.password` [**String**](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String) - User's password
* `credentials.signIn` [**String**](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String) - Whether or not to sign in when user is signing up (defaults to `true`)

* `profile` [**Object**](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)
* `profile.username` [**String**](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)
Expand Down Expand Up @@ -160,7 +161,7 @@ firebase.logout()
```

## resetPassword(credentials)
Calls Firebase's `ref.resetPassword(credentials)` then adds the output into redux state under `state.firebase.authError`
Calls Firebase's `firebase.auth().resetPassword()`. If there is an error, it is added into redux state under `state.firebase.authError`, which can be loaded using `pathToJS(state.firebase, 'authError')`.

##### Examples

Expand All @@ -179,3 +180,36 @@ firebase.resetPassword({
##### Returns
[**Promise**](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise) with user's UID in case of success or the error otherwise.
Always authenticate the new user in case of success

## confirmPasswordReset(code, newPassword)
Calls Firebase's `firebase.auth().confirmPasswordReset()`. If there is an error, it is added into redux state under `state.firebase.authError`, which can be loaded using `pathToJS(state.firebase, 'authError')`.

##### Examples

```js
firebase.confirmPasswordReset('some reset code', 'myNewPassword')
```

##### Parameters
* `code` [**String**](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object) - Password reset code
* `newPassword` [**String**](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object) - New password to set for user

##### Returns
[**Promise**](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise)

## verifyPasswordResetCode(code)
Verify a password reset code from password reset email.

Calls Firebase's `firebase.auth().verifyPasswordResetCode()`. If there is an error, it is added into redux state under `state.firebase.authError`, which can be loaded using `pathToJS(state.firebase, 'authError')`.

##### Examples

```js
firebase.verifyPasswordResetCode('some reset code')
```

##### Parameters
* `code` [**String**](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object) - Password reset code

##### Returns
[**Promise**](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise) - Email associated with reset code
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-redux-firebase",
"version": "1.3.1",
"version": "1.3.2",
"description": "Redux integration for Firebase. Comes with a Higher Order Component for use with React.",
"browser": "dist/react-redux-firebase.js",
"main": "lib/index.js",
Expand Down
30 changes: 26 additions & 4 deletions src/actions/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -338,11 +338,13 @@ export const createUser = (dispatch, firebase, { email, password, signIn }, prof
return firebase.auth()
.createUserWithEmailAndPassword(email, password)
.then((userData) =>
// Login to newly created account if signIn flag is true
// Login to newly created account if signIn flag is not set to false
firebase.auth().currentUser || (!!signIn && signIn === false)
? createUserProfile(dispatch, firebase, userData, profile)
? createUserProfile(dispatch, firebase, userData, profile || { email })
: login(dispatch, firebase, { email, password })
.then(() => createUserProfile(dispatch, firebase, userData, profile || { email }))
.then(() =>
createUserProfile(dispatch, firebase, userData, profile || { email })
)
.catch(err => {
if (err) {
switch (err.code) {
Expand Down Expand Up @@ -427,6 +429,25 @@ export const confirmPasswordReset = (dispatch, firebase, code, password) => {
})
}

/**
* @description Verify that password reset code is valid
* @param {Function} dispatch - Action dispatch function
* @param {Object} firebase - Internal firebase object
* @param {String} code - Password reset code
* @return {Promise} email - Email associated with reset code
* @private
*/
export const verifyPasswordResetCode = (dispatch, firebase, code) => {
dispatchLoginError(dispatch, null)
return firebase.auth()
.verifyPasswordResetCode(code)
.catch((err) => {
if (err) {
dispatchLoginError(dispatch, err)
}
return Promise.reject(err)
})
}

export default {
dispatchLoginError,
Expand All @@ -440,5 +461,6 @@ export default {
logout,
createUser,
resetPassword,
confirmPasswordReset
confirmPasswordReset,
verifyPasswordResetCode
}
4 changes: 4 additions & 0 deletions src/compose.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,9 @@ export default (fbConfig, otherConfig) => next =>
const confirmPasswordReset = (code, password) =>
authActions.confirmPasswordReset(dispatch, firebase, code, password)

const verifyPasswordResetCode = (code) =>
authActions.verifyPasswordResetCode(dispatch, firebase, code)

firebase.helpers = {
ref: path => Firebase.database().ref(path),
set,
Expand All @@ -159,6 +162,7 @@ export default (fbConfig, otherConfig) => next =>
createUser,
resetPassword,
confirmPasswordReset,
verifyPasswordResetCode,
watchEvent,
unWatchEvent,
storage: () => Firebase.storage()
Expand Down
84 changes: 69 additions & 15 deletions tests/unit/actions/auth.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
createUser,
resetPassword,
confirmPasswordReset,
verifyPasswordResetCode,
} from '../../../src/actions/auth'
import { promisesForPopulate } from '../../../src/utils/populate'

Expand Down Expand Up @@ -74,7 +75,10 @@ const fakeFirebase = {
confirmPasswordReset: (code, password) =>
password === 'error'
? Promise.reject({code: code})
: Promise.resolve()
: Promise.resolve(),
verifyPasswordResetCode: (code) => code === 'error'
? Promise.reject({ code: 'some' })
: Promise.resolve('success')
})
}

Expand Down Expand Up @@ -171,25 +175,24 @@ describe('Actions: Auth', () => {
})

describe('login', () => {
it('handles invalid email login', () => {
return login(dispatch, firebase, fakeLogin)
it('handles invalid email login', () =>
login(dispatch, firebase, fakeLogin)
.catch((err) => {
expect(err.code).to.equal('auth/user-not-found')
})
}, 4000)
it('handles invalid token login', () => {
return login(dispatch, firebase, { token: 'test@tst.com' })
, 4000)
it('handles invalid token login', () =>
login(dispatch, firebase, { token: 'test@tst.com' })
.catch((err) => {
expect(err.code).to.equal('auth/invalid-custom-token')
})
}, 4000)
it('handles token login', () => {
const token = 'asdfasdf'
return login(dispatch, fakeFirebase, { token }, { uid: 'asdfasdf' })
, 4000)
it('handles token login', () =>
login(dispatch, fakeFirebase, { token: 'asdfasdf' }, { uid: 'asdfasdf' })
.then((authData) => {
expect(authData).to.be.an.object
})
}, 4000)
, 4000)
})

describe('logout', () => {
Expand Down Expand Up @@ -296,11 +299,62 @@ describe('Actions: Auth', () => {
expect(err).to.be.undefined
})
})
it('dispatches for all other errors', () => {
return confirmPasswordReset(dispatch, fakeFirebase, 'auth/user-not-found', 'error')
.catch((err) => {
expect(err.code).to.be.a.string
describe('handles error code: ', () => {
it('auth/expired-action-code', () => {
return confirmPasswordReset(dispatch, fakeFirebase, 'auth/expired-action-code', 'error')
.catch((err) => {
expect(err.code).to.be.a.string
})
})
it('auth/invalid-action-code', () => {
return confirmPasswordReset(dispatch, fakeFirebase, 'auth/invalid-action-code', 'error')
.catch((err) => {
expect(err.code).to.be.a.string
})
})
it('auth/user-disabled', () => {
return confirmPasswordReset(dispatch, fakeFirebase, 'auth/user-disabled', 'error')
.catch((err) => {
expect(err.code).to.be.a.string
})
})
it('auth/user-not-found', () => {
return confirmPasswordReset(dispatch, fakeFirebase, 'auth/user-not-found', 'error')
.catch((err) => {
expect(err.code).to.be.a.string
})
})
it('auth/weak-password', () => {
return confirmPasswordReset(dispatch, fakeFirebase, 'auth/weak-password', 'error')
.catch((err) => {
console.log('error:', err)
expect(err.code).to.be.a.string
})
})
it('other', () => {
return confirmPasswordReset(dispatch, fakeFirebase, 'asdfasdf', 'error')
.catch((err) => {
expect(err.code).to.be.a.string
})
})
})

})

describe('verifyPasswordResetCode', () => {
it('resolves for valid code', () => {
return verifyPasswordResetCode(dispatch, fakeFirebase, 'test')
.then((res) => {
expect(res).to.equal('success')
})
})
describe('handles error code: ', () => {
it('other', () => {
return verifyPasswordResetCode(dispatch, fakeFirebase, 'error')
.catch((err) => {
expect(err.code).to.be.a.string
})
})
})
})
})