Skip to content

Commit

Permalink
Merge branch 'main' into release
Browse files Browse the repository at this point in the history
  • Loading branch information
aaronshiel committed Oct 10, 2023
2 parents 1d9d999 + 5d3fa2e commit 2fd0473
Show file tree
Hide file tree
Showing 8 changed files with 104 additions and 20 deletions.
9 changes: 6 additions & 3 deletions client/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import {
Organization,
MentorTrainStatusById,
RegenVttResponse,
LoginType,
} from "types";
import { SearchParams } from "hooks/graphql/use-with-data-connection";
import {
Expand Down Expand Up @@ -1957,13 +1958,14 @@ export async function login(accessToken: string): Promise<UserAccessToken> {

export async function loginGoogle(
accessToken: string,
signupCode?: string
signupCode?: string,
loginType?: LoginType
): Promise<UserAccessToken> {
return execGql<UserAccessToken>(
{
query: `
mutation LoginGoogle($accessToken: String!, $signupCode: String, $lockMentorToConfig: Boolean) {
loginGoogle(accessToken: $accessToken, mentorConfig: $signupCode, lockMentorToConfig: $lockMentorToConfig) {
mutation LoginGoogle($accessToken: String!, $signupCode: String, $lockMentorToConfig: Boolean, $loginType: String) {
loginGoogle(accessToken: $accessToken, mentorConfig: $signupCode, lockMentorToConfig: $lockMentorToConfig, loginType: $loginType) {
user {
_id
name
Expand All @@ -1984,6 +1986,7 @@ export async function loginGoogle(
accessToken: accessToken,
signupCode: signupCode,
lockMentorToConfig: true,
loginType: loginType,
},
},
// login responds with set-cookie, w/o withCredentials it doesnt get stored
Expand Down
41 changes: 38 additions & 3 deletions client/src/components/login.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ import { ConfigStatus } from "store/slices/config";
import { useWithLogin } from "store/slices/login/useWithLogin";
import { OverridableTokenClientConfig } from "@react-oauth/google";
import { MentorConfig } from "types-gql";
import { LoginType } from "types";
import { LoginRejectedReason } from "store/slices/login";
import { navigate } from "gatsby";

const useStyles = makeStyles({ name: { LoginPage } })((theme: Theme) => ({
toolbar: {
Expand All @@ -46,8 +49,9 @@ function LoginPage(props: {
overrideConfig?: OverridableTokenClientConfig | undefined
) => void;
mentorConfig?: MentorConfig;
loginType: LoginType;
}): JSX.Element {
const { mentorConfig } = props;
const { mentorConfig, loginType } = props;
const { classes } = useStyles();
const { state: configState, loadConfig } = useWithConfig();
const { state: loginState, login } = useWithLogin();
Expand Down Expand Up @@ -99,8 +103,22 @@ function LoginPage(props: {
<div className={classes.toolbar} /> {/* create space below app bar */}
<Typography variant="h5" className={classes.title}>
{mentorConfig?.loginHeaderText ||
"Please sign in to access the Mentor Studio portal"}
`Please sign ${
loginType === LoginType.SIGN_IN ? "in" : "up"
} to access the Mentor Studio portal`}
</Typography>
{loginState.rejectedReason && (
<Typography variant="h6" color="error">
{loginState.rejectedReason ===
LoginRejectedReason.NO_ACCOUNT_FOUND ? (
<div>
No account found, please <a href="/signup">sign up</a>.
</div>
) : (
"Error signing in"
)}
</Typography>
)}
{process.env.ACCESS_TOKEN ? (
<Button
variant="contained"
Expand All @@ -117,11 +135,28 @@ function LoginPage(props: {
color="primary"
onClick={() => props.onGoogleLogin()}
className={classes.button}
style={{ fontSize: "16px" }}
data-cy="login-btn"
>
Sign in with Google
Sign {loginType === LoginType.SIGN_IN ? "in" : "up"} with Google
</Button>
)}
<Button
variant="contained"
color="primary"
onClick={() => {
if (loginType === LoginType.SIGN_IN) {
navigate("/signup");
} else {
navigate("/");
}
}}
className={classes.button}
style={{ width: "fit-content", textTransform: "none" }}
data-cy="signup-btn"
>
Sign-{loginType === LoginType.SIGN_IN ? "Up" : "In"} page
</Button>
</div>
);
}
Expand Down
5 changes: 3 additions & 2 deletions client/src/pages/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { useWithLogin } from "store/slices/login/useWithLogin";
import { LoginStatus } from "store/slices/login";

import { GoogleOAuthProvider, useGoogleLogin } from "@react-oauth/google";
import { LoginType } from "types";

/**
* Separate functional component in order for useGoogleLogin to be nested under GoogleOAuthProvider (This provider did not want to work in gatsby-browser, bug reported by others)
Expand All @@ -20,13 +21,13 @@ function PrimaryDisplayHolder(): JSX.Element {
const { state: loginState, loginWithGoogle } = useWithLogin();
const login = useGoogleLogin({
onSuccess: (tokenResponse) => {
loginWithGoogle(tokenResponse.access_token);
loginWithGoogle(tokenResponse.access_token, undefined, LoginType.SIGN_IN);
},
});
if (loginState.loginStatus === LoginStatus.AUTHENTICATED) {
return <HomePage />;
} else {
return <LoginPage onGoogleLogin={login} />;
return <LoginPage onGoogleLogin={login} loginType={LoginType.SIGN_IN} />;
}
}

Expand Down
15 changes: 13 additions & 2 deletions client/src/pages/signup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { GoogleOAuthProvider, useGoogleLogin } from "@react-oauth/google";
import withLocation from "wrap-with-location";
import { fetchMentorConfig } from "api";
import { MentorConfig } from "types-gql";
import { LoginType } from "types";

/**
* Separate functional component in order for useGoogleLogin to be nested under GoogleOAuthProvider (This provider did not want to work in gatsby-browser, bug reported by others)
Expand All @@ -38,14 +39,24 @@ function PrimaryDisplayHolder(): JSX.Element {
}, []);
const login = useGoogleLogin({
onSuccess: (tokenResponse) => {
loginWithGoogle(tokenResponse.access_token, signupCode);
loginWithGoogle(
tokenResponse.access_token,
signupCode,
LoginType.SIGN_UP
);
},
});
if (loginState.loginStatus === LoginStatus.AUTHENTICATED) {
return <HomePage />;
} else {
// Check for url param
return <LoginPage onGoogleLogin={login} mentorConfig={mentorConfig} />;
return (
<LoginPage
onGoogleLogin={login}
mentorConfig={mentorConfig}
loginType={LoginType.SIGN_UP}
/>
);
}
}

Expand Down
21 changes: 18 additions & 3 deletions client/src/store/slices/login/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
sessionStorageClear,
sessionStorageStore,
} from "store/local-storage";
import { User } from "types";
import { LoginType, User } from "types";
import { extractErrorMessageFromError } from "helpers";

/** Store */
Expand All @@ -27,11 +27,19 @@ export enum LoginStatus {
FAILED = 4,
}

export enum LoginRejectedReason {
NONE = "NONE",
DISABLED = "DISABLED",
FAILED = "FAILED",
NO_ACCOUNT_FOUND = "NO_ACCOUNT_FOUND",
}

export interface LoginState {
accessToken?: string;
loginStatus: LoginStatus;
user?: User;
isDisabled?: boolean;
rejectedReason?: LoginRejectedReason;
}

const initialState: LoginState = {
Expand Down Expand Up @@ -80,13 +88,15 @@ export const googleLogin = createAsyncThunk(
args: {
accessToken: string;
signupCode?: string;
loginType?: LoginType;
},
thunkAPI
) => {
try {
const googleLogin = await api.loginGoogle(
args.accessToken,
args.signupCode
args.signupCode,
args.loginType
);
localStorageStore(ACCESS_TOKEN_KEY, googleLogin.accessToken);
sessionStorageClear(ACTIVE_MENTOR_KEY);
Expand Down Expand Up @@ -182,8 +192,13 @@ export const loginSlice = createSlice({
);
}
})
.addCase(googleLogin.rejected, (state) => {
.addCase(googleLogin.rejected, (state, action) => {
delete state.user;
if (action.error.message?.includes("No user found")) {
state.rejectedReason = LoginRejectedReason.NO_ACCOUNT_FOUND;
} else {
state.rejectedReason = LoginRejectedReason.FAILED;
}
state.loginStatus = LoginStatus.FAILED;
})
// Add cases for userSawSplashScreen action here, all you need for this one is the fulfilled case
Expand Down
19 changes: 16 additions & 3 deletions client/src/store/slices/login/useWithLogin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,18 @@ import { useEffect } from "react";
import { useAppSelector, useAppDispatch } from "store/hooks";
import { ACCESS_TOKEN_KEY, localStorageGet } from "store/local-storage";
import * as loginActions from ".";
import { LoginType } from "types";

interface UseWithLogin {
state: loginActions.LoginState;
login: (accessToken: string) => void;
userSawSplashScreen: (accessToken: string) => void;
userSawTooltips: (accessToken: string) => void;
loginWithGoogle: (googleAccessToken: string, signupCode?: string) => void;
loginWithGoogle: (
googleAccessToken: string,
signupCode?: string,
loginType?: LoginType
) => void;
logout: () => void;
}

Expand Down Expand Up @@ -45,14 +50,22 @@ export function useWithLogin(): UseWithLogin {
}
}

function loginWithGoogle(googleAccessToken: string, signupCode?: string) {
function loginWithGoogle(
googleAccessToken: string,
signupCode?: string,
loginType?: LoginType
) {
if (
state.loginStatus === loginActions.LoginStatus.NONE ||
state.loginStatus === loginActions.LoginStatus.NOT_LOGGED_IN ||
state.loginStatus === loginActions.LoginStatus.FAILED
) {
dispatch(
loginActions.googleLogin({ accessToken: googleAccessToken, signupCode })
loginActions.googleLogin({
accessToken: googleAccessToken,
signupCode,
loginType,
})
);
}
}
Expand Down
9 changes: 5 additions & 4 deletions client/src/store/slices/questions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,14 @@ export const loadQuestionsById = createAsyncThunk(
): Promise<CancellabeResult<Question[]>> => {
const state = thunkAPI.getState() as RootState;
const ids = args.reload
? args.ids
? args.ids.filter((id) => id)
: args.ids.filter((id) => {
const q = getValueIfKeyExists(id, state.questions.questions);
return (
!q ||
q.status === LoadingStatus.FAILED ||
q.status === LoadingStatus.NONE
id &&
(!q ||
q.status === LoadingStatus.FAILED ||
q.status === LoadingStatus.NONE)
);
});
if (ids.length === 0) {
Expand Down
5 changes: 5 additions & 0 deletions client/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -487,6 +487,11 @@ export enum EditType {
OLD_ANSWER = "OLD_ANSWER",
}

export enum LoginType {
SIGN_IN = "SIGN_IN",
SIGN_UP = "SIGN_UP",
}

export enum LoginStatus {
NONE = 0,
IN_PROGRESS = 1,
Expand Down

0 comments on commit 2fd0473

Please sign in to comment.