Skip to content

Instantly share code, notes, and snippets.

@guirald
Last active March 28, 2019 11:42
Show Gist options
  • Save guirald/a5392def439ff9684502b4891b4adec4 to your computer and use it in GitHub Desktop.
Save guirald/a5392def439ff9684502b4891b4adec4 to your computer and use it in GitHub Desktop.
AutenticacaoActions.js
// src/actions/AutenticacaoActions.js
export const cadastraUsuario = ({ nome, email, senha }) => {
// action de callback
// gerenciado pelo dispatch do Redux-Thunk
// fica intermediando nossa aplicação com o Redux
return dispatch => {
// ACTION PARA EVOLUIR ESTADO DA ACTION CADASTRO EM ANDAMENTO
dispatch({ type: CADASTRO_EM_ANDAMENTO })
auth.createUserWithEmailAndPassword(email, senha)
.then(async user => {
// converte o e-mail para base 64
let emailB64 = b64.encode(email)
let token = ''
// Set the current token if it exists
await firebase.messaging().getToken()
.then((fcmToken) => {
if (fcmToken) {
token = fcmToken
console.log('token', token)
}
}).catch((err) => console.log('err', err + ' ops'))
// qual o nó inserir a informação, no caso, contatos
await database.ref(`/contatos/${emailB64}`)
.push({ nome: nome, token: token })
// elemento que inserimos no DB através do push
// se o push deu certo, chama o cadastroUsuarioSucesso que vai dar o dispatch
.then(value => cadastroUsuarioSucesso(dispatch))
})
.catch(erro => cadastroUsuarioErro(erro, dispatch))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment