summaryrefslogtreecommitdiff
path: root/app/client/auth/auth.actions.js
diff options
context:
space:
mode:
authorJules Laplace <julescarbon@gmail.com>2018-09-16 22:40:05 +0200
committerJules Laplace <julescarbon@gmail.com>2018-09-16 22:40:05 +0200
commitd3e4bb3ed2585859a3adeb7eeff35b7c75ebd840 (patch)
treee88e9edae5a63328fb1acc625e5624990717d20f /app/client/auth/auth.actions.js
parent189be96150fbd49766228cf50c6a89279542565c (diff)
auth gate on main app. pull in auth routes from bucky.
Diffstat (limited to 'app/client/auth/auth.actions.js')
-rw-r--r--app/client/auth/auth.actions.js82
1 files changed, 82 insertions, 0 deletions
diff --git a/app/client/auth/auth.actions.js b/app/client/auth/auth.actions.js
new file mode 100644
index 0000000..5968f87
--- /dev/null
+++ b/app/client/auth/auth.actions.js
@@ -0,0 +1,82 @@
+import * as types from '../types';
+
+export const setToken = (data) => {
+ return { type: types.auth.set_token, data }
+}
+export const setError = (data) => {
+ return { type: types.auth.set_error, data }
+}
+export const setCurrentUser = (data) => {
+ return { type: types.auth.set_current_user, data }
+}
+export function logout() {
+ return { type: types.auth.logout_user };
+}
+export function authLoading() {
+ return { type: types.auth.loading };
+}
+
+export function InvalidCredentialsException(message) {
+ this.message = message;
+ this.name = 'InvalidCredentialsException';
+}
+
+export function login(username, password) {
+ return (dispatch) => {
+ dispatch(authLoading());
+ apiClient()
+ .post(api.GET_TOKEN, {
+ username,
+ password
+ })
+ .then(function (response) {
+ dispatch(setToken(response.data.token));
+ dispatch(getCurrentUser());
+ })
+ .catch(function (error) {
+ dispatch(setError(true));
+ if (error.response.status === 400) {
+ throw new InvalidCredentialsException(error);
+ }
+ throw error;
+ });
+ };
+}
+
+export function signup(data) {
+ return (dispatch) => {
+ dispatch(authLoading());
+ apiClient()
+ .post(api.SIGNUP, data)
+ .then(function (response) {
+ console.log(response.data);
+ dispatch(login(data.username, data.password));
+ })
+ .catch(function (error) {
+ console.log(error)
+ if (error.response.status === 400) {
+ // dispatch(accountError("There was an error creating your account."))
+ throw new InvalidCredentialsException(error);
+ }
+ throw error;
+ });
+ };
+}
+
+export function getCurrentUser() {
+ return (dispatch) => {
+ dispatch(authLoading());
+ apiClient()
+ .get(api.CURRENT_USER)
+ .then(function (response) {
+ dispatch(setCurrentUser(response.data));
+ console.log('set current user')
+ })
+ .catch(function (error) {
+ if (error.response.status === 400) {
+ throw new InvalidCredentialsException(error);
+ }
+ throw error;
+ });
+ };
+}