@@ -120,4 +156,8 @@ const LoginScreen = () => {
)
}
-export default LoginScreen;
\ No newline at end of file
+const mapStateToProps = state => ({
+ isAuthenticated: state.auth.isAuthenticated
+});
+
+export default connect(mapStateToProps, { login })(LoginScreen);
\ No newline at end of file
diff --git a/frontend/src/pages/Signup/SignupScreen.jsx b/frontend/src/pages/Signup/SignupScreen.jsx
index 89fcbb7..bcc6c17 100644
--- a/frontend/src/pages/Signup/SignupScreen.jsx
+++ b/frontend/src/pages/Signup/SignupScreen.jsx
@@ -42,31 +42,6 @@ const SignupScreen = () => {
setShowSignupTab3(true)
}
- const handleSubmit = async (e) => {
- e.preventDefault();
- const data = new FormData(e.currentTarget);
- const actualData = {
- name: data.get('name'),
- email: data.get('email'),
- aadhaar: data.get('aadhaar'),
- mobile: data.get('mobile'),
- password: data.get('password'),
- password2: data.get('password2'),
- }
- const res = await registerUser(actualData)
- if (res.error) {
- console.log(typeof (res.error.data.errors))
- console.log(res.error.data.errors)
- setServerError(res.error.data.errors)
- }
- if (res.data) {
- console.log(typeof (res.data))
- console.log(res.data)
- storeToken(res.data.token)
- navigate('/dashboard')
- }
- }
-
return (
diff --git a/frontend/src/services/actions/auth.js b/frontend/src/services/actions/auth.js
new file mode 100644
index 0000000..804515d
--- /dev/null
+++ b/frontend/src/services/actions/auth.js
@@ -0,0 +1,271 @@
+import axios from 'axios';
+import {
+ LOGIN_SUCCESS,
+ LOGIN_FAIL,
+ USER_LOADED_SUCCESS,
+ USER_LOADED_FAIL,
+ AUTHENTICATED_SUCCESS,
+ AUTHENTICATED_FAIL,
+ PASSWORD_RESET_SUCCESS,
+ PASSWORD_RESET_FAIL,
+ PASSWORD_RESET_CONFIRM_SUCCESS,
+ PASSWORD_RESET_CONFIRM_FAIL,
+ SIGNUP_SUCCESS,
+ SIGNUP_FAIL,
+ ACTIVATION_SUCCESS,
+ ACTIVATION_FAIL,
+ GOOGLE_AUTH_SUCCESS,
+ GOOGLE_AUTH_FAIL,
+ FACEBOOK_AUTH_SUCCESS,
+ FACEBOOK_AUTH_FAIL,
+ LOGOUT
+} from './types';
+
+export const load_user = () => async dispatch => {
+ if (localStorage.getItem('access')) {
+ const config = {
+ headers: {
+ 'Content-Type': 'application/json',
+ 'Authorization': `JWT ${localStorage.getItem('access')}`,
+ 'Accept': 'application/json'
+ }
+ };
+
+ try {
+ const res = await axios.get(`${process.env.REACT_APP_API_URL}/auth/users/me/`, config);
+
+ dispatch({
+ type: USER_LOADED_SUCCESS,
+ payload: res.data
+ });
+ } catch (err) {
+ dispatch({
+ type: USER_LOADED_FAIL
+ });
+ }
+ } else {
+ dispatch({
+ type: USER_LOADED_FAIL
+ });
+ }
+};
+
+export const googleAuthenticate = (state, code) => async dispatch => {
+ if (state && code && !localStorage.getItem('access')) {
+ const config = {
+ headers: {
+ 'Content-Type': 'application/x-www-form-urlencoded'
+ }
+ };
+
+ const details = {
+ 'state': state,
+ 'code': code
+ };
+
+ const formBody = Object.keys(details).map(key => encodeURIComponent(key) + '=' + encodeURIComponent(details[key])).join('&');
+
+ try {
+ const res = await axios.post(`${process.env.REACT_APP_API_URL}/auth/o/google-oauth2/?${formBody}`, config);
+
+ dispatch({
+ type: GOOGLE_AUTH_SUCCESS,
+ payload: res.data
+ });
+
+ dispatch(load_user());
+ } catch (err) {
+ dispatch({
+ type: GOOGLE_AUTH_FAIL
+ });
+ }
+ }
+};
+
+export const facebookAuthenticate = (state, code) => async dispatch => {
+ if (state && code && !localStorage.getItem('access')) {
+ const config = {
+ headers: {
+ 'Content-Type': 'application/x-www-form-urlencoded'
+ }
+ };
+
+ const details = {
+ 'state': state,
+ 'code': code
+ };
+
+ const formBody = Object.keys(details).map(key => encodeURIComponent(key) + '=' + encodeURIComponent(details[key])).join('&');
+
+ try {
+ const res = await axios.post(`${process.env.REACT_APP_API_URL}/auth/o/facebook/?${formBody}`, config);
+
+ dispatch({
+ type: FACEBOOK_AUTH_SUCCESS,
+ payload: res.data
+ });
+
+ dispatch(load_user());
+ } catch (err) {
+ dispatch({
+ type: FACEBOOK_AUTH_FAIL
+ });
+ }
+ }
+};
+
+export const checkAuthenticated = () => async dispatch => {
+ if (localStorage.getItem('access')) {
+ const config = {
+ headers: {
+ 'Content-Type': 'application/json',
+ 'Accept': 'application/json'
+ }
+ };
+
+ const body = JSON.stringify({ token: localStorage.getItem('access') });
+
+ try {
+ const res = await axios.post(`${process.env.REACT_APP_API_URL}/auth/jwt/verify/`, body, config)
+
+ if (res.data.code !== 'token_not_valid') {
+ dispatch({
+ type: AUTHENTICATED_SUCCESS
+ });
+ } else {
+ dispatch({
+ type: AUTHENTICATED_FAIL
+ });
+ }
+ } catch (err) {
+ dispatch({
+ type: AUTHENTICATED_FAIL
+ });
+ }
+
+ } else {
+ dispatch({
+ type: AUTHENTICATED_FAIL
+ });
+ }
+};
+
+export const login = (email, password) => async dispatch => {
+ const config = {
+ headers: {
+ 'Content-Type': 'application/json'
+ }
+ };
+
+ const body = JSON.stringify({ email, password });
+
+ try {
+ const res = await axios.post(`${process.env.REACT_APP_API_URL}/auth/jwt/create/`, body, config);
+
+ dispatch({
+ type: LOGIN_SUCCESS,
+ payload: res.data
+ });
+
+ dispatch(load_user());
+ } catch (err) {
+ dispatch({
+ type: LOGIN_FAIL
+ })
+ }
+};
+
+export const signup = (first_name, last_name, email, password, re_password) => async dispatch => {
+ const config = {
+ headers: {
+ 'Content-Type': 'application/json'
+ }
+ };
+
+ const body = JSON.stringify({ first_name, last_name, email, password, re_password });
+
+ try {
+ const res = await axios.post(`${process.env.REACT_APP_API_URL}/auth/users/`, body, config);
+
+ dispatch({
+ type: SIGNUP_SUCCESS,
+ payload: res.data
+ });
+ } catch (err) {
+ dispatch({
+ type: SIGNUP_FAIL
+ })
+ }
+};
+
+export const verify = (uid, token) => async dispatch => {
+ const config = {
+ headers: {
+ 'Content-Type': 'application/json'
+ }
+ };
+
+ const body = JSON.stringify({ uid, token });
+
+ try {
+ await axios.post(`${process.env.REACT_APP_API_URL}/auth/users/activation/`, body, config);
+
+ dispatch({
+ type: ACTIVATION_SUCCESS,
+ });
+ } catch (err) {
+ dispatch({
+ type: ACTIVATION_FAIL
+ })
+ }
+};
+
+export const reset_password = (email) => async dispatch => {
+ const config = {
+ headers: {
+ 'Content-Type': 'application/json'
+ }
+ };
+
+ const body = JSON.stringify({ email });
+
+ try {
+ await axios.post(`${process.env.REACT_APP_API_URL}/auth/users/reset_password/`, body, config);
+
+ dispatch({
+ type: PASSWORD_RESET_SUCCESS
+ });
+ } catch (err) {
+ dispatch({
+ type: PASSWORD_RESET_FAIL
+ });
+ }
+};
+
+export const reset_password_confirm = (uid, token, new_password, re_new_password) => async dispatch => {
+ const config = {
+ headers: {
+ 'Content-Type': 'application/json'
+ }
+ };
+
+ const body = JSON.stringify({ uid, token, new_password, re_new_password });
+
+ try {
+ await axios.post(`${process.env.REACT_APP_API_URL}/auth/users/reset_password_confirm/`, body, config);
+
+ dispatch({
+ type: PASSWORD_RESET_CONFIRM_SUCCESS
+ });
+ } catch (err) {
+ dispatch({
+ type: PASSWORD_RESET_CONFIRM_FAIL
+ });
+ }
+};
+
+export const logout = () => dispatch => {
+ dispatch({
+ type: LOGOUT
+ });
+};
diff --git a/frontend/src/services/actions/types.js b/frontend/src/services/actions/types.js
new file mode 100644
index 0000000..fd4faa7
--- /dev/null
+++ b/frontend/src/services/actions/types.js
@@ -0,0 +1,19 @@
+export const LOGIN_SUCCESS = 'LOGIN_SUCCESS';
+export const LOGIN_FAIL = 'LOGIN_FAIL';
+export const SIGNUP_SUCCESS = 'SIGNUP_SUCCESS';
+export const SIGNUP_FAIL = 'SIGNUP_FAIL';
+export const ACTIVATION_SUCCESS = 'ACTIVATION_SUCCESS';
+export const ACTIVATION_FAIL = 'ACTIVATION_FAIL';
+export const USER_LOADED_SUCCESS = 'USER_LOADED_SUCCESS';
+export const USER_LOADED_FAIL = 'USER_LOADED_FAIL';
+export const AUTHENTICATED_SUCCESS = 'AUTHENTICATED_SUCCESS';
+export const AUTHENTICATED_FAIL = 'AUTHENTICATED_FAIL';
+export const PASSWORD_RESET_FAIL = 'PASSWORD_RESET_FAIL';
+export const PASSWORD_RESET_SUCCESS = 'PASSWORD_RESET_SUCCESS';
+export const PASSWORD_RESET_CONFIRM_FAIL = 'PASSWORD_RESET_CONFIRM_FAIL';
+export const PASSWORD_RESET_CONFIRM_SUCCESS = 'PASSWORD_RESET_CONFIRM_SUCCESS';
+export const GOOGLE_AUTH_SUCCESS = 'GOOGLE_AUTH_SUCCESS';
+export const GOOGLE_AUTH_FAIL = 'GOOGLE_AUTH_FAIL';
+export const FACEBOOK_AUTH_SUCCESS = 'FACEBOOK_AUTH_SUCCESS';
+export const FACEBOOK_AUTH_FAIL = 'FACEBOOK_AUTH_FAIL';
+export const LOGOUT = 'LOGOUT';
diff --git a/frontend/src/services/reducers/auth.js b/frontend/src/services/reducers/auth.js
new file mode 100644
index 0000000..a76d029
--- /dev/null
+++ b/frontend/src/services/reducers/auth.js
@@ -0,0 +1,96 @@
+import {
+ LOGIN_SUCCESS,
+ LOGIN_FAIL,
+ USER_LOADED_SUCCESS,
+ USER_LOADED_FAIL,
+ AUTHENTICATED_SUCCESS,
+ AUTHENTICATED_FAIL,
+ PASSWORD_RESET_SUCCESS,
+ PASSWORD_RESET_FAIL,
+ PASSWORD_RESET_CONFIRM_SUCCESS,
+ PASSWORD_RESET_CONFIRM_FAIL,
+ SIGNUP_SUCCESS,
+ SIGNUP_FAIL,
+ ACTIVATION_SUCCESS,
+ ACTIVATION_FAIL,
+ GOOGLE_AUTH_SUCCESS,
+ GOOGLE_AUTH_FAIL,
+ FACEBOOK_AUTH_SUCCESS,
+ FACEBOOK_AUTH_FAIL,
+ LOGOUT
+} from '../actions/types';
+
+const initialState = {
+ access: localStorage.getItem('access'),
+ refresh: localStorage.getItem('refresh'),
+ isAuthenticated: null,
+ user: null
+};
+
+export default function(state = initialState, action) {
+ const { type, payload } = action;
+
+ switch(type) {
+ case AUTHENTICATED_SUCCESS:
+ return {
+ ...state,
+ isAuthenticated: true
+ }
+ case LOGIN_SUCCESS:
+ case GOOGLE_AUTH_SUCCESS:
+ case FACEBOOK_AUTH_SUCCESS:
+ localStorage.setItem('access', payload.access);
+ localStorage.setItem('refresh', payload.refresh);
+ return {
+ ...state,
+ isAuthenticated: true,
+ access: payload.access,
+ refresh: payload.refresh
+ }
+ case SIGNUP_SUCCESS:
+ return {
+ ...state,
+ isAuthenticated: false
+ }
+ case USER_LOADED_SUCCESS:
+ return {
+ ...state,
+ user: payload
+ }
+ case AUTHENTICATED_FAIL:
+ return {
+ ...state,
+ isAuthenticated: false
+ }
+ case USER_LOADED_FAIL:
+ return {
+ ...state,
+ user: null
+ }
+ case GOOGLE_AUTH_FAIL:
+ case FACEBOOK_AUTH_FAIL:
+ case LOGIN_FAIL:
+ case SIGNUP_FAIL:
+ case LOGOUT:
+ localStorage.removeItem('access');
+ localStorage.removeItem('refresh');
+ return {
+ ...state,
+ access: null,
+ refresh: null,
+ isAuthenticated: false,
+ user: null
+ }
+ case PASSWORD_RESET_SUCCESS:
+ case PASSWORD_RESET_FAIL:
+ case PASSWORD_RESET_CONFIRM_SUCCESS:
+ case PASSWORD_RESET_CONFIRM_FAIL:
+ case ACTIVATION_SUCCESS:
+ case ACTIVATION_FAIL:
+ return {
+ ...state
+ }
+ default:
+ return state
+ }
+};
diff --git a/frontend/src/services/reducers/index.js b/frontend/src/services/reducers/index.js
new file mode 100644
index 0000000..f5d9042
--- /dev/null
+++ b/frontend/src/services/reducers/index.js
@@ -0,0 +1,6 @@
+import { combineReducers } from 'redux';
+import auth from './auth';
+
+export default combineReducers({
+ auth
+});