react-redux-django-auth 1.1.0 → 1.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.mjs CHANGED
@@ -54,6 +54,101 @@ var load_user = (apiUrl) => async (dispatch) => {
54
54
  });
55
55
  }
56
56
  };
57
+ var googleAuthenticate = (state, code, apiUrl) => async (dispatch) => {
58
+ if (state && code && !localStorage.getItem("access")) {
59
+ const config = {
60
+ headers: {
61
+ "Content-Type": "application/x-www-form-urlencoded"
62
+ }
63
+ };
64
+ const details = {
65
+ "state": state,
66
+ "code": code
67
+ };
68
+ const formBody = Object.keys(details).map((key) => encodeURIComponent(key) + "=" + encodeURIComponent(details[key])).join("&");
69
+ try {
70
+ const res = await axios.post(`${apiUrl}/auth/o/google-oauth2/?${formBody}`, config);
71
+ dispatch({
72
+ type: GOOGLE_AUTH_SUCCESS,
73
+ payload: {
74
+ ...res.data,
75
+ status: res.status
76
+ }
77
+ });
78
+ dispatch(load_user(apiUrl));
79
+ } catch (err) {
80
+ const error = err;
81
+ if (error.response) {
82
+ dispatch({
83
+ type: GOOGLE_AUTH_FAIL,
84
+ payload: error.response.data
85
+ });
86
+ }
87
+ }
88
+ }
89
+ };
90
+ var facebookAuthenticate = (state, code, apiUrl) => async (dispatch) => {
91
+ if (state && code && !localStorage.getItem("access")) {
92
+ const config = {
93
+ headers: {
94
+ "Content-Type": "application/x-www-form-urlencoded"
95
+ }
96
+ };
97
+ const details = {
98
+ "state": state,
99
+ "code": code
100
+ };
101
+ const formBody = Object.keys(details).map((key) => encodeURIComponent(key) + "=" + encodeURIComponent(details[key])).join("&");
102
+ try {
103
+ const res = await axios.post(`${apiUrl}/auth/o/facebook/?${formBody}`, config);
104
+ dispatch({
105
+ type: FACEBOOK_AUTH_SUCCESS,
106
+ payload: res.data
107
+ });
108
+ dispatch(load_user(apiUrl));
109
+ } catch (err) {
110
+ const error = err;
111
+ if (error.response) {
112
+ dispatch({
113
+ type: FACEBOOK_AUTH_FAIL
114
+ // Assuming no payload is needed for this failure case, based on original code.
115
+ // If an error payload is expected, you would add it here.
116
+ });
117
+ }
118
+ }
119
+ }
120
+ };
121
+ var checkAuthenticated = (apiUrl) => async (dispatch) => {
122
+ if (localStorage.getItem("access")) {
123
+ const config = {
124
+ headers: {
125
+ "Content-Type": "application/json",
126
+ "Accept": "application/json"
127
+ }
128
+ };
129
+ const body = JSON.stringify({ token: localStorage.getItem("access") });
130
+ try {
131
+ const res = await axios.post(`${apiUrl}/auth/jwt/verify/`, body, config);
132
+ if (res.data.code !== "token_not_valid") {
133
+ dispatch({
134
+ type: AUTHENTICATED_SUCCESS
135
+ });
136
+ } else {
137
+ dispatch({
138
+ type: AUTHENTICATED_FAIL
139
+ });
140
+ }
141
+ } catch (err) {
142
+ dispatch({
143
+ type: AUTHENTICATED_FAIL
144
+ });
145
+ }
146
+ } else {
147
+ dispatch({
148
+ type: AUTHENTICATED_FAIL
149
+ });
150
+ }
151
+ };
57
152
  var login = (formData, apiUrl) => async (dispatch) => {
58
153
  const config = {
59
154
  headers: {
@@ -127,6 +222,52 @@ var verify = (uid, token, apiUrl) => async (dispatch) => {
127
222
  }
128
223
  }
129
224
  };
225
+ var reset_password = (email, apiUrl) => async (dispatch) => {
226
+ const config = {
227
+ headers: {
228
+ "Content-Type": "application/json"
229
+ }
230
+ };
231
+ const body = JSON.stringify({ email });
232
+ try {
233
+ const res = await axios.post(`${apiUrl}/auth/users/reset_password/`, body, config);
234
+ dispatch({
235
+ type: PASSWORD_RESET_SUCCESS,
236
+ payload: res.status
237
+ });
238
+ } catch (err) {
239
+ const error = err;
240
+ if (error.response) {
241
+ dispatch({
242
+ type: PASSWORD_RESET_FAIL,
243
+ payload: error.response.data
244
+ });
245
+ }
246
+ }
247
+ };
248
+ var reset_password_confirm = (formData, apiUrl) => async (dispatch) => {
249
+ const config = {
250
+ headers: {
251
+ "Content-Type": "application/json"
252
+ }
253
+ };
254
+ const body = JSON.stringify(formData);
255
+ try {
256
+ const res = await axios.post(`${apiUrl}/auth/users/reset_password_confirm/`, body, config);
257
+ dispatch({
258
+ type: PASSWORD_RESET_CONFIRM_SUCCESS,
259
+ payload: res.status
260
+ });
261
+ } catch (err) {
262
+ const error = err;
263
+ if (error.response) {
264
+ dispatch({
265
+ type: PASSWORD_RESET_CONFIRM_FAIL,
266
+ payload: error.response.data
267
+ });
268
+ }
269
+ }
270
+ };
130
271
  var logout = () => (dispatch) => {
131
272
  dispatch({
132
273
  type: LOGOUT
@@ -137,6 +278,37 @@ var clearErrors = () => {
137
278
  type: CLEAR_ERRORS
138
279
  };
139
280
  };
281
+ var changeLoggedInUserPassword = (formData, apiUrl) => async (dispatch) => {
282
+ if (localStorage.getItem("access")) {
283
+ const config = {
284
+ headers: {
285
+ "Content-Type": "application/json",
286
+ "Accept": "application/json",
287
+ "Authorization": `JWT ${localStorage.getItem("access")}`
288
+ }
289
+ };
290
+ const body = JSON.stringify(formData);
291
+ try {
292
+ const res = await axios.post(`${apiUrl}/auth/users/set_password/`, body, config);
293
+ dispatch({
294
+ type: PASSWORD_RESET_SUCCESS,
295
+ payload: res.data
296
+ });
297
+ } catch (err) {
298
+ const error = err;
299
+ if (error.response) {
300
+ dispatch({
301
+ type: PASSWORD_RESET_FAIL,
302
+ payload: error.response.data
303
+ });
304
+ }
305
+ }
306
+ } else {
307
+ dispatch({
308
+ type: AUTHENTICATED_FAIL
309
+ });
310
+ }
311
+ };
140
312
 
141
313
  // src/hooks/useActivate.tsx
142
314
  var useActivate = (uid, token, apiUrl) => {
@@ -283,6 +455,306 @@ var useSignup = (initialFields, apiUrl) => {
283
455
  };
284
456
  var useSignup_default = useSignup;
285
457
 
458
+ // src/hooks/useLoadUser.tsx
459
+ import { useCallback as useCallback5, useEffect as useEffect4 } from "react";
460
+ import { useDispatch as useDispatch5, useSelector as useSelector4 } from "react-redux";
461
+ var useAuthicatedUser = (apiURL, location) => {
462
+ const dispatch = useDispatch5();
463
+ const isAuthenticated = useSelector4(
464
+ (state) => state.auth.isAuthenticated
465
+ );
466
+ const error = useSelector4((state) => state.auth.error);
467
+ const currentUser = useSelector4(
468
+ (state) => state.auth.user
469
+ );
470
+ useEffect4(() => {
471
+ dispatch(checkAuthenticated(apiURL));
472
+ }, [dispatch, apiURL, location]);
473
+ useEffect4(() => {
474
+ if (isAuthenticated) {
475
+ dispatch(load_user(apiURL));
476
+ }
477
+ }, [dispatch, isAuthenticated, apiURL]);
478
+ const refreshUser = useCallback5(() => {
479
+ dispatch(load_user(apiURL));
480
+ }, [dispatch, apiURL]);
481
+ const refreshAuth = useCallback5(async () => {
482
+ await dispatch(checkAuthenticated(apiURL));
483
+ dispatch(load_user(apiURL));
484
+ }, [dispatch, apiURL]);
485
+ const clearErrorsWrapper = useCallback5(() => {
486
+ dispatch(clearErrors());
487
+ }, [dispatch]);
488
+ return {
489
+ error,
490
+ isAuthenticated,
491
+ currentUser,
492
+ clearErrors: clearErrorsWrapper,
493
+ refreshUser,
494
+ refreshAuth
495
+ };
496
+ };
497
+ var useLoadUser_default = useAuthicatedUser;
498
+
499
+ // src/hooks/useResetPassword.tsx
500
+ import { useState as useState4, useCallback as useCallback6, useEffect as useEffect5 } from "react";
501
+ import { useDispatch as useDispatch6, useSelector as useSelector5 } from "react-redux";
502
+ var useResetPassword = (apiUrl) => {
503
+ const dispatch = useDispatch6();
504
+ const [loading, setLoading] = useState4(false);
505
+ const [email, setEmail] = useState4("");
506
+ const status = useSelector5((state) => state.auth.status);
507
+ const error = useSelector5((state) => state.auth.error);
508
+ const isStatus204 = status === 204;
509
+ useEffect5(() => {
510
+ return () => {
511
+ dispatch(clearErrors());
512
+ };
513
+ }, [dispatch]);
514
+ const handleChange = useCallback6((e) => {
515
+ setEmail(e.target.value);
516
+ }, []);
517
+ const handleSubmit = useCallback6(
518
+ async (e) => {
519
+ e.preventDefault();
520
+ setLoading(true);
521
+ try {
522
+ await dispatch(reset_password(email, apiUrl));
523
+ } catch (err) {
524
+ console.error("Error submitting form", err);
525
+ } finally {
526
+ setLoading(false);
527
+ }
528
+ },
529
+ [dispatch, email, apiUrl]
530
+ );
531
+ return {
532
+ email,
533
+ loading,
534
+ handleChange,
535
+ status,
536
+ error,
537
+ handleSubmit,
538
+ isStatus204
539
+ };
540
+ };
541
+ var useResetPassword_default = useResetPassword;
542
+
543
+ // src/hooks/usePasswordConfirm.tsx
544
+ import { useState as useState5, useCallback as useCallback7, useEffect as useEffect6 } from "react";
545
+ import { useDispatch as useDispatch7, useSelector as useSelector6 } from "react-redux";
546
+ var usePasswordConfirm = (initialFields, apiUrl) => {
547
+ const dispatch = useDispatch7();
548
+ const [loading, setLoading] = useState5(false);
549
+ const [formData, setFormData] = useState5(initialFields);
550
+ const error = useSelector6((state) => state.auth.error);
551
+ const status = useSelector6((state) => state.auth.status);
552
+ const isStatus204 = status === 204;
553
+ useEffect6(() => {
554
+ return () => {
555
+ dispatch(clearErrors());
556
+ };
557
+ }, [dispatch]);
558
+ const handleChange = useCallback7((e) => {
559
+ const { name, value } = e.target;
560
+ setFormData((prevFields) => ({ ...prevFields, [name]: value }));
561
+ }, []);
562
+ const handleSubmit = useCallback7(
563
+ async (e) => {
564
+ e.preventDefault();
565
+ setLoading(true);
566
+ try {
567
+ await dispatch(reset_password_confirm(formData, apiUrl));
568
+ } catch (err) {
569
+ console.error("Error submitting form", err);
570
+ } finally {
571
+ setLoading(false);
572
+ }
573
+ },
574
+ [dispatch, formData, apiUrl]
575
+ );
576
+ return {
577
+ loading,
578
+ formData,
579
+ error,
580
+ onChange: handleChange,
581
+ onSubmit: handleSubmit,
582
+ status,
583
+ isStatus204
584
+ };
585
+ };
586
+ var usePasswordConfirm_default = usePasswordConfirm;
587
+
588
+ // src/hooks/useChangePassword.tsx
589
+ import { useState as useState6, useCallback as useCallback8, useEffect as useEffect7 } from "react";
590
+ import { useDispatch as useDispatch8, useSelector as useSelector7 } from "react-redux";
591
+ var useChangeAuthenticatedUserPassword = (initialFields, apiUrl) => {
592
+ const dispatch = useDispatch8();
593
+ const [loading, setLoading] = useState6(false);
594
+ const [formData, setFormData] = useState6(initialFields);
595
+ const [error, setError] = useState6(null);
596
+ const authError = useSelector7(
597
+ (state) => state.auth.error
598
+ );
599
+ const status = useSelector7((state) => state.auth.status);
600
+ useEffect7(() => {
601
+ return () => {
602
+ dispatch(clearErrors());
603
+ };
604
+ }, [dispatch]);
605
+ const handleChange = useCallback8((e) => {
606
+ const { name, value } = e.target;
607
+ setFormData((prevFields) => ({ ...prevFields, [name]: value }));
608
+ }, []);
609
+ const handleSubmit = useCallback8(
610
+ async (e) => {
611
+ e.preventDefault();
612
+ const { current_password, new_password, confirm_new_password } = formData;
613
+ if (new_password !== confirm_new_password) {
614
+ setError("New passwords do not match.");
615
+ return;
616
+ }
617
+ setError(null);
618
+ setLoading(true);
619
+ try {
620
+ await dispatch(
621
+ changeLoggedInUserPassword({ current_password, new_password }, apiUrl)
622
+ );
623
+ } catch (err) {
624
+ console.error("Error submitting form", err);
625
+ } finally {
626
+ setLoading(false);
627
+ }
628
+ },
629
+ [dispatch, formData, apiUrl]
630
+ );
631
+ return {
632
+ loading,
633
+ formData,
634
+ error: error || authError,
635
+ onChange: handleChange,
636
+ onSubmit: handleSubmit,
637
+ status
638
+ };
639
+ };
640
+ var useChangePassword_default = useChangeAuthenticatedUserPassword;
641
+
642
+ // src/hooks/useGoogleAuth.tsx
643
+ import { useDispatch as useDispatch9, useSelector as useSelector8 } from "react-redux";
644
+ import { useEffect as useEffect8, useCallback as useCallback9, useState as useState7 } from "react";
645
+ import axios2 from "axios";
646
+ var useGoogleAuth = (apiUrl) => {
647
+ const dispatch = useDispatch9();
648
+ const isAuthenticated = useSelector8(
649
+ (state) => state.auth.isAuthenticated
650
+ );
651
+ const error = useSelector8((state) => state.auth.error);
652
+ const status = useSelector8((state) => state.auth.status);
653
+ const [loading, setLoading] = useState7(false);
654
+ const isStatus201 = status === 201;
655
+ const continueWithGoogle = useCallback9(async () => {
656
+ setLoading(true);
657
+ const redirectUri = `${window.location.origin}/login`;
658
+ try {
659
+ const res = await axios2.get(
660
+ `${apiUrl}/auth/o/google-oauth2/?redirect_uri=${redirectUri}`
661
+ );
662
+ window.location.replace(res.data.authorization_url);
663
+ } catch (err) {
664
+ console.error("Google Auth Failed", err);
665
+ } finally {
666
+ setLoading(false);
667
+ }
668
+ }, [apiUrl]);
669
+ const authenticateWithGoogle = useCallback9(
670
+ async (state, code) => {
671
+ if (!state || !code)
672
+ return;
673
+ setLoading(true);
674
+ try {
675
+ await dispatch(googleAuthenticate(state, code, apiUrl));
676
+ } catch (err) {
677
+ console.error("Authentication failed", err);
678
+ } finally {
679
+ setLoading(false);
680
+ }
681
+ },
682
+ [apiUrl, dispatch]
683
+ );
684
+ useEffect8(() => {
685
+ return () => {
686
+ dispatch(clearErrors());
687
+ };
688
+ }, [dispatch]);
689
+ return {
690
+ continueWithGoogle,
691
+ authenticateWithGoogle,
692
+ isAuthenticated,
693
+ error,
694
+ isStatus201,
695
+ loading
696
+ };
697
+ };
698
+ var useGoogleAuth_default = useGoogleAuth;
699
+
700
+ // src/hooks/useFacebookAuth.tsx
701
+ import { useDispatch as useDispatch10, useSelector as useSelector9 } from "react-redux";
702
+ import { useEffect as useEffect9, useCallback as useCallback10, useState as useState8 } from "react";
703
+ import axios3 from "axios";
704
+ var useFacebookAuth = (apiUrl) => {
705
+ const dispatch = useDispatch10();
706
+ const isAuthenticated = useSelector9(
707
+ (state) => state.auth.isAuthenticated
708
+ );
709
+ const error = useSelector9((state) => state.auth.error);
710
+ const status = useSelector9((state) => state.auth.status);
711
+ const [loading, setLoading] = useState8(false);
712
+ const isStatus201 = status === 201;
713
+ const continueWithFacebook = useCallback10(async () => {
714
+ setLoading(true);
715
+ const redirectUri = `${window.location.origin}/login`;
716
+ try {
717
+ const res = await axios3.get(
718
+ `${apiUrl}/auth/o/facebook-oauth2/?redirect_uri=${redirectUri}`
719
+ );
720
+ window.location.replace(res.data.authorization_url);
721
+ } catch (err) {
722
+ console.error("Facebook Auth Failed", err);
723
+ } finally {
724
+ setLoading(false);
725
+ }
726
+ }, [apiUrl]);
727
+ const authenticateWithFacebook = useCallback10(
728
+ async (state, code) => {
729
+ if (!state || !code)
730
+ return;
731
+ setLoading(true);
732
+ try {
733
+ await dispatch(facebookAuthenticate(state, code, apiUrl));
734
+ } catch (err) {
735
+ console.error("Authentication failed", err);
736
+ } finally {
737
+ setLoading(false);
738
+ }
739
+ },
740
+ [apiUrl, dispatch]
741
+ );
742
+ useEffect9(() => {
743
+ return () => {
744
+ dispatch(clearErrors());
745
+ };
746
+ }, [dispatch]);
747
+ return {
748
+ continueWithFacebook,
749
+ authenticateWithFacebook,
750
+ isAuthenticated,
751
+ error,
752
+ isStatus201,
753
+ loading
754
+ };
755
+ };
756
+ var useFacebookAuth_default = useFacebookAuth;
757
+
286
758
  // src/app.tsx
287
759
  import React from "react";
288
760
  import { Provider } from "react-redux";
@@ -384,9 +856,10 @@ function authReducer(state = initialState, action) {
384
856
  }
385
857
 
386
858
  // src/reducers/index.ts
387
- var reducers_default = combineReducers({
859
+ var rootReducer = combineReducers({
388
860
  auth: authReducer
389
861
  });
862
+ var reducers_default = rootReducer;
390
863
 
391
864
  // src/store.ts
392
865
  var initialState2 = {};
@@ -405,7 +878,13 @@ var app_default = AuthProvider;
405
878
  export {
406
879
  app_default as AuthProvider,
407
880
  useActivate_default as useActivate,
881
+ useLoadUser_default as useAuthicatedUser,
882
+ useChangePassword_default as useChangeAuthenticatedUserPassword,
883
+ useFacebookAuth_default as useFacebookAuth,
884
+ useGoogleAuth_default as useGoogleAuth,
408
885
  useLogin_default as useLogin,
409
886
  useLogout_default as useLogout,
887
+ usePasswordConfirm_default as usePasswordConfirm,
888
+ useResetPassword_default as useResetPassword,
410
889
  useSignup_default as useSignup
411
890
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-redux-django-auth",
3
- "version": "1.1.0",
3
+ "version": "1.3.0",
4
4
  "description": "A React Redux Authentication system for django app built with TypeScript.",
5
5
  "main": "./dist/index.js",
6
6
  "module": "./dist/index.mjs",
@@ -16,7 +16,15 @@
16
16
  "react",
17
17
  "component",
18
18
  "typescript",
19
- "npm"
19
+ "npm",
20
+ "redux",
21
+ "django",
22
+ "authentication",
23
+ "auth",
24
+ "redux-toolkit",
25
+ "redux-thunk",
26
+ "react-redux",
27
+ "redux-devtools"
20
28
  ],
21
29
  "author": "Your Name",
22
30
  "license": "MIT",