Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed an issue with HMI not getting fetched correctly #447

Merged
merged 3 commits into from
Sep 23, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/api/hatAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ export const getPublicProfile = (path: string) => {
return get<BundleValues>(path, { method: 'get', headers });
};

export const resetPassword = (resetToken: string, body: { newPassword: string }) => {
const path = `/control/v2/auth/passwordreset/confirm/${resetToken}`;
export const resetPassword = (pda: string, resetToken: string, body: { newPassword: string }) => {
const path = `https://${pda}/control/v2/auth/passwordreset/confirm/${resetToken}`;
const headers = { 'Content-Type': 'application/json' };

return post(path, {}, { method: 'post', headers: headers, body: JSON.stringify(body) });
Expand Down
4 changes: 2 additions & 2 deletions src/app/PrivateRoute.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ type Query = {
redirect_uri?: string;
};

export function PrivateRoute({ children, newAuth, ...rest }: OwnProps) {
export function PrivateRoute({ children, ...rest }: OwnProps) {
const isAuthenticated = useSelector(selectIsAuthenticated);
// TODO Add the type to the state useState<Query>
const [query, setQuery] = useState({});
Expand Down Expand Up @@ -54,7 +54,7 @@ export function PrivateRoute({ children, newAuth, ...rest }: OwnProps) {
) : (
<DelayedRedirect
to={{
pathname: newAuth ? '/auth/login' : '/user/login',
pathname: '/auth/login',
state: { from: location, query: query },
}}
delay={100}
Expand Down
4 changes: 2 additions & 2 deletions src/features/applications/applicationsSlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,11 @@ export const getApplications = (): AppThunk => async (dispatch, getState) => {
};

export const getApplicationHmi =
(applicationId: string): AppThunk =>
(applicationId: string, pda: string): AppThunk =>
async (dispatch) => {
try {
dispatch(setAppsHmiState('pending'));
const apps = await HatClientService.getInstance().getApplicationHmi(applicationId);
const apps = await HatClientService.getInstance().getApplicationHmi(applicationId, pda);

if (apps?.parsedBody) {
dispatch(setAppsHmi(apps.parsedBody));
Expand Down
37 changes: 31 additions & 6 deletions src/features/authentication/AuthChangePassword.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ import {
import FormatMessage from '../messages/FormatMessage';
import { selectLanguage } from '../language/languageSlice';
import { selectMessages } from '../messages/messagesSlice';
import { isEmail } from "../../utils/validations";
import { pdaLookupWithEmail } from "../../services/HattersService";
import { PdaLookupResponse } from "../../types/Hatters";

type Query = {
email?: string;
Expand Down Expand Up @@ -45,6 +48,7 @@ export const AuthChangePassword: React.FC<ChangePasswordProps> = ({ passwordStre
const [score, setScore] = useState(0);
const [errorMessage, setErrorMessage] = useState('');
const [errorSuggestion, setErrorSuggestion] = useState('');
const [lookupResponse, setLookupResponse] = useState<PdaLookupResponse | null>(null);
const dispatch = useDispatch();
const [openPopup, setOpenPopup] = useState(false);
const [passwordMatch, setPasswordMatch] = useState<boolean | undefined>(undefined);
Expand Down Expand Up @@ -78,7 +82,11 @@ export const AuthChangePassword: React.FC<ChangePasswordProps> = ({ passwordStre

const resetPasswordRequest = async () => {
try {
const res = await resetPassword(resetToken, { newPassword: password });
const res = await resetPassword(
lookupResponse?.hatName + '.' + lookupResponse?.hatCluster,
resetToken,
{ newPassword: password }
);

if (res) {
setSuccessfulResponse(new Date());
Expand Down Expand Up @@ -128,16 +136,33 @@ export const AuthChangePassword: React.FC<ChangePasswordProps> = ({ passwordStre
}
};

const getPdaDetails = async (emailAddress?: string) => {
try {
if (!emailAddress) return;

const res = await pdaLookupWithEmail(emailAddress);
if (res.parsedBody) {
setLookupResponse(res.parsedBody);
}
} catch (e) {
setErrorMessage(messages['ds.auth.error.oops']);
}
};

useEffect(() => {
const { email, application_id } = queryString.parse(window.location.search) as Query;
setEmail(email || '');
const { email, application_id } = queryString.parse(location.search) as Query;
if (email && isEmail(email) && !lookupResponse) {
setEmail(email);
getPdaDetails(email);
}

if (!parentApp && application_id) {
dispatch(getApplicationHmi(application_id));
if (!parentApp && application_id && lookupResponse) {
dispatch(getApplicationHmi(application_id, lookupResponse?.hatName + '.' + lookupResponse?.hatCluster));
} else {
dispatch(setAppsHmiState('completed'));
}
}, [dispatch, parentApp]);
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [dispatch, parentApp, lookupResponse]);

useEffect(() => {
passwordMatchDebounce(password, passwordConfirm, score);
Expand Down
8 changes: 4 additions & 4 deletions src/features/authentication/AuthLogin.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ const AuthLogin: React.FC = () => {

const getPdaDetails = async () => {
try {
if (!email) return;
if (!email) return history.replace('/');

const res = await pdaLookupWithEmail(email);

Expand Down Expand Up @@ -128,12 +128,12 @@ const AuthLogin: React.FC = () => {
};

useEffect(() => {
if (applicationId && !parentApp) {
dispatch(getApplicationHmi(applicationId));
if (applicationId && lookupResponse && !parentApp) {
dispatch(getApplicationHmi(applicationId, lookupResponse?.hatName + '.' + lookupResponse?.hatCluster));
} else {
dispatch(setAppsHmiState('completed'));
}
}, [dispatch, parentApp, applicationId]);
}, [dispatch, parentApp, applicationId, lookupResponse]);

if (!lookupResponse) {
return null;
Expand Down
8 changes: 4 additions & 4 deletions src/features/authentication/AuthVerifyEmail.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -165,18 +165,18 @@ export const AuthVerifyEmail: React.FC<AuthVerifyEmailProps> = ({ passwordStreng

useEffect(() => {
const { email, application_id } = queryString.parse(location.search) as Query;
if (email && isEmail(email)) {
if (email && isEmail(email) && !lookupResponse) {
setEmail(email);
getPdaDetails(email);
}

if (!parentApp && application_id) {
dispatch(getApplicationHmi(application_id));
if (!parentApp && application_id && lookupResponse) {
dispatch(getApplicationHmi(application_id, lookupResponse?.hatName + '.' + lookupResponse?.hatCluster));
} else {
dispatch(setAppsHmiState('completed'));
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [dispatch, parentApp]);
}, [dispatch, parentApp, lookupResponse]);

useEffect(() => {
passwordMatchDebounce(password, passwordConfirm, score);
Expand Down
6 changes: 3 additions & 3 deletions src/features/hat-login/HatLoginParamValidation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,18 @@ const HatLoginParamValidation: React.FC<Props> = (props) => {
const applicationIdSafe = applicationId?.toLowerCase();
const redirectParam = redirect_uri || redirect;

if (emailStored !== email) {
if (email && (emailStored !== email)) {
dispatch(logoutUser());
return;
}

if (!redirectParam) {
dispatch(setRedirectError('application_misconfigured', 'redirect_is_required '));
dispatch(setRedirectError('application_misconfigured', 'redirect_is_required'));
return;
}

if (!applicationIdSafe) {
dispatch(setRedirectError('application_misconfigured', 'application_id_is_required '));
dispatch(setRedirectError('application_misconfigured', 'application_id_is_required'));
return;
}

Expand Down
4 changes: 2 additions & 2 deletions src/services/HatClientService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ export class HatClientService {
return get<HatApplication>(path, { method: 'get', headers: { 'x-auth-token': token } });
}

public async getApplicationHmi(applicationId: string) {
const path = `${this.pathPrefix}/applications/${applicationId}/hmi`;
public async getApplicationHmi(applicationId: string, pda: string) {
const path = `https://${pda}${this.pathPrefix}/applications/${applicationId}/hmi`;

return get<HatApplicationContent>(path);
}
Expand Down