Skip to content

Commit

Permalink
Finish oauth with google
Browse files Browse the repository at this point in the history
  • Loading branch information
stefan-petrov1 committed Apr 21, 2024
1 parent a62d08e commit 76c8613
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 9 deletions.
17 changes: 14 additions & 3 deletions client/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,20 @@ function App() {
<Route path={PageEnum.Register} element={<Register />} />
</Route>

{/* Only logged users */}
{/* Only logged users AND finished OAuth2*/}
<Route
element={
<ProtectedRoute
role={RoleEnum.USER}
onlyAuth={true}
blockNotFinishedOAuth={true}
/>
}>
<Route path={PageEnum.Chat} element={<Chat />} />
<Route path={PageEnum.Profile} element={<Profile />} />
</Route>

{/* Only logged users with or without finished OAuth2*/}
<Route
element={
<ProtectedRoute role={RoleEnum.USER} onlyAuth={true} />
Expand All @@ -61,9 +74,7 @@ function App() {
path={PageEnum.FinishRegister}
element={<FinishRegister />}
/>
<Route path={PageEnum.Chat} element={<Chat />} />
<Route path={PageEnum.Logout} element={<Logout />} />
<Route path={PageEnum.Profile} element={<Profile />} />
</Route>

{/* Only organisations */}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,17 @@ import { PageEnum, RoleEnum } from '../../../types';
type ProtectedRouteProps = {
role: RoleEnum | null;
onlyAuth?: boolean;
blockNotFinishedOAuth?: boolean;
};

// The component that protects a route based on the user data
// whether he is logged or not, whether he is a teacher or not, etc.
export default function ProtectedRoute({
role,
onlyAuth,
blockNotFinishedOAuth,
}: ProtectedRouteProps) {
const { user, isOrganisation, isAuthenticated } = useAuthContext();
const { user, isAuthenticated, hasFinishedOAuth2 } = useAuthContext();
const { pathname } = useLocation();

// Attach redirectTo search param
Expand All @@ -38,6 +40,10 @@ export default function ProtectedRoute({
(role === user.role && isAuthenticated) ||
(onlyAuth && isAuthenticated);

if (passThrough && blockNotFinishedOAuth && !hasFinishedOAuth2) {
return <Navigate to={PageEnum.Home} />;
}

if (!user.role && !passThrough && pathname !== PageEnum.Logout) {
return <Navigate to={generateNavPath(PageEnum.Login)} />;
}
Expand Down
10 changes: 5 additions & 5 deletions client/src/pages/auth/register/Register.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import FormInput from '../../../components/form-input/FormInput';
import SkillsSelect, {
SkillOption,
} from '../../../components/skills-select/SkillsSelect';
import { authPaths, skillsPaths } from '../../../config/api';
import { authPaths, OAuthPaths, skillsPaths } from '../../../config/api';
import { useErrorContext } from '../../../contexts/ErrorContext';
import useValidators from '../../../hooks/useValidator';
import { AlertTypeEnum, IAuthResponse, ISkill, RoleEnum } from '../../../types';
Expand Down Expand Up @@ -319,12 +319,12 @@ function Register() {
</div>
<hr className="my-4" />
<div className="d-grid mb-2">
<button
disabled={loading}
<a
className="btn btn-google btn-login text-uppercase fw-bold"
type="submit">
type="submit"
href={OAuthPaths.google}>
<i className="fab fa-google me-2" /> Sign in with Google
</button>
</a>
</div>
</form>
</div>
Expand Down

0 comments on commit 76c8613

Please sign in to comment.