Created
November 11, 2020 23:47
-
-
Save blogcacanid/ff5279a201a8cf33322782471262906b to your computer and use it in GitHub Desktop.
auth.js Authentication JWT React JS Lumen 7
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import { | |
| REGISTER_SUCCESS, | |
| REGISTER_FAIL, | |
| LOGIN_SUCCESS, | |
| LOGIN_FAIL, | |
| LOGOUT, | |
| SET_MESSAGE, | |
| } from "./types"; | |
| import AuthService from "../services/auth.service"; | |
| export const register = (username, email, password) => (dispatch) => { | |
| return AuthService.register(username, email, password).then( | |
| (response) => { | |
| dispatch({ | |
| type: REGISTER_SUCCESS, | |
| }); | |
| dispatch({ | |
| type: SET_MESSAGE, | |
| payload: response.data.message, | |
| }); | |
| return Promise.resolve(); | |
| }, | |
| (error) => { | |
| const message = | |
| (error.response && | |
| error.response.data && | |
| error.response.data.message) || | |
| error.message || | |
| error.toString(); | |
| dispatch({ | |
| type: REGISTER_FAIL, | |
| }); | |
| dispatch({ | |
| type: SET_MESSAGE, | |
| payload: message, | |
| }); | |
| return Promise.reject(); | |
| } | |
| ); | |
| }; | |
| export const login = (username, password) => (dispatch) => { | |
| return AuthService.login(username, password).then( | |
| (data) => { | |
| dispatch({ | |
| type: LOGIN_SUCCESS, | |
| payload: { user: data }, | |
| }); | |
| return Promise.resolve(); | |
| }, | |
| (error) => { | |
| const message = | |
| (error.response && | |
| error.response.data && | |
| error.response.data.message) || | |
| error.message || | |
| error.toString(); | |
| dispatch({ | |
| type: LOGIN_FAIL, | |
| }); | |
| dispatch({ | |
| type: SET_MESSAGE, | |
| payload: message, | |
| }); | |
| return Promise.reject(); | |
| } | |
| ); | |
| }; | |
| export const logout = () => (dispatch) => { | |
| AuthService.logout(); | |
| dispatch({ | |
| type: LOGOUT, | |
| }); | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment