Skip to content

Commit

Permalink
Merge pull request fortran-lang#12 from arteevraina/authentication-bu…
Browse files Browse the repository at this point in the history
…g-fixes

Authentication bug fixes
  • Loading branch information
henilp105 authored Feb 11, 2023
2 parents cd8c0a5 + 59e59ed commit e3c3618
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 26 deletions.
29 changes: 18 additions & 11 deletions flask/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,19 +63,26 @@ def login():
@swag_from("documentation/signup.yaml", methods=["POST"])
def signup():
uuid = request.form.get("uuid")

if not uuid:
name = request.form.get("name")
email = request.form.get("email")
password = request.form.get("password")
password += salt
hashed_password = hashlib.sha256(password.encode()).hexdigest()
registry_user = db.users.find_one({"$or": [{"name": name}, {"email": email}]})
uuid = generate_uuid()
else:
return (
jsonify({"message": "A user with this email already exists", "code": 400}),
400,
)

name = request.form.get("name")
email = request.form.get("email")
password = request.form.get("password")

if not name:
return jsonify({"message": "Name is required", "code": 400}), 400

if not email:
return jsonify({"message": "Email is required", "code": 400}), 400

if not password:
return jsonify({"message": "Password is required", "code": 400}), 400

password += salt
hashed_password = hashlib.sha256(password.encode()).hexdigest()
registry_user = db.users.find_one({"$or": [{"name": name}, {"email": email}]})

user = {
"name": name,
Expand Down
28 changes: 27 additions & 1 deletion registry/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,45 @@ import rootReducer from "./store/reducers/rootReducer";
import { persistStore, persistReducer } from "redux-persist";
import storage from "redux-persist/lib/storage";
import { PersistGate } from "redux-persist/integration/react";
import { createTransform, REGISTER } from "redux-persist";

const authTransform = createTransform(
// Transform state on its way to being serialized and stored
(inboundState, key) => {
return {
isAuthenticated: inboundState.isAuthenticated,
username: inboundState.username,
uuid: inboundState.uuid,
};
},
// Transform state on its way back from storage to be rehydrated
(outboundState, key) => {
return {
...outboundState,
};
},
// Specify the key for the persistable state, in this case it is 'auth'
{ whitelist: ["auth"] }
);

const persistConfig = {
key: "root",
storage,
whitelist: ["auth"],
transforms: [authTransform],
};

const persistedReducer = persistReducer(persistConfig, rootReducer);

const root = ReactDOM.createRoot(document.getElementById("root"));
const store = configureStore({
reducer: persistedReducer,
middleware: (getDefaultMiddleware) => getDefaultMiddleware({
serializableCheck: {
ignoreActions: [REGISTER]
}
}),
});

const persistor = persistStore(store);

root.render(
Expand Down
21 changes: 15 additions & 6 deletions registry/src/pages/login.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import React, { useEffect, useState } from "react";
import { useNavigate } from "react-router-dom";
import { useCookies } from "react-cookie";
import { useDispatch, useSelector } from "react-redux";
import { login } from "../store/actions/authActions";
import { login, resetErrorMessage } from "../store/actions/authActions";
import { Link } from "react-router-dom";

const Login = () => {
const [email, setEmail] = useState("");
Expand All @@ -19,11 +20,12 @@ const Login = () => {
if (isAuthenticated) {
setCookie("uuid", uuid);
navigate("/manage/projects");
} else if (errorMessage !== null) {
const errorDiv = document.getElementById("error");
errorDiv.innerHTML = errorMessage;
}
}, [isAuthenticated, errorMessage]);

if (errorMessage != null) {
dispatch(resetErrorMessage());
}
}, [isAuthenticated]);

const handleSubmit = async (e) => {
e.preventDefault();
Expand All @@ -48,8 +50,15 @@ const Login = () => {
value={password}
onChange={(e) => setPassword(e.target.value)}
/>
<p id="error" className="error"></p>
{errorMessage != null ? (
<p id="error" className="error">
{errorMessage}
</p>
) : null}
<input type="submit" value="Log In" />
<p>
Don't have an account?<Link to="/account/register"> Sign up </Link>
</p>
</form>
);
};
Expand Down
21 changes: 15 additions & 6 deletions registry/src/pages/register.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import React, { useEffect, useState } from "react";
import { useNavigate } from "react-router-dom";
import { useCookies } from "react-cookie";
import { useDispatch, useSelector } from "react-redux";
import { signup } from "../store/actions/authActions";
import { signup, resetErrorMessage } from "../store/actions/authActions";
import { Link } from "react-router-dom";

const Register = () => {
const [email, setEmail] = useState("");
Expand All @@ -25,11 +26,12 @@ const Register = () => {
if (isAuthenticated) {
setCookie("uuid", uuid);
navigate("/manage/projects");
} else if (errorMessage !== null) {
const errorDiv = document.getElementById("error");
errorDiv.innerHTML = errorMessage;
}
}, [isAuthenticated, errorMessage]);

if (errorMessage != null) {
dispatch(resetErrorMessage());
}
}, [isAuthenticated]);

return (
<form id="login-form" onSubmit={handleSubmit}>
Expand All @@ -56,8 +58,15 @@ const Register = () => {
value={password}
onChange={(e) => setPassword(e.target.value)}
/>
<p id="error" className="error"></p>
{errorMessage != null ? (
<p id="error" className="error">
{errorMessage}
</p>
) : null}
<input type="submit" value="Sign Up" />
<p>
Already have an account?<Link to="/account/login"> Log in </Link>
</p>
</form>
);
};
Expand Down
12 changes: 10 additions & 2 deletions registry/src/store/actions/authActions.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import axios from "axios";

export const RESET_ERROR_MESSAGE = "RESET_ERROR_MESSAGE";

export const LOGIN_SUCCESS = "LOGIN_SUCCESS";
export const LOGIN_FAILURE = "LOGIN_FAILURE";

Expand Down Expand Up @@ -41,7 +43,7 @@ export const login = (email, password) => async (dispatch) => {
dispatch({
type: LOGIN_FAILURE,
payload: {
error: error,
error: error.response.data.message,
},
});
}
Expand Down Expand Up @@ -129,8 +131,14 @@ export const signup = (name, email, password) => async (dispatch) => {
dispatch({
type: SIGNUP_FAILURE,
payload: {
error: error,
error: error.response.data.message,
},
});
}
};

export const resetErrorMessage = () => async (dispatch) => {
dispatch({
type: RESET_ERROR_MESSAGE,
});
};
6 changes: 6 additions & 0 deletions registry/src/store/reducers/authReducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
SIGNUP_FAILURE,
LOGOUT_SUCCESS,
LOGOUT_FAILURE,
RESET_ERROR_MESSAGE,
} from "../actions/authActions";

const initialState = {
Expand Down Expand Up @@ -60,6 +61,11 @@ const authReducer = (state = initialState, action) => {
isAuthenticated: false,
error: action.payload.error,
};
case RESET_ERROR_MESSAGE:
return {
...state,
error: null,
};
default:
return state;
}
Expand Down

0 comments on commit e3c3618

Please sign in to comment.